无法使用java代码从gmail发送邮件

时间:2016-03-02 03:16:44

标签: java email smtp gmail javamail

package abc;

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

public class SendMail {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String to="to@gmail.com";
        String from="from@gmail.com";
        final String username="from";
        final String password="password";
        Properties properties=new Properties();
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.port", "587");

        Session session=Session.getInstance(properties,
                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("Hey I wrote a java code to send mail. Thanks ");
            Transport.send(message);
            System.out.println("Sent Mail :)");
        }       
        catch(MessagingException e){
            e.printStackTrace();
        }

    }

}

当我尝试运行上面的代码::

时,我得到了以下错误信息
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvHc
534-5.7.14 gCjfbim5WYCDTQee6sZlZ2d31nncueOizOcz9NieexN5nSlGr0c49lSZ43qc4RQPQvpWLH
534-5.7.14 qCUQUecjIR7qxdYJ5R_WgLxkzD9u4Ds3EEG7ceSMyTZg0dpSGJb-zl5C82YDTdLOYTX5Pl
534-5.7.14 tmEfWrmktFCdAxUjtDiPNruDLqhPSIZ9dd187tQjBtOw2X8zx7MUcysN9BRawwDmbXT6mJ
534-5.7.14 cLn_sJS5UBuqommn0uJK7W1tPZzU> 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/answer/78754 qy7sm48619995pab.34 - gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at abc.SendMail.main(SendMail.java:36)

Gmail不允许使用此代码。我甚至从gmail收到了一封关于可疑登录的邮件,并查看了我运行此代码的位置。解决方案是什么?

1 个答案:

答案 0 :(得分:1)

AuthenticationFailedException ,您应该使用真实的用户名和密码组合以使程序正常工作,并且应该允许您访问您的程序以登录到gmail,详细说明如下

你也应该检查这一点,以便访问你的程序发送邮件,在堆栈跟踪中给出的链接中的整个答案;

https://support.google.com/mail/answer/78754

您需要做的是(如上面链接中所述);

  1. 授予用户访问权限;
  2. https://accounts.google.com/DisplayUnlockCaptcha

    enter image description here

    1. 访问安全性较低的登录信息;
    2. https://www.google.com/settings/security/lesssecureapps

      enter image description here

      之后,如果再次尝试使用有效参数运行代码,将发送测试邮件。