sending an attachment

3 views
Skip to first unread message

Isaac4given

unread,
Aug 17, 2007, 10:15:51 AM8/17/07
to WITL (Walk In The Light)
Here is the Java code to send an email attachment from a file (with
JavaMail):

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:

http://groups.google.com/group/witl/web/SendFile.java

Isaac4given

unread,
Aug 20, 2007, 1:01:06 PM8/20/07
to WITL (Walk In The Light)
Here's part of the code to send an attachment without JavaMail:

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

Reply all
Reply to author
Forward
0 new messages