Last active
February 27, 2018 16:36
-
-
Save garcia-jj/9e8112191fe6845706ff to your computer and use it in GitHub Desktop.
Sending email via GMail with Java SE environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.otavio.mail.helper; | |
import java.util.Properties; | |
import javax.mail.Authenticator; | |
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; | |
/** | |
* Sends an e-mail using GMail, | |
* @author Otávio S Garcia | |
*/ | |
public class GMailSender { | |
private static final String SMTP_HOST = "smtp.gmail.com"; | |
private static final int SMTP_PORT = 465; | |
private final String user; | |
private final String pass; | |
private boolean debug; | |
public GMailSender(String user, String pass) { | |
this.user = user; | |
this.pass = pass; | |
} | |
/** | |
* All transport messages will printed into console. | |
*/ | |
public GMailSender debug() { | |
debug = true; | |
return this; | |
} | |
/** | |
* Sends the email. | |
*/ | |
public void sendMail(String from, String to, String subject, String message) { | |
final Session session = Session.getDefaultInstance(createMailProperties(), createAuthenticator()); | |
final Message msg = new MimeMessage(session); | |
try { | |
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); | |
msg.setFrom(new InternetAddress(from)); | |
msg.setSubject(subject); | |
msg.setContent(message, "text/plain"); | |
msg.saveChanges(); | |
Transport.send(msg); | |
} catch (Exception e) { | |
throw new IllegalStateException("The e-mail can't sent.", e); | |
} | |
} | |
private Authenticator createAuthenticator() { | |
return new GMailAuthenticator(user, pass); | |
} | |
private Properties createMailProperties() { | |
final Properties props = new Properties(); | |
props.put("mail.transport.protocol", "smtp"); | |
props.put("mail.smtp.starttls.enable", "true"); | |
props.put("mail.smtp.host", SMTP_HOST); | |
props.put("mail.smtp.auth", "true"); | |
props.put("mail.smtp.user", user); | |
props.put("mail.smtp.port", SMTP_PORT); | |
props.put("mail.smtp.socketFactory.port", SMTP_PORT); | |
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | |
props.put("mail.smtp.socketFactory.fallback", "false"); | |
props.put("mail.debug", String.valueOf(debug)); | |
return props; | |
} | |
class GMailAuthenticator extends Authenticator { | |
public final String username; | |
public final String password; | |
public GMailAuthenticator(String username, String password) { | |
this.username = username; | |
this.password = password; | |
} | |
@Override | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication(username, password); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment