File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Sending Mail via socket - attachments? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sending Mail via socket - attachments?" Watch "Sending Mail via socket - attachments?" New topic

Sending Mail via socket - attachments?

Judy Liddle

Joined: Sep 28, 2000
Posts: 8
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

Joined: Sep 28, 2000
Posts: 8
If anyone else is interested in this question...
I found out that the smtp protocol ( one way to send mail through Java without using Java Mail API.
But the Smtp API does not seeem to support attachments.
public class
extends {
public void closeServer()
throws IOException;
public void to(String)
throws IOException;
public void from(String)
throws IOException;
public PrintStream startMessage()
throws IOException;
throws IOException;
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
Multipart multipart = new MimeMultipart();
// 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(
messageBodyPart.setDataHandler(new DataHandler(

// add the Multipart to the message
Transport.send( message );

Van Glass
Ranch Hand

Joined: Nov 18, 2000
Posts: 110
JMail is is nice lightweight solution. Small in size yet supports features like attachments and embedded HTML. They have a free download at
JFind - Your Java Resource [url][/url
Don't get me started about those stupid light bulbs.
subject: Sending Mail via socket - attachments?
It's not a secret anymore!