Spring java mail : send mail impossible with yahoo - java

I am trying to user spring java mail with theses properties :
mail:
host: smtp.mail.yahoo.com
port: 587
username: xxx
password: xxx
protocol: smtp
properties.mail.smtp:
auth: true
starttls.enable: true
Code :
#Inject
private JavaMailSenderImpl javaMailSender;
...
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
message.setTo(to);
message.setFrom(fromEmail);
message.setSubject(subject);
message.setText(content, isHtml);
javaMailSender.send(mimeMessage);
But I am getting this error when I send the mail :
E-mail could not be sent to user 'xxx#yahoo.fr', exception is:
Authentication failed; nested exception is
javax.mail.AuthenticationFailedException: 535 5.7.0 (#MBR1212)
Incorrect username or password.
I am sure my login/pwd are correct.
Are my properties incorrect?

Check your username / password, are you parsing it with plain text,string variable or char array, does it contains special character that needed to be escaped?
Make sure there is no empty space, extra space/break.
Your code and config are too narrow ,we can't help much to be honest.
Are you able to send email using gmail account or other email?
If you do so, there are problems in either yahoo config,such as port or
the username password are indeed really incorrect.

This is most likely Yahoo's new sign in method restrictions.
Try this:
https://help.yahoo.com/kb/mail-for-desktop/turn-account-key-sln25781.html
or this:
https://login.yahoo.com/account/security#less-secure-apps

Yes I am also using Yahoo mail and connecting with Java mail, you need to set up a third party access key, and then you will be able to connect.

Related

How to send emails from different accounts through JavaMailSender (w/o user login)

I have a Contact Us form on my webpage where a user can enter their name, email address and a message to send me an email. However I do not have the user logged into their mailing account. Is it possible to generate and send an e-mail through their mailing account without logging in?
On the back-end, when I set the value for the From Email, it is ignored and the email address used for sending the email is redacted#gmail.com
MailSender.java
#Autowired
private JavaMailSender mailSender;
public void sendEmail(String fromEmail, String body, String subject) throws MessagingException{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
message.setFrom(fromEmail);
message.setTo("redacted#gmail.com");
message.setText(body);
message.setSubject(subject);
message.setSentDate(new Date());
mailSender.send(mimeMessage);
System.out.println("Mail sent successfully");
}
application.yaml
spring:
mail:
host: "smtp.gmail.com"
port: 587
username: redacted#gmail.com
password: redacted
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
The from part isn't your actual username or mail address. It's just the "display name" for the recipient.
Depending on the mail provider the from part must be identical to your username. In other cases it's fine to send mails with an arbitrary from. But most providers will reject this, because it can be a security risk for the receiver if they for example receive a mail from you, but their mail application shows support#microsoft.com. ;)
The from part also can be used to add a real name in addition to the mail address which requires a special format (something like My Name<redacted#gmail.com>). In Java this can be done like this:
message.setFrom(new InternetAddress("redacted#gmail.com", "My Name"));
But this again is highly dependent on your mail provider. Some will set this automatically for you. Some will require that it's identical to your user account data. And some will let you send whatever you want.
Btw: The same applies to sentDate. I don't think that Google allows you to provide a custom date, instead they will override it.

Send email OAUTH2 SMTP Outlook Java

I am using the scopes: mail.send, mail.readwrite, mail.read, offline_access, openid, email, and profile (although I am fairly confident I do not need all of these -> goal is to read inbox and send emails, while also getting email and name if they exist).
I am then connecting to SMTP server with the following code:
OAuth2Authenticator.connectToSmtp("smtp-mail.outlook.com",
587,
user.getOutlookUid(),
accessToken,
true);
The code that actually connects to the server is here:
public static SMTPTransport connectToSmtp(String host, int port, String userEmail, String oauthToken, boolean debug)
throws Exception {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put("mail.smtp.sasl.mechanisms.oauth2.oauthToken", oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);
URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
String password = "";
transport.connect(host, port, userEmail, password);
return transport;
}
Okay, now I can get to the most frustrating part... I have used the "connectToSMTP" method to connect to Gmail and it worked perfectly.
OAuth2Authenticator.connectToSmtp("smtp.gmail.com",
587,
user.getGoogleUid(),
accessToken,
true);
So ultimately my question is "what am I doing wrong?" or "what can I update to be able to send emails through Outlook"? I have seen that Outlook has a REST API, but that is plan B. Is there something different about Outlook vs Gmail?
Some things I have considered:
Scope did not request enough access (so I probably am asking for too much now)
access_token was stored incorrectly or encoded in some way (tried decoding it from base_64 which provided nothing). I am able to use my refresh_token to update the access_token so that tells me I am probably storing them correctly.
I tried passing null for the password. Also passed in the actual password and that WORKED, but I have the access_token and refresh_token so I shouldn't need to ask for their explicit password. Also this would be dangerous and sketchy to ask of users.
I tried manually connecting to the smtp server using "openssl s_client -crlf -starttls smtp -connect smtp-mail.outlook.com:587", but it seemed to think my access_token was wrong "535 5.0.0 OAuth failed: OAuth authentication failed due to Invalid token. Code -2147184118" That number when taken two's complement and converted to hex is 0x8004920a. Helped in searches but was to no avail.
I have done a lot of searching for this and will continue now to post this everywhere. A lot of resources for it working with Gmail, but as previously stated I already have it working for Gmail. Something seems different for Outlook. Also I have encountered lots of posts regarding email forwarding on an email client... I am semi-creating an email client so going through outlook.com settings doesn't help me.
Another concern that a buddy of mine had was that my access token was really long, contributing to what the manual smtp server claimed. It is 1188 characters long. It's something like 'EwB4Aul3BAAUo4xeBIbHjhBxWOFekj4Xy2...x9stHxi2K/VFggE=' (obviously I hid most of the characters).
Preemptive THANK YOU for anyone who offers advice or finds my issue. Especially why I can pass in the email password and that fails, but using the oauth access_token fails.
Try using "pop3://user:password#host:port/INBOX". to retrieve email from the inbox ,
more information can be found out https://javamail.java.net/docs/api/com/sun/mail/pop3/package-summary.html
hope this helps : https://technet.microsoft.com/en-ca/dn44016. Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", username, oauth2_access_token);

AppEngine Email : Unauthorized Sender

When I try to send mail I get a "Unauthorized sender" exception
javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Illegal Arguments (java.lang.IllegalArgumentException: Unauthorized Sender: Unauthorized sender))
at javax.mail.Transport.send(Transport.java:163)
at javax.mail.Transport.send(Transport.java:48)
My code to send mail is very simple:
Session session = Session.getDefaultInstance(new Properties(), null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("admingae#tecurti.com", "Adming"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("name#gmail.com", "Felipe"));
msg.setSubject("Assunto", "UTF-8");
msg.setText("texto corpo");
Transport.send(msg);
I´ve already give to admingae#tecurti.com "owner" permission on admin console.
Here is my App Engine Console permission
If anyone could help me I appreciated
thanks
Solutions
Thanks to Andrei Volgin I will register the solution
Admin Console Correct Register
In console go to App Engine > Settings > Application Settings. Add this email address to the list of authorized senders.
Today this is little different and requires more setup for security reasons.
The zero configuration way, is sending email from an email address with this format:
[anything]#[project_id].appspotmail.com
FYI: https://cloud.google.com/appengine/docs/standard/java/mail/#who_can_send_mail

Java Mail API password Verification

I am using Java Mail API to send an email. So I am getting email and password from user, I just want to check the password when they are enter their password, but I don't get any ideas to verify the password in Java Mail API. Can anyone help?
Question 2:
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("xxx#gmail.com","*");
}
What is use of the above the above method
To validate the password you have to use it. You don't have to send a message, but you do have to connect to the server and authenticate. Use the Transport.connect method. You can close the connection right away if you're not going to send the message soon, or you can save the Transport object for use later when you send the message and close it when you're done. Note that if you're waiting for user input between the Transport.connect (to validate the password) and the Transport.sendMessage (to send the message), the server may time out the connection and you'll have to reconnect before sending.

Send Email from one address server to another server using Java Mail API

I have an online form that allows users to email a complaint to the company. To test it I have used gmail smtp as my host. I have no problem receiving the message to the designated email account when the sender is also a gmail but I want the "From" to not be limited to just gmail accounts. It appears that smtp is only good for sending emails from the same server?
Example: My form works great if the from is abc#gmail.com and the company email is company#gmail.com.
However if xyz#yahoo.com is entered for the sender, the receiver company#gmail.com never gets it.
Any help would be greatly appreciated. I can provide my code as well if that is needed.
Your problem is a common security restriction when using SMTP. Outgoing SMTP email can only contain a "mail from" address belonging to the sender. If you break this rule, your email may be considered SPAM.
The following will allow your recipient to reply to an alternate address.
Properties properties = new Properties();
props.put("mail.smtp.from", "abc#gmail.com");
Session session = Session.getDefaultInstance(props, null);
MimeMessage m = new MimeMessage(session);
m.addFrom(InternetAddress.parse("xyz#yahoo.com"));
m.setReplyTo(InternetAddress.parse("xyz#yahoo.com"));
See also
http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
http://www.openspf.org/Best_Practices/Webgenerated
Well you will have to own the other email as well as set it to work with gmail,
Check here for more details.
It's probably better to send the message to your company's mail server using the identity of the user who owns the application on the server, and include the information that the customer provides in the online form as data in the message you send. The message won't look like it came from the customer, but then it really didn't come from the customer since it wasn't sent using the customer's mail server.

Categories

Resources