Sending Simple HTML Mail Through Java

Monday, July 13, 2009

Now a Day's informing the Clients about the successful updation of data or sending other automated information form your Java Programs, either from your Servlets or from your Applications has become a requirement, more than a feature. Here we show how to use the Java Mail API to send a Mail.
To Test Program all you need to have is a SMTP address (Which your ISP Provides).

The Google GMail service offers email client access for retrieving and sending emails through your Gmail account.
However, for security reasons, GMail uses POP3 over an SSL connection, so make sure your email client supports encrypted SSL connections.

Google Gmail Incoming Mail Server (POP3) - pop.gmail.com (SSL enabled,port 995)Outgoing Mail Server - use the SMTP mail server address provided by your local ISP or smtp.gmail.com (SSL enabled, port 465)

For this application you need two jar files those are
You need 2 jars : mail.jar and activation.jar.


The following java class contains simple javamail example
GoogleSimpleMailSender.java


/**
* File Name : GoogleSimpleMailSender.java
* Created By : Nagarajuv
* Created Date : Jun 19, 2009
* Purpose :
* Tables (for selecting) :
* Tables (by inserting/updating) :
* :::Change History :::
* Add comment
*/
package com.raj.mailservice;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import java.security.Security;
import java.util.Properties;

/**
* @author nagarajuv
*
*/
public class GoogleSimpleMailSender {

private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_PORT = "465";
private static final String emailMsgTxt = "

Hi! From HtmlJavaMail

";
private static final String emailSubjectTxt = "Hi raju accept my wishes";
private static final String emailFromAddress = "raju.veeranala@gmail.com";
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[] sendTo = {"raju.veeranala@gmail.com", "nagaraju.veeranala@valuemomentum.biz"};

public void sendSSLMessage(String recipients[], String subject,
String messageTxt, String from) throws MessagingException {

Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("yourmail@gmail.com",
"password");
}
});

Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
System.out.println("recipient " + i + "-->" + recipients[i]);
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

msg.setSubject(subject);

msg.setContent(messageTxt, "text/html");

Transport.send(msg);
}// end of sendSSLMessage

public static void main(String args[]) throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
new GoogleSimpleMailSender().sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}//end main
}//end class

Enjoy with this is example
Image Hosted by ImageShack.us
Image Hosted by ImageShack.us

0 comments: