Thursday, March 29, 2007

Using stmp.gmail.com To Send Mail

Here is a snippet of code I wrote to use smtp.gmail.com to send mails.
Please use vaild email address.

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

/**
* @author prasad
*/

public class MailInfo {
public void send() throws EmailException {
Email email = new SimpleEmail();
String authuser = "--------@gmail.com";
String authpwd = "**********";
// Very Important, Don't use email.setAuthentication()
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(false); // true if you want to debug
email.setHostName("smtp.gmail.com");
email.getMailSession().getProperties().put(
"mail.smtp.starttls.enable", "true");

email.setFrom("++++++++@gmail.com", "SenderName");

email.setSubject("TestMail");
email.setMsg("This is a test mail?");

email.addTo(".......@gmail.com", "ToName");
email.send();
}
}

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?