Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

  • Advertisements

Send EMAIL using Java Code

Posted by Pankil Patel on April 7, 2010

Required Jar:


package cc.co.enlightensof.sendmail.service.spring;

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 TestJavaMailSenderImpl {
public static final String USERNAME = “proxibidinc@gmail.com”;
public static final String PASSWORD = “1qlchexaacuby”;
public static final String HOST = “smtp.gmail.com”;
// gmail-smtp.l.google.com or gmail-smtp-in.l.google.comans check if you are
// getting the same error?
// “gmail-smtp-in.l.google.com and ”
public static final String PORT = “465”;
// public static final String PORT = “25”;
public Session mailSession;

public TestJavaMailSenderImpl() {
Properties props = new Properties();
props.put(“mail.smtp.user”, USERNAME);
props.put(“mail.smtp.host”, HOST);
props.put(“mail.smtp.port”, PORT);
props.put(“mail.transport.protocol”, “smtp”);
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.socketFactory.fallback”, “false”);

Authenticator auth = new SMTPAuthenticator();
mailSession = Session.getInstance(props, auth);

public void postMail(String recipients[], String subject, String message,
String from) throws MessagingException {
Message msg = new MimeMessage(mailSession);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Optional : You can also set your custom headers in the Email if you
// Want
msg.addHeader("MyHeaderName", "myHeaderValue");

// Setting the Subject and Content Type
msg.setContent(message, "text/plain");

class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = TestJavaMailSenderImpl.USERNAME;
String password = TestJavaMailSenderImpl.PASSWORD;
return new PasswordAuthentication(username, password);

Main Class to run the code:

package cc.co.enlightensof.sendmail.service.spring;

import javax.mail.MessagingException;

public class Main {
public static void main(String[] args) {
TestJavaMailSenderImpl sendMail = new TestJavaMailSenderImpl();
String recipients[] = { “hello.pankilpatel@gmail.com” };
String subject = “Test”;
String message = “Simple mail”;
try {
sendMail.postMail(recipients, subject, message,
} catch (MessagingException e) {


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: