Java, unable to send mail on linux - java

Using my home computer I am able to send an email through Gmail without error, however, when I try to run the same code on my Linux box It throws:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2182)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:726)
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)
at io.paratek.updates.util.Mail.sendMail(Mail.java:34)
at io.paratek.updates.MailTest.main(MailTest.java:8)
My code
public class Mail {
public static void sendMail(String subject, String contents, String to) {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myemail", "mypasswd");
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(to));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(contents);
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
I am using Oracle JDK 181 on both systems.
I am able to successfully connect "telnet smtp.gmail.com 465" on the Linux box

After switching to TLS it works
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");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

Related

Unsupported or unrecognized SSL message javax.mail.MessagingException

Properties props = new Properties();
props.put("mail.smtp.proxy.host","host");
props.put("mail.smtp.proxy.port","587");
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.ssl.enable", "false");
// props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.fallback", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username#domain.com", "password");
}
});
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the email subject");
message.setText("This is the email body");
Transport.send(message);
System.out.println("mailsent");
} catch (MessagingException mex) {
mex.printStackTrace();
}
I have written the above code for sending email using javamail API through SMTP but I ended up with the following exception. Please see my code below. I have used debugging mode and below the code you can find the exception as well.
Error Details
DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
javax.mail.MessagingException: Exception reading response;

JavaMail Messaging Exception

I wrote a program to send gmails using JavaMail API. Following is my code:
import java.util.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import javax.mail.Session;
import javax.mail.Transport;
class emailSend{
public static void main(String[] args) {
String recipient = "receiver#gmail.com";
String sender = "sender#gmail.com";
String host = "smtp.gmail.com";
Properties props = System.getProperties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.user", sender);
props.put("mail.smtp.password", "password");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject("This is the subject");
message.setText("This the message");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com",465,"sender01#gmail.com", "password");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Mail successfully sent");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
But I'm getting following error:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2197)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
at javax.mail.Service.connect(Service.java:366)
at emailSend.main(emailSend.java:29)
I tried disabling the firewall, but it was of no use.
What should I do?
Add properties:
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.ssl.checkserveridentity", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.debug", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
You need to configure your Gmail account security to allow it to send emails from your app check the link
This is my working code
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication( from, password);
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(recepient));
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(msg, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException e) {throw new RuntimeException(e);}

I have this code to send mail from java showing error

package Controller;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
static String from = "******#gmail.com";
static String pass ="*****";
static String to = "****#gmail.com";
static String host = "smtp.gmail.com";
public static void main(String[] args) {
Properties properties = System.getProperties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setText("Ithis is a test");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Sent message successfully....");
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
This code shows the error of:
javax.mail.MessagingException: Could not convert socket to TLS;
Could anyone help me, why can't it convert socket to TLS. What can I do to resolve the error?? Please do help.
By default, Gmail does not allow "less-secure" Apps to access your email.
In order to get your code to run:
Sign into your gmail account in a browser.
Go to https://www.google.com/settings/security/lesssecureapps
Set the Access for less secure apps option to Turn on
Either the certificate has not been installed in file cacerts or you might want to set the google smtp url as "trusted". try add this code
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
on the other note. You might need to set the javax.mail.Authenticator when you try to get the session
// Build session to create MimeMessage to send
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.setProperty("mail.smtp.quitwait", "false");
Session session = Session.getDefaultInstance(props, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("name_here", "password_here");
}
});

unalbe to connect smtp.gmail.com at 465 in a loop

I am sending mail through gmail coount using port 465.
I am sending mail in loop.(reciients are present in a list)
I am able to send first mail succesfully , but as soon as second mail shoots
I get error unalbe to connect smtp.gmail.com at 465.
enter code hereprops.put("mail.smtp.host", smtp_host);
props.put("mail.smtp.port", smtp_port);
props.put("mail.smtp.user", smtp_user);
props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.debug", DEBUG);
props.put("mail.smtp.socketFactory.port", smtp_port);
props.put("mail.smtp.socketFactory.class", socket_factory));
props.put("mail.smtp.socketFactory.fallback", "false");
try {
//Obtain the default mail session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
//Construct the mail message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));
message.setSubject(subject);
message.setContent(messageText, "text/html");
message.setHeader("Content-Type" , "text/html" );
message.saveChanges();
//Use Transport to deliver the message
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
Use Port 587 for TLS/STARTTLS. And following properties only if you use TLS/STARTTLS
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");
And if you are trying to connect through SSL. Try only this -
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
...
Session session = Session.getDefaultInstance(props, null);
Change this with -
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username#gmail.com", "password");
}
});

How to solve javax.mail.AuthenticationFailedException?

I try to send mail using below code but I am getting Exception like.
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at mail.MailTest.sendMailSSL(MailTest.java:116)
Please any one help me how can I solve it.
Here is my code.Thanks
String mailBODY = "<h3>Hi this is my test Mail</h3>;
final String username = "username#gmail.com";
final String password = "password";
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 GMailAuthenticator(username, password));
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("TEST"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(test#gmail.com));
message.setSubject("Mail subject test");
message.setContent(mailBODY,"text/html; charset=utf-8");
Transport.send(message);
System.out.println("mail has been send");
} catch (MessagingException e) {
e.printStackTrace();
}
Try adding props and sending like this:
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
// ...
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
// ...
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
I got this error after I enabled 2-step verification on my google account

Categories