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

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.

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

JavaMail cannot send email

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.

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

How to send email in java GWT

I am using this code in my java GWT application
public String greetServer(String input) throws Exception {
try{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.host", "smtp.random.com");
props.setProperty("mail.user", "foo#bar.com");
props.setProperty("mail.password", "000000000");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("hello");
message.setContent("helloo sss", "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("junaidp#gmail.com"));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch(NoSuchProviderException e){
throw new Exception(e);
}
return input;
}
Error: javax.mail.MessagingException: Could not connect to SMTP host: smtp.random.com, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
if i use
props.setProperty("mail.host", "smtp.live.com");
and use my hotmail account , it gives this error
javax.mail.MessagingException: can't determine local email address
Any idea what could be the solution
thanks
I just used Simple Java Mail in a GWT project. You might want to try it. It's very simple to configure.
There are lots of example there, including one of sending using gmail's SMTP server using for example TLS:
Email email = new Email.Builder()
.from("Michel Baker", "m.baker#mbakery.com")
.to("mom", "jean.baker#hotmail.com")
.to("dad", "StevenOakly1963#hotmail.com")
.subject("My Bakery is finally open!")
.text("Mom, Dad. We did the opening ceremony of our bakery!!!")
.build();
new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email);
If you have two-factor login turned on, you need to generate an application specific password from your Google account.
Here's some Gmail settings which work for me:
//these are fed into the Properties object below:
mail.smtp.starttls.enable = true
mail.transport.protocol = smtp
mail.smtp.auth = true
and some Java:
Properties properties = ...
javax.mail.Session session = javax.mail.Session.getInstance(properties, null);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", username, password);
Error: javax.mail.MessagingException: Could not connect
to SMTP host: smtp.random.com port: 25;
nested exception is: java.net.ConnectException: Connection refused: connect
This error means that your SMTP server you provided is invalid. The code you have is correct, but smtp.random.com must not be a valid SMTP server.
I suggest you look into using Google's SMTP server that is free provided you use a valid gmail account.
Refer to this page for more information on using Gmail's STMP server: http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm
This tutorial worked for me in the past

Error in Sending mail by Java

My code is :
// File Name SendEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "toEmail#gmail.com";
// Sender's email ID needs to be mentioned
String from = "fromEmail#gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// 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(from));
// 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.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
My program was working correctly last night; I could send email from any address to any other, but now this error is occuring:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at SendEmail.main(SendEmail.java:49)
Line 49 is:
Transport.send(message);
Can anyone help me fix this error?
My operating system is : Linux,Fedora 16 -kernel: 3.3.7
Could not connect to SMTP host: localhost, port: 25;
SMTP must be not running on your system or disabled for you as user. Check with your system administrator and get it enabled for you.
To check if SMTP is working on your Linux system, try the following commands:
To simply verify Sendmail is running, try: netstat -an | grep 25
Use telnet to connect to port 25 of smtp server: telnet <yourhost-or-ipnumber> 25
a. Connecting To localhost...Could not open connection to the host, on port 25: Connect failed.
b. if you or your ip is blocked, you would see error message something like this:
220-localhost ESMTP Exim 4.63 #1 Fri, 01 Jun 2012 19:35:30 +0530
220-We do not authorize the use of this system to transport unsolicited, and/or bulk e-mail.
echo -e "quit" | nc localhost 25
localhost.localdomain [127.0.0.1] 25 (?) : Connection refused
mail at shell prompt.
and, may be more...
You should check that sendmail daemon is started and is available always.
And if you have access to any other SMTP servers, try to send mail using their SMTP host name, to check if your code snippet is working.
Example : String host = "smtp.gmail.com";
it was because my SendMail Service Stopped working.
to enable it : try this command in shell(as root).
service sendmail start
U on right way..just change Property setting and Session sentence .my java mail work Fine...
final String username = "abc#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);
}
});

Categories