After going through post provided for the same problem, I have written the following code. But I am getting the following exception :
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect
package com.nura.mail;
import constants.Constants;
import java.util.Properties;
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 logger.LoggerUtil;
/**
*
* @author Arun kumar
*/
public class SendMail implements Constants {
private final static LoggerUtil log = new LoggerUtil();
public static void main(String receiver, String msg, String header) {
log.addLog("Entry in SendMail");
final String username = "sendmailnotification";
final String password = "version5.0";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(receiver));
message.setSubject(header);
message.setText(msg);
Transport.send(message);
log.addLog("Your mail has been sent");
log.addLog("Exit from Send Mail");
} catch (MessagingException e) {
log.addLog(e.getLocalizedMessage());
}
}
public static void main(String[] args) {
SendMail.main("aprodigalboy@gmail.com", "test", "test");
System.out.println("End of the program");
}
}
I have created a signup form ,I get the email from the signup field and sending the OTP but i could not receive OTP due to this error
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/C:/Program%20Files%20(x86)/NetBeans%208.0.1/java/modules/ext/hibernate4/slf4j-simple-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/Ashok/Final/CloudArmour%20(2)/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/Ashok/Final/CloudArmour%20(2)/lib/slf4j-simple-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 492 [main] INFO logger.LoggerUtil - Entry in SendMail End of the program 22060 [main] INFO logger.LoggerUtil - Could not connect to SMTP host: smtp.gmail.com, port: 587