how will I send Mail using SSMTP - java

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.

Related

OAuth 2 Flow for Office365 SMTP and JavaMail

I have a Web Application in which one of the module was used for sending emails from the application to the clients. It was using basic authentication for sending emails but now there is a requirement for implementing OAuth2 Authentication in the process. I am new to this mailing and Authentication stuff. I would be really grateful if someone could explain me where to start and what process should i folllow.
It is a legacy project and we are not using Spring. Instead, we are using Struts 1.2 with Java 7.
Current JavaMail properties are :
mailProperties.put("mail.smtp.host", host);
mailProperties.put("mail.smtp.auth", "true");
mailProperties.put("mail.smtp.starttls.enable", "true");
mailProperties.put("mail.smtp.port", "587");
mailProperties.put("mail.smtp.ssl.protocols", "TLSv1.2");

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.

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.

javax.mail unable to send mail to hotmail

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

Categories

Resources