Properties props = new Properties();
props.put("mail.smtp.host", JOptionPane
.showInputDialog("Your SMTP server name?"));
Message msg = new MimeMessage(Session.getDefaultInstance(props));
InternetAddress addressFrom = new InternetAddress(
"i...@users.sourceforge.net");
msg.setFrom(addressFrom);
msg.setRecipients(Message.RecipientType.TO,
new InternetAddress[] { new InternetAddress(
"i...@users.sourceforge.net") });
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("test");
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(textPart);
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(new File("lgpl.txt"));
multipart.addBodyPart(attachmentPart);
msg.setContent(multipart);
Transport.send(msg);
System.exit(0);
This example uses a text file, but WITL needs to send screen shots as
email attachments. This example also uses JavaMail, Swing, and an
external SMTP server. We would like to avoid those requirements for
WITL, if possible.
The full program:
Socket socket = new Socket(JOptionPane
.showInputDialog("Your SMTP server name?"), 25);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream(), "8859_1"));
out = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream(), "8859_1"));
String boundary = "DataSeparatorString";
for (String s : new String[] { "HELO theWorld",
"MAIL FROM: <i...@users.sourceforge.net>",
"RCPT TO: <i...@users.sourceforge.net>", "DATA" }) {
out.write(s + "\r\n");
out.flush();
s = in.readLine();
}
for (String s : new String[] { "MIME-Version: 1.0",
"Content-Type: multipart/mixed; boundary=\"" + boundary + "\"",
"\r\n--" + boundary,
"Content-Type: text/plain; charset=\"us-ascii\"\r\n",
"test\r\n\r\n", "\r\n--" + boundary,
"Content-Type:text/plain; name=lgpl.txt",
"Content-Disposition: attachment;filename=\"lgpl.txt\"",
"Content-transfer-encoding: base64\r\n", "{data}",
"\r\n--" + boundary, "\r\n\r\n--" + boundary + "--\r\n" }) {
if (s.equals("{data}")) {
MIMEBase64.encode("lgpl.txt");
} else {
out.write(s + "\r\n");
out.flush();
}
}
for (String s : new String[] { ".", "QUIT" }) {
out.write(s + "\r\n");
out.flush();
s = in.readLine();
}
socket.close();
System.exit(0);
Here's the rest of the code:
http://groups.google.com/group/witl/web/SendFile2.java