JavaMail: Send Mail Using Gmail’s SMTP Server
Posted by tech on
January 1, 2009
Sometimes having our own SMTP server can cause problems especially the newbie ones. If our SMTP server is being detected by such spam security sites like Spamhaus as a probable spam server, emails that we send to recipients will eventually be blocked. We would have to register our server to these sites so that they would get unblocked. If you are too lazy to wait or want to avoid the hassle of this kind of dilemna, you can actually use GMail’s SMTP server.
The code below sends an email using JavaMail, Java’s mail technology API to send and receive emails. It also includes a Reply-To Email parameter in case you want your Reply-To email address to be different. The other parameter option is to specify if you want to email to be sent at text or HTML. Just input html as the value if you want to send the email as HTML.
public static boolean send(String replyTo, String from_email, String from_name, String to_email, String subject, String body, String type, String bcc_email) {
boolean sent = false;
try {
Properties prop = new Properties();
prop.put("mail.smtp.port", "25");
prop.put("mail.smtp.socketFactory.fallback", "false");
prop.put("mail.smtp.quitwait", "false");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(prop);
String username = (String) prop.get("gmail.username");
String password = (String) prop.get("gmail.password");
Message msg = new MimeMessage(session);
msg.setSubject(subject);
InternetAddress from = new InternetAddress(from_email, from_name);
InternetAddress to = new InternetAddress(to_email);
msg.addRecipient(Message.RecipientType.TO, to);
// add bcc if not null
if ((bcc_email != null) && (!bcc_email.equals(""))) msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc_email));
msg.setFrom(from);
// set reply to email address
InternetAddress[] replyToAddress = new InternetAddress[1];
replyToAddress[0] = new InternetAddress(replyTo);
msg.setReplyTo(replyToAddress);
Multipart multipart = new MimeMultipart("related");
BodyPart htmlPart = new MimeBodyPart();
if (type.equals("html")) htmlPart.setContent(body, "text/html");
else htmlPart.setContent(body, "text/plain");
multipart.addBodyPart(htmlPart);
msg.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(GMAIL_USERNAME, GMAIL_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
sent = true;
System.out.println("[MailTool] mail sent successfully ...");
} catch (Exception e) {
System.err.println("[MailTool] send() : " + e.getMessage());
e.printStackTrace();
}
return sent;
}
Note that connecting to GMail’s SMTP server requires you to authenticate first so you would have to use a GMail account before transactions push through.
Donations appreciated. Every little $ helps. Or click Google +1








October 24th, 2010 at 7:24 pm
@alvaro01: thanks for pointing out this typo
September 15th, 2011 at 3:26 am
Thanks alot for your help.
Your tutorial really helpful.
Good job guy.
September 15th, 2011 at 3:27 am
@mr big: thanks