Archive for SMTP

javamail smtp with Gmail

Posted in Programming with tags , , , , on August 3, 2007 by wsjoung

package com.mycompany.emailsender;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* Servlet implementation class for Servlet: GmailSender
*
*/
public class GmailSender extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public GmailSender() {
super();
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
final String SMTP_HOST_NAME = “smtp.gmail.com”;
final int SMTP_HOST_PORT = 465;
final String SMTP_AUTH_USER = “XXXXXX@gmail.com”;
final String SMTP_AUTH_PWD = “XXXXXX”;
final String SSL_FACTORY = “javax.net.ssl.SSLSocketFactory”;

String emailFromAddress = null;
String[] sendTo = null;
String subject = null;
String content = null;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

String address = request.getParameter(“address”);
sendTo = address.split(“;”);

emailFromAddress = request.getParameter(“sender”);
this.subject = request.getParameter(“title”);
this.content = request.getParameter(“content”);

try {
for (int i=0; i<sendTo.length; i++) {
sendSSLMessage(sendTo[i]);
}
System.out.println(“Sucessfully Sent mail to All Users”);

} catch (Exception e) {System.out.println(e);}
}

public void sendSSLMessage(String dest) throws MessagingException {

Properties props = new Properties();

props.put(“mail.transport.protocol”, “smtps”);
props.put(“mail.smtps.host”, SMTP_HOST_NAME);
props.put(“mail.smtps.auth”, “true”);
props.put(“mail.smtps.quitwait”, “false”);

Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(false);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
InternetAddress addressFrom = new InternetAddress(emailFromAddress);
message.setFrom(addressFrom);
message.setContent(content, “text/html”);

message.addRecipient(Message.RecipientType.TO,new InternetAddress(dest));

transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}

reference: http://www.rgagnon.com/javadetails/java-0083.html