简体   繁体   English

无法使用Java代码从Gmail发送邮件

[英]Not able to send mails from gmail using java code

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();
        }

    }

}

I got the following errror whene i tried running the above code :: 我尝试运行上面的代码时遇到以下错误::

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 is not allowing this code. Gmail不允许使用此代码。 I even received a mail from gmail about suspicious logins and reviewed the devide from where i am running this code. 我什至收到了来自gmail的关于可疑登录的邮件,并从运行此代码的地方查看了这个想法。 What is the solution ? 解决办法是什么 ?

It is AuthenticationFailedException , you should use the real username and password combination in order to get the program working and should give access to your program to sign-in to the gmail which explained in detail down below. 它是AuthenticationFailedException ,您应该使用真实的用户名和密码组合才能使程序正常工作,并且应该允许您访问程序以登录gmail,下面将对此进行详细说明。

And also you should check this out to give access your program to send mail, the whole answer in the link given in the stack trace; 而且,您还应该检查一下这一点,以使您的程序可以发送邮件,即堆栈跟踪中给出的链接中的完整答案;

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

What you have to do is (as explained in the link above); 您需要做的是(如上面的链接中所述);

  1. Give access to your user; 授予您的用户访问权限;

https://accounts.google.com/DisplayUnlockCaptcha https://accounts.google.com/DisplayUnlockCaptcha

在此处输入图片说明

  1. Give access to less-secure sign-in; 允许访问不太安全的登录;

https://www.google.com/settings/security/lesssecureapps https://www.google.com/settings/security/lesssecureapps

在此处输入图片说明

After then, If you try again to run your code with valid parameters, the test mail will be sent. 之后,如果您再次尝试使用有效参数运行代码,则将发送测试邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM