Official Site

click here to view our new site launched

Pages

Friday, February 4, 2011

Send Mail in Java


package mail;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendMail {
String MAIL_SERVER = "smtp.gmail.com";


public void Sendmail(String USERNAME,String PASSWORD,String fromAddress,String toAddress,String subject,String message) {
try {

Properties properties = System.getProperties();
properties.put("mail.smtps.host", MAIL_SERVER);
properties.put("mail.smtps.auth", "true");

Session session = Session.getInstance(properties);
MimeMessage msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(fromAddress));
msg.addRecipients(Message.RecipientType.TO, toAddress);
msg.setSubject(subject);
msg.setText(message);

Transport tr = session.getTransport("smtps");
tr.connect(MAIL_SERVER, USERNAME, PASSWORD);
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
} catch (AddressException ex) {
System.out.println(ex.getMessage());
}
catch (MessagingException ex) {
System.out.println(ex.getMessage());
}

}



public static void main(String[] args){

String USERNAME="xxxx@gmail.com";
String PASSWORD="your password";
String fromAddress="xxx@gmail.com";
String toAddress="toaddress";
String subject="your subject";
String message="your message";

s.Sendmail(USERNAME, PASSWORD, fromAddress, toAddress, subject, message);

}


}



click here to download mail.jar and SendMail Java bean

No comments:

Post a Comment

$2.49 .COM at GoDaddy.com! Expires 5/14/13