One might ask why bother using Java (or any other programming language) to send mail using GMail, since you can easily do that using GMail web. But consider you want to send the same mail to some one, so that they will think the mail was sent only to them (personalize mass email).
(Today it is Sinhala New year festival, in Sri Lanka. So this was a great tool for me to wish everyone).
I tried to use the basic mail client with Google, but it didn't work out as GMail needs more settings. But I found the best Android hacker, aka Dims had put up a nice blog on using Android to send mails with GMail. So I extracted some code from that for this task.
First get activation.jar and mail.jar and put them in your classpath.
Put your recipients list in a simple text file like this and name it recipients.txt.
John=john@greatplace.com
Siripala=siri@srilanka.net
Then use the following code. Make sure you put valid gmail username and a password.
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/**
* @author : Eran Chinthaka
* @date : Apr 13, 2008
*/
public class GMailClient extends Authenticator {
// Edit these settings with your username and password
private String gmailUserName = "username";
private String gmailPassword = "password";
//=====================================================
public void postMail(String recipients[], String subject,
String message, String from)
throws MessagingException {
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
Session session = Session.getDefaultInstance(props, this);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo =
new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/html");
Transport.send(msg);
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(gmailUserName, gmailPassword);
}
public static void main(String[] args) {
try {
String message = "Your HTML Message goes here";
String subject = "This is the subject";
String fromAddress = "from@whatever.com";
GMailClient mailClient = new GMailClient();
BufferedReader in = new BufferedReader(
new FileReader("recipients.txt"));
String str;
while ((str = in.readLine()) != null) {
String[] details = str.split("=");
String name = details[0];
String email = details[1];
mailClient.postMail(new String[]{email}, subject,
"Hi " + name + ", \n" + message, fromAddress);
}
in.close();
} catch (IOException e1) {
e1.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Read more...