In this tutorials we are going to see how to send the HTML email message using Java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package used for sending email, for that add latest version of mail.jar into your build path, I used javamail-1.4.7 version.Follow the simple steps for create HTML email application using java
Create Java project called EmailSender in your eclipse IDE and then create package called com.javatutorialscorner.email under EmailSender project then create Java class called EmailSender.java under com.javatutorialscorner.email package.
EmailSender.java
package com.javatutorialscorner.email;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailSender {
/**
* @param args
*/
public static void main(String[] args) {
String TO = "toemail@gmail.com";
String host = "smtp.gmail.com";
String port = "465";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties mailConfig = new Properties();
mailConfig.put("mail.smtp.host", host);
mailConfig.put("mail.smtp.socketFactory.port", port);
mailConfig.put("mail.smtp.socketFactory.class", SSL_FACTORY);
mailConfig.put("mail.smtp.auth", "true");
mailConfig.put("mail.smtp.port", port);
Session session = Session.getDefaultInstance(mailConfig,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("fromeail@gmail.com","password");
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO));
message.setSubject("Java Tutorials Corner - HTML Email ");
message.setContent("<h1>Simple Email sent by Java Tutorials Corner</h1>",
"text/html");
Transport.send(message);
System.out.println("Email sent successfully");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
This is Similar to previous Example the only difference is here I used setContent() instead of setText()
message.setContent("<h1>Simple Email sent by Java Tutorials Corner</h1>",
"text/html");
for send html message setcontent() accept two parameter first parameter is html string and second parameter used to set content type
In above program smtp.gmail.com is gmail SMTP server, you can add your SMTP server if you own. The javax.net.ssl.SSLSocketFactory used for SSL support, mail.smtp.auth is to set authentication required by mail server for sending email.The PasswordAuthentication class is used to authenticate user name and password. The Session object used to store information like host,port,user name,password. The getDefaultInstance() method used to get session object.MimeMessage used to set subject ,message and add recipient and The Transport class send() method is used to send MIME message
Now you can run the application and receive the mail.
0 comments:
Post a Comment