I need to send an email from my application using Gmail as the SMTP server. This is my mail connector class and I have set values in a separate property file
public class EmailConnector {
public static Session sessionCreate() {
final String fromEmail = ConfigurationManager.getInstance().getProperty(EmailConfig.SENDER_EMAIL.toString());
final String password = ConfigurationManager.getInstance().getProperty(EmailConfig.SENDER_PASSWORD.toString());
Properties props = new Properties();
props.put("mail.smtp.host", ConfigurationManager.getInstance().getProperty(EmailConfig.SMTP_HOST.toString()));
props.put("mail.smtp.socketFactory.port",
ConfigurationManager.getInstance().getProperty(EmailConfig.SSL_PORT.toString()));
props.put("mail.smtp.socketFactory.class",
ConfigurationManager.getInstance().getProperty(EmailConfig.SSL_FACTORY_CLASS.toString()));
props.put("mail.smtp.auth",
ConfigurationManager.getInstance().getProperty(EmailConfig.SMTP_AUTHENTICATION.toString()));
props.put("mail.smtp.port", ConfigurationManager.getInstance().getProperty(EmailConfig.SMTP_PORT.toString()));
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
return Session.getDefaultInstance(props, auth);
}
}
Properties:
#Email send configuration
SENDER_EMAIL = [email protected]
SENDER_PASSWORD = abcalert321
SMTP_HOST = smtp.gmail.com
SSL_PORT = 465
SMTP_AUTHENTICATION = true
SMTP_PORT = 465
SSL_FACTORY_CLASS = javax.net.ssl.SSLSocketFactory
Then I implemented a mail sender class, called "GroupEmail.class"
public class GroupEmail {
public void sendEmail() throws IOException {
String recipient = "[email protected]";
Session session = EmailConnector.sessionCreate();
/* subject of email */
String emailSubject = "ABC_Alert";
try {
MimeMessage msg = new MimeMessage(session);
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress("[email protected]", "ABC Alerts"));
msg.setReplyTo(InternetAddress.parse("[email protected]"));
msg.setSubject(emailSubject, "UTF-8");
msg.setSentDate(new Date());
/* buyer email address */
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
/* Create the message body part */
msg.setText("A new Test-Alert from AB_Alerts");
/* Send message */
Transport.send(msg, "[email protected]", "abcalert321");
} catch (MessagingException | UnsupportedEncodingException e) {
SystemLogger.logErrorMessege(this, e);
}
}
}
After all I called the "GroupEmail.class" in a place that I needed to trigger the email to be sent.
GroupEmail groupEmail = new GroupEmail();
groupEmail.sendEmail();
I used Tomcat v8 server on localhost and when the application works, I got the below exception.
98656 [http-nio-8080-exec-9] ERROR it.codegen.rnd.cgalert.notification.template.email.GroupEmail - Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
..more