4

I try to send SMTP email in java, but I have an error like this and I don't get a mail. I turn off all firewall and anti-virus.

The error:

javax.mail.AuthenticationFailedException: 534-5.7.14<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuIW

534-5.7.14 tAxHxbq4WR-vUuV1uOFAvx8NhInfxYTyNHi_8sJ80lX5lBdBla2ROiSKoysMNcFoQ6sGe
534-5.7.14 DUh173tDMolJ64W-Rahx1fhVF_08AvWrphibgQXiyyz5U1FNMMb-eGGJlUIbjyvBgQuZY6

534-5.7.14 tnykIXdVn__mg87aOmtxoss-EiFYeKdvuiBbt5eb9t_NOc97h-PkXOco-9FcYW69Iz9CTu

534-5.7.14 rfyhlo24k9oqIiWtcJwv85oUCO2g> Please log in via your web browser and

534-5.7.14 then try again.

534-5.7.14 Learn more at

534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 pd8sm1306363pdb.93 - gsmtp   

Here is my code:

private void btn_mailActionPerformed(java.awt.event.ActionEvent evt) {                                         

    String to = "receive.address@gmail.com";

    String from = "send.address@gmail.com";
    final String username = "send.address";
    final String password = "sendpassword";
    String host = "smtp.gmail.com";
    Properties pro = new Properties();
    pro.put("mail.smtp.host",host);
    pro.put("mail.smtp.socketFactory.port","465");
     pro.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    pro.put("mail.smtp.auth","true");
     pro.put("mail.smtp.port","465");
    Session session = Session.getInstance(pro,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication()
                {
                    return new PasswordAuthentication(username,password);
                }
            }

            );
    try 
    {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject("test mail");
        message.setText("Hello how are you?");
        Transport.send(message);
        JOptionPane.showMessageDialog(null,"Send");

    } 
    catch (Exception e) {
        JOptionPane.showMessageDialog(null,e.toString());
        System.out.println(e.toString());
    }
siegi
  • 5,646
  • 2
  • 30
  • 42
ThanhLam112358
  • 878
  • 1
  • 20
  • 51
  • 5
    And have you looked at https://support.google.com/mail/bin/answer.py?answer=78754 to find out why its failing? – Tom Feb 16 '15 at 08:30
  • Note that your code is full of the [most common JavaMail mistakes](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes). I don't think that's related to your problem, but you should still clean it up. – Bill Shannon Feb 16 '15 at 19:19

2 Answers2

14

Login to email from browser and go this page. You will see this;

enter image description here

Make sure to click "Turn on" and try your code again.

Community
  • 1
  • 1
Semih Eker
  • 2,389
  • 1
  • 20
  • 29
2

I had the same issue. After much testing with Gmail, I discovered the issue is that Gmail requires an OAuth sign-in, and not just a password. The solution for this is to use the Gmail API. However, this is a very complicated solution that I won't go into too much detail about. If you are interested in this, read the first answer here.

If you want a simple solution, however, what I did is simply switch to a Yahoo account. Because Yahoo doesn't use the same encryption, it works perfectly. Note: Don't forget to change the SMTP server to 'smtp.mail.yahoo.com', and the port to '25'.

If you want to set it up from scratch, simply follow this tutorial to download the JavaMail API and Java Activation Framework.

Then you could just copy-and-paste my code, change the top variables, and everything should work! If I missed anything, please let me know! Thanks!

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Mailer {

    public static void main(String[] args) {
        final String username = "your-email@yahoo.com";
        final String password = "your-password";
        final String recipient = "email-recipient";
        final String subject = "message-subject";
        final String emailmessage = "message";

        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.mail.yahoo.com");
        props.put("mail.smtp.port", "25");

        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(username));
            message.setRecipients(Message.RecipientType.TO,    InternetAddress.parse(recipient));
            message.setSubject(subject);
            message.setText(emailmessage);

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }

}
Community
  • 1
  • 1
user4775991
  • 127
  • 2
  • 14
  • What you're saying may be correct for specifically Java, but I'm not sure, because on other platforms, Google doesn't require OAuth for using their SMTP. – Amar Syla Dec 14 '15 at 03:02
  • Well, if you wish to use Google, supposedly you can [turn on access for less secure apps](https://www.google.com/settings/security/lesssecureapps), but I couldn't get it to work for me. That is why I used Yahoo instead. Anyway, I haven't heard of OAuth not being required for certain things. Maybe it's just with Java – user4775991 Jan 17 '16 at 17:36
  • @user4775991 I doubt if the above code snippet works now with Yahoo's double sign on authentication – raikumardipak Dec 13 '17 at 20:57