javax.mail unable to send mail to hotmail - java

I'm trying to send mails using javax.mail from an hotmail account.
Until now I've tested my code also using gmail account and everything works fine, but
with hotmail nothing works in particular, I receive a MailConnectException.
This is the code that I use in order to send the mail:
props.put("mail.starttls.enable", true);
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", true);
props.put("mail.smtp.auth", true);
As you can see I've created a PropertiesFactory in order to create a specific instance of the Properties object for each specific mail host that I use. In the code there is the factory for the HotmailProperties structure.
Starting a debugging session I've checked that useAuth option and isSSL are both equal to false.
What do I change in the properties configuration? Maybe there are some other errors in the code?
This is the debugging result of my program:
http://ideone.com/SDu4JG
SOLVED
I've solved my problem with the hotmail server.
Looking to this page: http://windows.microsoft.com/en-us/windows/outlook/send-receive-from-app
I've understood that hotmail, differently from the other mail servers like gmail and yahoo, considers the username as the complete email address. So when I do the login I always receive an error.
Finally it works. Thank you to all.

From your debug output:
MAIL FROM:<shadowtemplate#hotmail.com>
530 5.7.0 Must issue a STARTTLS command first
DEBUG SMTP: got response code 530, with response: 530 5.7.0 Must issue a STARTTLS command first
Status code 530 means that the client was not authenticated. (I googled "SMTP error status code 530" to get this)
Your message above indicates that TLS (transport layer security: encryption) is required
Apparently, Hotmail requires that you send mail over smtps and the default port is 587.

It's Working for me . Surely it will work for u
:--
with port 587:--
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.setProperty("mail.smtp.quitwait", "false");
props.put("mail.smtp.starttls.enable", "true");
Session smtpSession = Session.getInstance(props1, authenticator);
smtpSession.setDebug(true);

You named the properties mail.smtps instead of mail.smtp
There is already a question which is solved, have a look: Java Mail over TLS

Related

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.

Smtp connection outlook with java

I have created Session successfully.
I am able to get IMAP urlName but when trying to make connection of Store for outlook, I get an error.
What hostname will come in parameter for outlook?
You could use the below code to connect outlook:
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");smtp.office365.com
props.put("mail.smtp.host", "smtp.office365.com");//This is hostname
props.put("mail.smtp.port", "587"); //change this port
Before connection, You should check few things:
SMTP server should be appropriate.
Some organisations has blocked 587 port number, please check with your IT team if
that port is open to communicate with given SMTP server.
For more information, you could refer to this link:
java outlook send mail

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 api and hmailserver RECEIVED: 530 5.7.0 Must issue a STARTTLS command first

I am using Java mail api and hmailserver to send outbound emails from my localhost. I am using the SMTP relayer outbound.att.net with my username and password for authentication. When I run my program and check the logs I have the line:
"RECEIVED: 530 5.7.0 Must issue a STARTTLS command first"
I am confused considering I set up my mail properties to include the STARTTLS command:
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Does anyone have an idea of what I may be doing incorrectly?
Check that you haven't made any of these common mistakes.
Based on the log output, it appears that you might not be using the JavaMail reference implementation. Perhaps you're using the GNU version of JavaMail? It may not support some of these features that are specific to the reference implementation.
If none of that solves your problem, include more of the debug output.

using java how to send a mail without authentication

I set following property to send a mail to gmail without authenticating but i am not able to send. please help me
Properties props = new Properties();
props.put("mail.smtp.host", SMTPServer);
//props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTPPort);
1) port 465 is ssl port, therefore you need create SSL connection (you comment this string)
//props.put("mail.smtp.ssl.enable", "true");
2) I'm sure that smtp.gmail.com doesn't allow connection without authentification, because everybody could send mail from any user....
edit:
And check telnet to smtp.gmail.com 465
look at the article for sending mail
http://www.journaldev.com/2532/java-program-to-send-email-using-smtp-gmail-tls-ssl-attachment-image-example

Categories

Resources