JAVA MAIL API

JavaMail Projects

JavaMail Project 1

Sending an HTML E-Mail with using JavaMail API
Previous Home Next
adplus-dvertising

If you want to send an HTML E-Mail from your machine. Now it is assumed that your local host connected to the internet and it's capable for send an E-Mail. That time you using the setContent() method to set the content and second argument is "text/html" . The "text/html" is specify that HTML content is including in the message.

package R4R;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class HTMLMailSend
   public static void main(String [] args)
   {
            // Recipient's email ID needs to be mentioned.
      String to = "r4r@localhost";
      // Sender's email ID needs to be mentioned
      String from = "r4r111@localhost";
      // Assuming you are sending email from localhost
      String host = "localhost";
      // Get system properties
      Properties properties = System.getProperties();
      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);
      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);
         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));
         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));
         // Set Subject: header field
message.setSubject("This is the Subject Line!"); // Send the actual HTML message, as big as you like message.setContent("<h1>This is the HTML MAIL SEND PROGRAM</h1>", "text/html" ); // Send message Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } }
Previous Home Next