Thursday, March 1, 2012

GMail Send

package az.mb;
import java.util.Properties;
import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GMail {
@Resource(name = "vpdmail")
private Session mailSession;
public void sendMessage(String email,String head,String mesaj) throws Exception {
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
boolean debug = true;
String SMTP_HOST_NAME = "smtp.gmail.com";
String SMTP_PORT = "465";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put ("mail.smtp.socketFactory.fallback", "false");
mailSession = Session.getInstance (props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email", "password");
}
});
mailSession.setDebug(debug);
Message msg = new MimeMessage(mailSession);
msg.setSubject(head);
msg.setRecipients (Message.RecipientType.TO,
InternetAddress.parse(email));
msg.setText(mesaj);
Transport.send(msg);
System.out.println("message sent");
}
}

No comments:

Post a Comment