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

Send HTML Email

From WikiJava

Jump to: navigation, search


this example shows how to send an email containing HTML code

Contents

the article

This example uses the JavaMail library to send an email with an HTML page

SimpleMail

import javax.mail.*;
import javax.mail.internet.*;
 
import java.util.Properties;
 
class SimpleHTMLMail {
    public static void main(String[] args) throws Exception{
      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);
      Transport transport = mailSession.getTransport();
 
      MimeMessage message = new MimeMessage(mailSession);
      message.setSubject("Testing javamail html");
      message.setContent
         ("This is a test <b>HOWTO<b>", "text/html; charset=ISO-8859-1");
      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 Email with attachment, 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: