Tuesday, 22nd June 2010
The new frontier for learning Java

Send Email with attachment

From WikiJava

Jump to: navigation, search


this example shows how to send an email containing an attachment

Contents

the article

This example uses the JavaMail library to send an email with an attachment

SimpleMail

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
 
import java.util.Properties;
 
class SimpleMailWithAttachment {
    public static void main(String[] args) throws Exception{
      boolean debug = false;
      Properties props = new Properties();
      props.setProperty("mail.transport.protocol", "smtp");
      props.setProperty("mail.host", "mymail.server.org");
      props.setProperty("mail.user", "emailuser");
      props.setProperty("mail.password", "");
 
      Session mailSession = Session.getDefaultInstance(props, null);
      mailSession.setDebug(debug);
      Transport transport = mailSession.getTransport();
 
      MimeMessage message = new MimeMessage(mailSession);
      message.setSubject("Testing javamail with attachment");
 
      MimeBodyPart textPart = new MimeBodyPart();
      textPart.setContent("<h1>Check attachment</h1>", "text/html");
 
      MimeBodyPart attachFilePart = new MimeBodyPart();
      FileDataSource fds = 
          new FileDataSource("SimpleMailWithAttachment.java");
      attachFilePart.setDataHandler(new DataHandler(fds));
      attachFilePart.setFileName(fds.getName());
 
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(textPart);
      mp.addBodyPart(attachFilePart);
 
      message.setContent(mp);
      message.addRecipient(Message.RecipientType.TO,
          new InternetAddress("elvis@presley.org"));
 
      transport.connect();
      transport.sendMessage(message,
          message.getRecipients(Message.RecipientType.TO));
      transport.close();
    }
}

See Also

Send Simple email, send HTML Email, http://java.sun.com/products/javamail/FAQ.html, Real's Java How To

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Title (required):

Website:

Comment: