mail
Send e-mail via GMail
With this example we are going to see how to send an e-mail via GMail in a Java program. To do that you have to specify certain parameters about the SMTP host you have to use.
In short to send an email via GMail in a Java Application, one should follow these steps:
- Set smtp properties using a
Properties
object. - Use
put("mail.smtp.host", host)
to set the smtp host. - Use put(“mail.smtp.port”, 25) to set the smtp port.
- Use
put("mail.debug", "true")
if you want to recieve debugging messages. - Use
put("mail.transport.protocol", "smtp")
to set the email protocol. - Use
put("mail.smtp.auth", "true")
to set on the authentication. - Use
put("mail.smtp.starttls.enable", "true")
to set on the tls protocol. - Create e class tha extends
Authenticator
. - Override
getPasswordAuthentication
that returns anew PasswordAuthentication(username, password)
. - Use create a new
Session
. - Create a
Message
using aMimeMessage
. - Set message source using
setFrom(new InternetAddress(from))
. - Set message recipients use
setRecipients(Message.RecipientType.TO, address).
- Use a
Transport
to send the email. - Use
send
to send the email.
Let’s see the code:
package com.javacodegeeks.snippets.enterprise; import java.util.Date; import java.util.Properties; 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; public class SendEMailViaGMail { private static final String username = "myusername"; private static final String password = "mypassword"; public static void main(String[] args) { String host = "smtp.gmail.com"; String from = "myuser@gmail.com"; String to = "myrecipient@mailprovider.com"; // Set properties Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.port", 25); props.put("mail.debug", "true"); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); // Get session with authenticator Session session = Session.getInstance(props, new GMailAuthenticator()); try { // Instantiate a message Message msg = new MimeMessage(session); // Set the FROM message msg.setFrom(new InternetAddress(from)); // The recipients can be more than one so we use an array but you can // use 'new InternetAddress(to)' for only one address. InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); // Set the message subject and date we sent it. msg.setSubject("Email from JavaMail test"); msg.setSentDate(new Date()); // Set message content msg.setText("Java Code Geeks - Java Examples & Code Snippets"); // Send the message Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); } } private static class GMailAuthenticator extends Authenticator { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } } }
This was an example on how to send e-mail via GMail.