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

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

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.

How to send message JavaMail on GMAIL? [duplicate]

I am getting this error when I try to send mail using the JavaMail API:
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted
How can I fix this?
Sorry for coming late to Party.These could be the problem in your task if you are using Gmail Server.
Two Step Verification should be turned off.
Allow Less Secure App(should be turned on).
Please Check Your UserName and Password.
Check the code(which was my Problem), Above three You can change form google help center and by Yourself last one My Experience I can Share with You.
You need to Authenticate the Mail server before Communicating the message because it is Challenge Response System By which You are Communicating through the mail.I am sharing code snippets file you can refer not able to format my code by ctrl+K.
This worked for me:
Login to the gmail account you are sending mail from
Go to Manage your Google Account -> Security -> Less secure app access -> Turn on access (not recommended)
or
Access the URL:
https://www.google.com/settings/security/lesssecureapps
Turn "Allow less secure apps: OFF" to "Allow less secure apps: ON"
Update:
Google stopped supporting "Less Secure Apps" as of May 30th, 2022.
One of the alternatives to solve this problem is to use 2-Step Verification and generate app password:
Google Account -> Security -> 2-Step Verification -> Input password as asked -> Turn ON (you could use SMS to get Gmail code to activate 2-Step Verification)
Google Account -> Security -> App password -> Input password as asked -> Select the app and device... -> e.g. Other(Custom name) -> Input app name e.g. MyApp -> Generate
Copy a 16-character password
Use a 16-character password with Gmail username in your application
Google support link here.
Update since June 2022:
Google closed the "Less secure app access",
but opened another option called "App Passwords":
"When you use 2-Step Verification, some less secure apps or devices may be blocked from accessing your Google Account. App Passwords are a way to let the blocked app or device access your Google Account." (see docs).
Follow this answer to set up App Passwords
Hint: when using Jenkins Email Extension Plugin, emailext (which does not permit tokens like Google's App Passwords, still requiring logins), just use App Password as the password, and supply any string as login, e.g. your Gmail address login.
OLD ANSWER , not relevant anymore
Step 1: Log into your gmail account
Step 2: Click Settings
Step 3: Click the Accounts and Import Tab > Other Google Account Settings
Step 4: Click Security
Scroll to the bottom of the page Under Less secure app access, click on Turn on access
Step 5: Set Allow less secure apps to ON
First of all make sure that all properties should be defined as follows:-
mail.smtp.host=smtp.gmail.com,
mail.smtp.port=25,
mail.smtp.auth=true
mail.smtp.starttls.enable=true
Now,make sure that two step verification is off
Allow less secure app (ON) follow this link :-
https://myaccount.google.com/lesssecureapps
Also, check your username and password is correct or not.
It works for me, you must configure your Gmail account with the below steps:
In the security section:
You need to Change "Allow less secure apps: OFF" to "Allow less secure apps: ON"
Log on gmail account, in Account ->
click Security -> turn off 2-step verification and turn on "Less secure app access"
May be because of things above, hope help you
I have the same error but when I run the app from the terminal, it goes away. My email configuration is provided:
spring.mail.host=smtp.googlemail.com
spring.mail.username=weddingcard9999#gmail.com
spring.mail.password=Weddingcard.1
spring.mail.port=587
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.required=true
Follow the steps:
Disable antivirus
Allow Less Secure App On
Two Step Verification should be turned off.
1.Allow Less Secure App(should be turned on).
2.Check Gmail Username and Password..
public static void main(String[] args) {
final String username = "YourMailId";
final String password = "password";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true"); //TLS
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("Tarunsunny143#gmail.com"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("balachandralucky2#gmail.com, to_username_b#yahoo.com")
);
message.setSubject("Testing Gmail TLS");
message.setText("Dear Mail Crawler,"
+ "\n\n Please do not spam my email!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Please turn on by clicking the toggle button inside the google security panel.
Then it will allow less secured app also.
Change the settings in Google like this:
You probably got this error because the username and password of the from mail id is not matching. Please recheck your password and mail-id (username). It could be a typo.
In some cases, Gmail prevents logging in through external applications or programs which are not authorised.
Also login to your gmail account to check if gmail has prevented logging in to your account via your Java Mail API program.
If nothing works, you could try some other SMTP server (like yahoo, yandex).
For me works change the property "mail.host" recomended for #Heisenberg.
At documentation says to use "smtp.gmail.com" but works only I use "smtp.googlemail.com".
PS: My Allow Less Secure App configuration was already On
I had this issue while setting up my Jenkins and I resolved it without turning off my Two-Step Verification on my email account security.
This error in my case was because Two-Step Verification was enabled in my email, so I needed to add an app password to generate a password that will give Jenkins access to my mail in order to send emails on my behalf.
STEP 1: Click settings on your email account, select the "Account and Import" tab and in the change account settings, select "Other Google accounts settings"
STEP 2: In the settings screen that opens, select "Security" on the left-hand side, and under Signing in to Google, click "App passwords"
STEP 3: In the add passwords section, click the select app, and select "Mail"
STEP 4: Still in the add passwords, click on the select device and choose "Other (Custome name)".
And in the field that will show, enter a custom name for the app you are creating a password for and click the generate button.
FINALLY: Copy the generated password and use it as your email password while setting up email sending on Jenkins or any other app you are setting it up on.
I just hope this helps someone, give me a vote if it does, Thanks...
Google stopped supporting "Less Secure Apps" a time ago.
I used 2-Step Verification to generate app password and it worked for me:
Go to your Google Account -> Security -> 2-Step Verification
Then follow the steps to turn on
After that, go to your Google Account -> Security -> App passwords -> Select the app and device... -> e.g. Other(Custom name) -> Input app name e.g. Linux Computer -> Generate
Then copy the 16 character password shown in the screen and place it in the your applications config:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=myappemailhere#gmail.com
spring.mail.password=mygeneratedpasswordhere
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
A couple of answers have mentioned the Less Secure App setting which google stopped supporting in May 2022. Just wanted to clarify that the May 2022 date concerns gmail accounts. If you use Google Workspace for your email, you can still access and enable the Less Secure App Access setting then proceed to use smtp.gmail.com via port 465 or 587.
The help text accompanying the setting says "Important: This deadline does not apply to Google Workspace or Google Cloud Identity customers. The enforcement date for these customers will be announced on the Workspace blog at a later date".

Send mail to #hotmail.com/#live.com with JavaMail

I develop a application on android to send email. I want to use account MSN to send mail but it not send and error code on debug as below:
My configure on property are:
systemProperty.put("mail.smtp.starttls.enable","true");
systemProperty.put("mail.smtp.auth", "true");
systemProperty.setProperty("mail.host", "smtp.live.com");
systemProperty.put("mail.smtp.port", "587");
The password and email address are correct and i ever test with other host are work except MSN.
It looks like JavaMail isn't able to figure out your host name correctly, although I don't know why it would think "????" is your host name. Set the mail.smtp.localhost property to the correct host name for your machine. See the javadocs for the com.sun.mail.smtp package for details.
There seems nothing wrong with your configuration. 501 5.5.4 Invalid Address can be occuring because of the possible reasons below
The To email address is wrong in format (like check if it violates the possible combinations of email addresses. Eg., "My Name" - try simplifying it to myname#live.com and try
It might be bouncing email address
Check the library you use for sending this, how and what it allows

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 ...

Java Mail Exception

I have currently face a problem when I try to send an email using JavaMail API. The exception I get from my application console is :
"javax.mail.MessagingException: 550 Access denied - Invalid HELO name (See RFC2821 4.1.1.1)"
by the way, I have already set my mail.smtp.auth property to true as : props.put("mail.smtp.auth", "true"), but it still fail, does anyone has idea? or face similar problem before ?
A 550 error is often returned by the SMTP server when the sending hostname cannot be inverse resolved to the originating IP address. This allows mail servers a bit of authentication that the sending client is who it says it is. Unfortunately, many test clients - especially systems behind a NAT device - will have originating IP addresses that don't map to any name.
For example, the machine I am typing this on has an unroutable IP address of 192.168.1.103 and my hostname could be so.example.myhouse which works fine because my router pretends that packets from my desk come from (e.g.) 69.59.196.211 which is my WAN address. However, if you use a props.put("mail.from", "me#so.example.myhouse") the SMTP server may try a DNS lookup and obviously fail for my fictional hostname (that is, one that the global DNS doesn't know of).
Even if I used the DNS name which maps to 69.59.196.211 (e.g. stackoverflow.com) the SMTP server may do a reverse DNS lookup to check that 211.196.59.69.in-addr.arpa maps to stackoverflow.com. If that fails, the SMTP server may consider you a spoofer and return a 550.
Finally, your sending client or every host it its IP address block could be blacklisted by the SMTP server for reasons that you have no control over.
Without more context than you probably want to post (names and addresses of the guilty client and server) I can't be sure that it is an SMTP/DNS problem unrelated to Java so you'll have to check those bits yourself. You can skip the Java altogether and telnet smtp-servername 25 and talk to the server yourself. You'll find RFC 2821 helpful should you try.
replace
props.put("mail.smtp.auth", true);
to
props.put("mail.smtp.auth", "true");
and it works :)

Categories

Resources