• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending Mail via socket - attachments?

 
Judy Liddle
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently using our server and mail host to send emails
via a socket. I am not trying to have a mail server, just the ability to send automated emails.
This works fine, but now I want to be able to add a file as an attachment.
Any suggestions?
 
Judy Liddle
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anyone else is interested in this question...
I found out that the smtp protocol (sun.net.smtp.SmtpClient)is one way to send mail through Java without using Java Mail API.
But the Smtp API does not seeem to support attachments.
public class sun.net.smtp.SmtpClient
extends sun.net.TransferProtocolClient {
public void closeServer()
throws IOException;
public void to(String)
throws IOException;
public void from(String)
throws IOException;
public PrintStream startMessage()
throws IOException;
public sun.net.smtp.SmtpClient(String)
throws IOException;
public sun.net.smtp.SmtpClient()
throws IOException;
public String getMailHost();
}
I found a great example that uses Java Mail to send an attachment.
// create mime message object
// set the required parameters
MimeMessage message = createMessage(to, cc, subject);

// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
//fill message
messageBodyPart.setText(msg);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// fill the array of files to be attached
File [] attachments = {
new File("x.txt"),
new File("y.htm"),
new File("z.jpg")
};
for( int i = 0; i < attachments.length; i++ )
{
messageBodyPart = new MimeBodyPart();
FileDataSource fileDataSource =new FileDataSource(
attachments[i]);
messageBodyPart.setDataHandler(new DataHandler(
fileDataSource));
messageBodyPart.setFileName(
attachments[i].getName());

multipart.addBodyPart(messageBodyPart);
}
// add the Multipart to the message
message.setContent(multipart);
// SEND THE MESSAGE
Transport.send( message );

 
Van Glass
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JMail is is nice lightweight solution. Small in size yet supports features like attachments and embedded HTML. They have a free download at http://www.jscape.com
------------------
JFind - Your Java Resource [url]www.jfind.com[/url
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic