Thursday, August 20, 2009

Java Mail need authentication, including send mail by Gmail

Sample of Java Mail need authentication

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

import java.util.Properties;


public class SimpleMail {

private static final String SMTP_HOST_NAME = "smtp.myserver.com";
private static final String SMTP_AUTH_USER = "myusername";
private static final String SMTP_AUTH_PWD = "mypwd";

public static void main(String[] args) throws Exception{
new SimpleMail().test();
}

public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");

Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
// uncomment for debugging infos to stdout
// mailSession.setDebug(true);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setContent("This is a test", "text/plain");
message.setFrom(new InternetAddress("me@myhost.org"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("elvis@presley.org"));

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

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}

Sample of sending mail by Gmail SMTPs

import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;


public class SimpleSSLMail {

private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final int SMTP_HOST_PORT = 465;
private static final String SMTP_AUTH_USER = "myaccount@gmail.com";
private static final String SMTP_AUTH_PWD = "mypwd";

public static void main(String[] args) throws Exception{
new SimpleSSLMail().test();
}

public void test() throws Exception{
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(true);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("This is a test", "text/plain");

message.addRecipient(Message.RecipientType.TO,
new InternetAddress("elvis@presley.org"));

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

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


 



Settings for well known mail providers

Yahoo


Incoming Mail Server - pop.mail.yahoo.com (POP3 - port 110)


Outgoing Mail Server - smtp.mail.yahoo.com (SMPTP - port 25)


Google GMail


Incoming Mail Server - pop.gmail.com (POP3S SSL enabled, port 995)


Outgoing Mail Server - gmail.com (SMPTS SSL enabled, port 465)

No comments:

Post a Comment