JavaMail cannot send email - java

i am trying to write an application in java that will send emails, i found a tutorial on youtube and tried to follow it. however it does not work for me still, here is the error i get
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: 421
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1949)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at libraryFineList.ParseRecords.main(ParseRecords.java:90)
i have no idea, what's wrong, anything i found on google did not help,
here is the code
public static void main(String[] args) throws IOException, MessagingException {
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");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("user#gmail.com", "pass");
}
}
);
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("user#gmail.com"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("ycharnetskaya#gmail.com", "Mr. User"));
message.setSubject("Your Example.com account has been activated");
message.setText("Worked");
Transport.send(message);
}catch(Exception e){
e.printStackTrace();
}

The tutorial you followed is full of errors. Start by fixing these common mistakes. Then follow these instructions for connecting to Gmail. If you're still having problems, you'll find lots more help in the JavaMail FAQ. There's also many sample programs available.

It's strange that your error message is complaining about a problem connecting to localhost:25 when your properties file is clearly suggesting that you should use smtp.gmail.com.
I don't suppose there's anything dodgy going on with your hosts file that's redirecting smtp.gmail.com back to 127.0.0.1 or something, is there? What happens if you ping smtp.gmail.com from the command line?
Apart from that, I'd suggest checking you're using the latest version of Java Mail.

Related

Not able to send Mail in Java through SMTP server

When I try to send mail in java from my personal email like (sp#gmail.com) it is sent successfully.
But when I am using my company email(sp#example.com) it throws Authentication failed exception. I am using TLS authentication and it is successfully connected to host.
When I am manually login on my email it will always ask for Two Step
verification. Even if I have disabled my two step verification and have also done the change to make it less secure, it still asks for two step verification as it is showing this message after putting my username and password:
2-Step Verification
Based on your organization's policy, you need to turn on 2-step verification. Contact your administrator to learn more.
Enter one of your 8-digit backup codes
In this situation what should I do? As this is my first task in this company, I would be so happy if you could help me. How I can solve this problem?
My code :
String to = "abc#example.com";
String user = "sp#example.com";
String pass = "1234";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user,pass);
}
});
session.setDebug(true);
try
{
/* Create an instance of MimeMessage,
it accept MIME types and headers
*/
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
/* Transport class is used to deliver the message to the recipients */
Transport.send(message);
}
catch(Exception e)
{
e.printStackTrace();
}
Error message:
535 5.7.3 Authentication unsuccessful javax.mail.AuthenticationFailedException at javax.mail.Service.connect(Service.java:319) at javax.mail.Service.connect(Service.java:169) at javax.mail.Service.connect(Service.java:118) at javax.mail.Transport.send0(Transport.java:188) at javax.mail.Transport.send(Transport.java:118) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPo‌​olExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.r‌​un(TaskThread.java:6‌​1) at java.lang.Thread.run(Thread.java:748)
SMTP configuration:
configuration:props.put("mail.smtp.host", "smtp.oceaneering.com"); props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
First is need to turn on 2-Step Verification .(after that google script to give new option for creation app passwords) https://support.google.com/accounts/answer/185839?
Next you must to create the app passwords https://support.google.com/accounts/answer/185833?hl=en
Last step to replace this line in your code
String pass = "1234"; - with app specific password, (it must generate on step 2) Some like String pass = "djd5n7hsd";
And For future reader in may be useful to check network and firewall setting on your system and mail setting your ISP. To check if packet can to reach gmail server.
Try telnet command : telnet smtp.gmail.com 587

Reading Mails from Outlook Javamail

Am trying to read the emails from the outlook using javamail
herez the code snippet.
try {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imaps.starttls.enable", "true");
props.setProperty("mail.imaps.host", "outlook.office365.com");
props.setProperty("mail.imaps.port", "143");
Session mailSession = Session.getInstance(props);
mailSession.setDebug(true);
Store mailStore = mailSession.getStore("imaps");
mailStore.connect("outlook.office365.com", "<username>", "<password>");
} catch (Exception ex){
ex.printStackTrace();
}
Exception
javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:727)
Any pointers are appreciated.
Your code is very confused. Keep it simple:
Properties props = new Properties();
props.setProperty("mail.imap.ssl.enable", "true");
Session mailSession = Session.getInstance(props);
mailSession.setDebug(true);
Store mailStore = mailSession.getStore("imap");
mailStore.connect("outlook.office365.com", "<username>", "<password>");
Use the "imap" protocol, but tell JavaMail to enable SSL. Don't worry about ports, JavaMail knows what to do. More detail in the JavaMail FAQ.
Port 143 (what you're using) is for plain-text IMAP (hence the error message plaintext connection?). IMAPS uses port 993, so try that.

Unknown SMTP host: outlook.office365.com;

I have written code in java to send mail. The issue I am facing is UnknownHostException. I am using SMTP Host as outlook.office.com as the email account from where i want to send the mail is corporate and accessed using outlook. Here is the code.
Properties props=new Properties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host","outlook.office365.com");
props.put("mail.smtp.port","587");
Session session=Session.getInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(sender, password);
}
});
The session object is not null as I have put a check on that. If session is getting created then what is the reason I am facing this exception?
change
props.put("mail.smtp.host","outlook.office365.com");
to
props.put("mail.smtp.host","smtp.office365.com");
or
props.put("mail.smtp.host", "m.outlook.com");
for more info, visit this and this site.

tried tjavax.mail.MessagingException: Unknown SMTP host: "smtp.office365.com";

I am getting this error when I try to send an email from Java web app hosted on AWS.
I have already tried to change SMTP server to smtp.live.com and also smtp-mail.outlook.com, none of those work.
Can it be some AWS config? It runs on Ubuntu. (There are no outbound restrictions on the server itself, there might be some on Java server though)
Code for sending the email:
final String username = smtpUsername;
final String password = smtpPwd;
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
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(smtpUsername));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(sendTo));
message.setSubject(subject);
message.setContent(content);
Transport.send(message);
System.out.println("Sent");
} catch (MessagingException e) {
e.printStackTrace();
}
The most interesting part of this is, that it works from my local computer...(but only when I disable Avast)
I have tried to execute telnet smtp.office365.com 587 and the result was:
Trying 132.245.195.162...
Connected to outlook-emeawest2.office365.com.
Escape character is '^]'.
220 HE1PR08CA0021.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 26 Aug 2015 14:32:11 +0000
I have tried to set up the AWS SMTP (SES) and I am getting the same error, even after I followed the documentation, I also added the email from which I was sending and to which I was sending to the verified emails (whitelist):
javax.mail.MessagingException: Unknown SMTP host: "email-smtp.eu-west-1.amazonaws.com";
try doing a dig from a bash shell from the ubuntu machine
dig email-smtp.eu-west-1.amazonaws.com
what are you getting. from what I can see, it might be a DNS problem.

Sending Email using java timeout exception

I am trying to send email to gmail using java. I am using this code.
final String username = "xyz#gmail.com";
final String 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 javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("xyz#gmail.com"));
message.setRecipient(Message.RecipientType.TO,newInternetAddress("abcdef#gmail.com"));
message.setSubject("First email to using java");
message.setContent("<h:body style =background-color:white> This is a test mail sent using java" + "</body>","text/html; charset=utf-8");
Transport.send(message);
System.out.println("Message Sent");
But When i run the above code it shows follwoing error:
Exception in thread "main" com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
I have an Internet connection which uses proxy server and requires authentication. Is this error because of Proxy or there is some problem in my code. Please tell me how to resolve it.
JavaMail can't use a web proxy server directly, although it can use a SOCKS proxy server. If you only have a web proxy server, programs like Corkscrew can help.
You can setup SOCKS proxy server in JavaMail with like this:
Properties p = System.getProperties();
p.setProperty("proxySet","true");
p.setProperty("socksProxyHost","192.168.1.1");
p.setProperty("socksProxyPort","1234");
Yes, it's because of your proxy server.
How do I configure JavaMail to work through my proxy server?
the above link expired: check this link
http://www.oracle.com/technetwork/java/faq-135477.html#proxy

Categories