JavaMail Not Working on Amazon EC2 - java

PROBLEM:
I'm trying to use JavaMail to send email from an EC2 Ubuntu 12.04 Server but it's not working.
BACKGROUND:
I intend to send an auto generated mail from a Tomcat Server to a few team members. I used JavaMail 1.4.7 for this task and I've successfully tried it on a local installation of Tomcat (on a personal laptop).
The next step was where I put this module on an EC2 Server but it started failing with the below given error:
ERROR:
DEBUG SMTP: AUTH LOGIN failed
javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 j13sm1603739pat.17 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
Code Snippet:
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.put("mail.smtps.host","smtp.gmail.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.port", 465);
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl.enable", true);
Session session = Session.getDefaultInstance(props);
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress("fromId#gmail.com");
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject("test subject");
msg.setContent("test message", "text/plain");
Transport tp = session.getTransport("smtp");
tp.connect(SMTP_HOST_NAME, "dummyId#gmail.com", "dummypassword");
tp.sendMessage(msg, addressTo);
tp.close();
ADDITIONAL INFO:
I've added permissions for all ports on the EC2 instance. Also, I've added explicit permissions for ports 465(SMTPS) and 25(SMTP).
I've cross checked the correctness of the username and password provided in the code.

In the past, spam sent from servers within EC2 has been a major problem for both Amazon and mail service providers. Initially, Amazon deliberately had insufficient DNS information to pass anti-spam checking of common email services (lack of DNS reverse lookup PTR records), then Amazon introduced low mail quotas, followed approval processes to give full access to EC2 or SES mail (provides specific technical setup to fully enable outgoing email).
Here's a link describing some of the history: http://shlomoswidler.com/2009/07/sending-email-from-ec2.html
While the error you're getting is a failed authentication at Gmail server, it may be (probably is) because Gmail identifies your sender IP address as a potential spammer, because you didn't setup Amazon reverse DNS lookup properties correctly.
Setup Amazon Reverse DNS lookup details, so that your mail is not detected as spam by Gmail. It seems this can only be done by submitting a request form to lift Email Sending quota. From Amazon EC2 IP Information FAQs:
Q: Can I configure the reverse DNS record for my Elastic IP address?
Yes, you can configure the reverse DNS record of your Elastic IP
address (submit the Request to Remove Email Sending Limitations
Form). Note that a corresponding forward DNS record pointing to
that Elastic IP address must exist before we can create the reverse
DNS record.
Here's an example of an email sent out by Amazon when an email quota is exhausted - again spelling out request & approval for DNS PTR configuration: http://www.practicalclouds.com/content/guide/sending-email-ec2-instances
Precisely follow all the instructions in the link included in you Gmail error message: http://support.google.com/mail/bin/answer.py?answer=14257. This will reset your Gmail account status, if Gmail thinks your account is spamming from your IP address.
Alternatively, you can send mail via Amazon SES, also requiring request, approval and appropriate Amazon-provided configuration. This allows mail to be sent via com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient.sendEmail. If you use this, add authorisation for your code to sendmail: Unable to send email from Amazon EC2 Server in Java.

Related

own java mail client & yandex smtp server - javax.mail.AuthenticationFailedException

evening,
i'm trying to do my own email client and then error came.
im using javax.mail library
i did try several methods for sending, most of them crash on sad places. anyway, one method i consider with potential is fine until end when message pops up:
m02 error-2: javax.mail.AuthenticationFailedException: 535 5.7.8 Error: authentication failed: Your message looks like spam. You need to use web for sending or prove you are not a robot using the following link http://ya.cc/[deleted] where [deleted] is originally few letters string.
i saw with gmail, you need to set something in settings to be able to use your own client, i expected this would be similar case but i couldn't find any settings about it in yandex mailbox settings.
another point, smtp address i have found at some forum so i presume it is possible to use own client
properties i set:
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.socketFactory.port", port_ssl);
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", port_ssl);
where
private String host = "smtp.yandex.com";
private String port_ssl = "465";
other code, im using classic way: Session for Authenticator, then Message and Transport.send. anyway, i think problem is with properties or mailbox settins? i just cant think of where.
question: how can i fix it so i can send an email with my client?
note: reason for using yandex is thanks to simple sign up since i dont have phone number and gmail requires one. if you know about other email service where own client should work & no need for phone that is also nice alternative answer
As an alternative answer, you could try the service https://protonmail.com, it should not require phone number and it can be reachable by SMTP clients using these settings https://mailsettings.co/protonmail-smtp-server-settings

Unable to connect to Office 365 SMTP Server via Jakarta Mail using OAuth 2

I am currently trying to integrate OAuth2 into an existing e-mail infrastructure of a java application. The application is using Jakarta mail, which according to their documentation supports OAuth2 (https://eclipse-ee4j.github.io/mail/OAuth2). For some reason I am struggeling to connect to the Office 365 SMTP Server, while connecting via IMAP works perfectly fine. So here is what I have been doing so far:
Create Office 365 Developer Account, populate with users and user data.
Log into the azure backend, configure an app registration, including callback url etc. and the following api rights: https://i.stack.imgur.com/lXjER.png
Use the following authentication url to create an authentication code:
https://login.microsoftonline.com/{my_tenant_id}/oauth2/v2.0/authorize?
client_id={my_client_id}&
state=state_to_check&
redirect_uri=http://localhost:5555/callback/authorization&
scope=offline_access https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send
&response_type=code
As you can see i am using the following scopes:
offline_access
https://outlook.office.com/IMAP.AccessAsUser.All
https://outlook.office.com/POP.AccessAsUser.All
https://outlook.office.com/SMTP.Send
Retrieve the authorization code and use it to get refresh and access token, which gets me the following response:
{
"token_type": "Bearer",
"scope": "https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/Mail.Read https://outlook.office.com/Mail.Read.All https://outlook.office.com/Mail.Read.Shared https://outlook.office.com/Mail.ReadBasic https://outlook.office.com/Mail.ReadWrite https://outlook.office.com/Mail.Send https://outlook.office.com/Mail.Send.All https://outlook.office.com/Mail.Send.Shared https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send https://outlook.office.com/User.Read",
"expires_in": 3599,
"ext_expires_in": 3599,
"access_token": ...,
"refresh_token": ...
}
So I would say, everything is working as expected so far regaring the OAuth 2.0 authentication process. Now, going on to use the access token to get access to the users email account, I added the following few lines in the e-mail logic of our application to enabe IMAP via OAuth:
[more props stuff here]
if (useOauth) {
props.put("mail." + protocol + ".auth", "true");
props.put("mail." + protocol + ".auth.mechanisms", "XOAUTH2");
props.put("mail." + protocol + ".auth.login.disable", "true");
props.put("mail." + protocol + ".auth.plain.disable", "true");
}
return Session.getInstance(props);
This works perfectly fine and I can connect via IMAP, read folders, messages, etc. My problem is, if I try to modify our code in a similar way for SMTP I get the following error:
Exception in thread "main" jakarta.mail.AuthenticationFailedException: 451 4.7.0 Temporary server error. Please try again later. PRX4 [AM9P191CA0011.EURP191.PROD.OUTLOOK.COM]
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:947)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:858)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:762)
at jakarta.mail.Service.connect(Service.java:342)
at jakarta.mail.Service.connect(Service.java:222)
at jakarta.mail.Service.connect(Service.java:243)
at Application.main(Application.java:52)
I had a look through the following example application that I have found on github (https://github.com/eino-makitalo/vesa-mailtest/tree/master/src/main) and the few answers on stackoverflow to see if I have missed any properties to set specifically for SMTP but I keep running into the same error, using the following configuration:
Properties props = new Properties();
props.put("mail.smtp.auth.xoauth2.disable", "false");
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host","smtp.office365.com");
props.put("mail.smtp.port", "587");
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.auth.login.disable","true");
props.put("mail.smtp.auth.plain.disable","true");
props.put("mail.debug.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
Transport transport = session.getTransport("smtp");
transport.connect( username, token);
Now I am hoping, that maybe someone has run into this issue before, and can help me out. The only questions I can find regarding the exception postet above are all related to custom exchange server setups, and how you should configure DNS setup on these servers. But I dont think this should be relevant for me, as I am not trying to connect to a custom exchange server.
UPDATE:
So I tried the same configuration with the google service, and it works for both IMAP and SMTP, so it is for sure a problem with the Microsoft Services. But I am still not sure what more I can try to make it work.
Okay, found the problem: For some reason I did not think to try and explicitly request the openId scope. Not sure why, but for some reason I had it in my head that it will be requested automatically, if you don't specify it explicitly. After requesting openId explicitly both SMTP and IMAP work.

how will I send Mail using SSMTP

props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", portnumber);
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.auth", "true");
I am using this code for sending email in java using my linux machine.
Please tell me which OS service it is using internally currently for sending mail.and
I want to change this service to ssmtp . How will I send through ssmtp.
The code you show seems to use the Java Mail API, which is included in your java project and used here as an SMTP client to send the mail. The SMTP server used here is : smtp.office365.com
However, ssmtp is not a service as you mentioned. It is another email client.
I invite you to have a deeper look into these tools to have a clearer idea before using them.

Java Mail Exception Error;

MyCode:
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;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "myemailid#gmail.com";
final String password = "myemailpassword";
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("myemailid#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("myfriedemail#ymail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"+
"\n\n No spam to my email,please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
My Error:
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
nested exception is:
java.net.UnknownHostException: smtp.gmail.com
at Mail.SendMailTLS.main(SendMailTLS.java:56)
Caused by: javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
nested exception is:
java.net.UnknownHostException: smtp.gmail.com
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1970)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
at javax.mail.Service.connect(Service.java:317)
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 Mail.SendMailTLS.main(SendMailTLS.java:51)
Caused by: java.net.UnknownHostException: smtp.gmail.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
... 7 more
Java Result: 1
I am having the Local proxy as 172.17.0.4:8080
with username as :user1, password as: user2 Solutions pls??
Try this server:
smtp.googlemail.com
If you are under a proxy I think you can't use Java Mail.
From : oracle.com
Q: How do I configure JavaMail to work through my proxy server?
A: JavaMail does not currently support accessing mail servers through a web proxy server. One of the major reasons for using a proxy server is to allow HTTP requests from within a corporate network to pass through a corporate firewall. The firewall will typically block most access to the Internet, but will allow requests from the proxy server to pass through. In addition, a mail server inside the corporate network will perform a similar function for email, accepting messages via SMTP and forwarding them to their ultimate destination on the Internet, and accepting incoming messages and sending them to the appropriate internal mail server.
Update
Q: How do I configure JavaMail to work through my proxy server? [updated!]
A: Starting with JavaMail 1.6.0, JavaMail supports accessing mail servers through a web proxy server. Set the "mail.protocol.proxy.host" and "mail.protocol.proxy.port" properties for the proxy server. Proxy server BASIC authentication is supported by setting the "mail.protocol.proxy.user" and "mail.protocol.proxy.password" properties.
In addition, if your proxy server supports the SOCKS V4 or V5 protocol (http://www.socks.nec.com/aboutsocks.html, RFC1928) and allows anonymous connections, and you're using JDK 1.5 or newer and JavaMail 1.4.5 or newer, you can configure a SOCKS proxy on a per-session, per-protocol basis by setting the "mail.smtp.socks.host" property as described in the javadocs for the com.sun.mail.smtp package. Similar properties exist for the "imap" and "pop3" protocols. Authentication for SOCKS servers is supported by the JDK by setting the "java.net.socks.username" and "java.net.socks.password" System properties (and thus apply to all SOCKS connections) as describe in the JDK Networking Properties documentation.
If you're using older versions of the JDK or JavaMail, you can tell the Java runtime to direct all TCP socket connections to the SOCKS server. See the Networking Properties guide for the latest documentation of the socksProxyHost and socksProxyPort properties. These are system-level properties, not JavaMail session properties. They can be set from the command line when the application is invoked, for example: java -DsocksProxyHost=myproxy .... This facility can be used to direct the SMTP, IMAP, and POP3 communication from JavaMail to the SOCKS proxy server. Note that setting these properties directs all TCP sockets to the SOCKS proxy, which may have negative impact on other aspects of your application.
When using older versions of JavaMail, and without such a SOCKS server, if you want to use JavaMail to access mail servers outside the firewall indirectly, you might be able to use a program such as connect to tunnel TCP connections through an HTTP proxy server. Configure JavaMail to use the connect instance as the SOCKS server.
This happens because of Gmail security...
just allow access to your gmail account via apps go to under your account:
https://www.google.com/settings/security/lesssecureapps
and it will work .. smtp.gmail.com is correct.No need to change it.
Also don't forget to check internet connection as well.
could you try with props.put("mail.smtp.host", "gmail.com"); without smtp.gmail.com
Just for development purposes, you can try to open tunnel via your proxy to smtp.gmail.com
proxytunnel -q -p proxy_host:proxy_port -d smtp.gmail.com:465 -a local_port
and use in your code
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "local_port");
It works fine when you uprade javamail to 1.6.2

Transport not work for sending smtp email in java

I am using :
transport.connect(getHost(), getPort(), getUsername(), getPassword());
to send email, but it always gives me the following exception:
class com.sun.mail.smtp.SMTPAddressFailedException: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
But actually I have provided the username and password above, and the username and password is right as I tested in thunderbird, it can send email well.
So what's my problem ? Please point me the right direction. Thanks
When creating the javax.mail.Session, be sure the given properties contain:
props.put("mail.smtp.auth", "true");
http://www.oracle.com/technetwork/java/javamail/faq/index.html#smtpauth
I think that you need to talk to the administrators of the mail server to see what is going on. You might be using the wrong port for instance. Or there might be some local policy that you need to observe ...

Categories

Resources