wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sending Mail via socket - attachments?" Watch "Sending Mail via socket - attachments?" New topic
Author

Sending Mail via socket - attachments?

Judy Liddle
Greenhorn

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
Greenhorn

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

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 http://www.jscape.com
------------------
JFind - Your Java Resource [url]www.jfind.com[/url
 
 
subject: Sending Mail via socket - attachments?
 
Similar Threads
SMTP Mail problem
javax.mail.AuthenticationFailedException
Mail without authentication
Reading an object from ObjectInputStream
Sending mails from same domain