SEGUE...
-Original Message-From: Alipio Krohn
[mailto:[EMAIL PROTECTED]Sent: terça-feira, 18 de março de 2003
07:53To: [EMAIL PROTECTED]Subject: [java-list]
Anexar arquivo em email.
Senhores...
Alguém já implementou alguma
rotina de envie email contendoanexo, seja em JAVA ou em PL/SQL
(ORACLE) ???
Obrigado,
Obs... George Bush provou que é
realmente LOUCO !!! NÃO À GUERRA !!!
Send email with an attachment
In this example, Elvis is sending a GIF of his old Gumby friend. ()
import java.io.*;
import java.net.*;
public class testMailMIME {
static int SMTPport = 25;
static Socket socket;
static DataInputStream in;
static DataOutputStream out;
static PrintStream prout;
/* Name of file to be sent. */
String FileName = gumby.gif;
public static void main(String s[]) {
/*
** testMAILMIME [server] [destinataire]
*/
testMailMIME t = new testMailMIME();
t.sendMail(s[0], s[1]);
}
public void sendMail(String mailServer, String recipient) {
System.out.println(Send mail with attached file);
try {
Socket s = new Socket(mailServer, 25);
BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream(), 8859_1));
BufferedWriter out = new BufferedWriter
(new OutputStreamWriter(s.getOutputStream(), 8859_1));
String boundary = DataSeparatorString;
// here you are supposed to send your username
sendln(in, out, HELO theWorld);
// warning : some mail server validate the sender address
// in the MAIL FROM command, put your real address here
sendln(in, out, MAIL FROM: [EMAIL PROTECTED]);
sendln(in, out, RCPT TO: + recipient + );
sendln(in, out, DATA);
sendln(out, MIME-Version: 1.0);
sendln(out, Subject: remember me);
sendln(out, From: Elvis Presley [EMAIL PROTECTED]);
sendln(out, Content-Type: multipart/mixed; boundary=\ + boundary +\);
sendln(out, \r\n-- + boundary);
// Send the body
sendln(out, Content-Type: text/plain; charset=\us-ascii\\r\n);
sendln(out, I'm alive. Help me!\r\n\r\n);
sendln(out, \r\n-- + boundary );
// send the GIF
sendln(out, Content-Type:image/gif; name=+FileName);
sendln(out, Content-Disposition: attachment;filename=\+FileName+\);
sendln(out, Content-transfer-encoding: base64\r\n);
MIMEBase64.encode(FileName, out);
sendln(out, \r\n-- + boundary);
sendln(out, \r\n\r\n-- + boundary + --\r\n);
sendln(in, out,.);
sendln(in, out, QUIT);
s.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void sendln(BufferedReader in, BufferedWriter out, String s) {
try {
out.write(s + \r\n);
out.flush();
// System.out.println(s);
s = in.readLine();
// System.out.println(s);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void sendln(BufferedWriter out, String s) {
try {
out.write(s + \r\n);
out.flush();
System.out.println(s);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
And the MIMEBase64 class
import java.io.*;
public class MIMEBase64 {
/*
Base64 uses a 65 character subset of US-ASCII,
allowing 6 bits for each character so the character
m with a Base64 value of 38, when represented
in binary form, is 100110.
With a text string, let's say men is encoded this
is what happens :
The text string is converted into its US-ASCII value.
The character m has the decimal value of 109
The character e has the decimal value of 101
The character n has the decimal value of 110
When converted to binary the string looks like this :
m 01101101
e 01100101
n 01101110
These three 8-bits are concatenated to make a
24 bit stream
011011010110010101101110
This 24 bit stream is then split up into 4 6-bit
sections
011011 010110 010101 101110
We now have 4 values. These binary values are
converted to decimal form
27 22 21 46
And the corresponding Base64 character are :
b W V u
The encoding is always on a three characters basis
(to have a set of 4 Base64 characters). To encode one
or two then, we use the special character = to pad
until 4 base64 characters is reached.
ex. encode me
01101101 01100101
0110110101100101
011011 010110 0101
11(AND to fill the missing bits)
011011 010110 010100
b W U
b W U = (= is the padding character)
so bWU= is the base64 equivalent.
encode m
01101101
011011 01
11 (AND to fill the missing bits)
011011 01
b Q = = (two paddings are added)
Finally, MIME specifies that lines are 76 characters wide maximum.
*/
static String