Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Archive for the ‘EMAIL’ Category

Send EMAIL using Java Code

Posted by Pankil Patel on April 7, 2010

Required Jar:
javaMail1.2

Code:

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.class”,
“javax.net.ssl.SSLSocketFactory”);
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);
msg.setFrom(addressFrom);

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.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
}

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,
TestJavaMailSenderImpl.USERNAME);
System.out.println(“Sent……………………”);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}

Posted in Core Java, EMAIL | Leave a Comment »