Reputation: 43
I am trying to send a mail using mail.smtp.yahoo.com in java
Here is my code for sending a mail :
final String Username = "*****************";
final String Password = "*****************";
String to = "*****************";
String host = "smtp.mail.yahoo.com";
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", Username);
properties.put("mail.smtp.password", Password);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(Username));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, Username, Password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
I am getting this error :
avax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 587;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at MailHelper.main(MailHelper.java:51)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at sun.security.ssl.InputRecord.handleUnknownRecord(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 4 more
I tried to send it using port 25,465 also but to no avail. I also tried to send it using smtp.gmail.com and gmail userid and pass but getting different errors.
Please Help
Upvotes: 0
Views: 1530
Reputation: 26
Use these settings
SMTP SERVER : smtp.mail.yahoo.com
PORT Number : 465
Upvotes: 0
Reputation: 1216
Try adding this properties.put("mail.smtp.socketFactory.port", "465");
You are using 587 as port number that is used for TLS connection.
Upvotes: 0
Reputation: 26
Use this code:
public class SendMail {
String host, port, emailid,username, password;
Properties props = System.getProperties();
Session l_session = null;
public SendMail() {
host = "smtp.mail.yahoo.com";
port = "587";
emailid = "[email protected]";
username = "a";
password = "pwd";
emailSettings();
createSession();
sendMessage("[email protected]", "[email protected]","Test","test Mail");
}
public void emailSettings() {
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.port", port);
}
public void createSession() {
l_session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) {
//System.out.println("Inside sendMessage 2 :: >> ");
try {
MimeMessage message = new MimeMessage(l_session);
emailid = emailFromUser;
message.setFrom(new InternetAddress(this.emailid));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail));
message.setSubject(subject);
message.setContent(msg, "text/html");
Transport.send(message);
System.out.println("Message Sent");
} catch (MessagingException mex) {
mex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}//end catch block
return true;
}
}
Upvotes: 1
Reputation: 39
try adding below property:
properties.put("mail.smtp.ssl.enable","true");
Upvotes: 1