简体   繁体   English

使用 java 身份验证错误发送电子邮件

[英]Sending email using java authentication error

I've seen a lot of error regarding authentication error on sending email and the host is gmail.我已经看到很多关于发送电子邮件和主机是 gmail 的身份验证错误的错误。 I have tried different properties also without authentication but still nothing happens.我也在没有身份验证的情况下尝试了不同的属性,但仍然没有任何反应。 I do not know why on other tutorial, this code is working but when I am trying to run it here it is authentication error.我不知道为什么在其他教程中,此代码有效,但当我尝试在此处运行它时,出现身份验证错误。 I have imported my library but still it is error.我已经导入了我的库,但它仍然是错误的。 I also tried different gmail account but nothing happens.我也尝试了不同的 gmail 帐户,但没有任何反应。 All of the accounts that I've tried are all verified.我试过的所有帐户都经过验证。 Whats wrong?怎么了? Here's the code:这是代码:

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
  public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");


        Session session = Session.getDefaultInstance(properties, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication("mine@gmail.com", "minepass");
            }
        });


        try{
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("yours@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("theirs@yahoo.com"));
            message.setSubject("Send meessage");
            message.setText("Email received");
            Transport.send(message);

            System.out.println("Sent");
        }
        catch(MessagingException e){
            throw new RuntimeException(e);
        }


  }
}

Here is the output log:这是输出日志:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/answer/14257 hg3sm8230925pbb.52 - gsmtp

    at SendEmail.main(SendEmail.java:40)
Caused by: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/answer/14257 hg3sm8230925pbb.52 - gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at SendEmail.main(SendEmail.java:35)

If you are using gmail account for Emails (SMTP) then make sure you have correct Email password in the application and also enable this setting allow less secure apps for your gmail account.如果您使用 gmail 帐户发送电子邮件 (SMTP),请确保您在应用程序中拥有正确的电子邮件密码,并启用此设置以允许您的 gmail 帐户使用安全性较低的应用程序

Allow Secure apps-->Go to manage Account settings-->Left side navigation click security--> then enable less secure apps.允许安全应用-->去管理账户设置-->左侧导航点击安全-->然后启用不太安全的应用。

Make sure that you have enabled connection to your gmail account from external and uncertified apps.确保您已启用从外部和未经认证的应用程序连接到您的 gmail 帐户。 For more info on enabling less secure apps to connect to your google account check: https://support.google.com/accounts/answer/6010255?hl=en This could be your problem from the very beginning, enable less secure apps to connect to your gmail account and re-run your app.有关启用安全性较低的应用程序以连接到您的 Google 帐户的更多信息,请查看: https ://support.google.com/accounts/answer/6010255?hl=en 这可能是您从一开始就存在的问题,启用安全性较低的应用程序以连接到您的 gmail 帐户并重新运行您的应用程序。

If this wont help try putting username and password in properties then there will be no need to use Authenticator .如果这无助于尝试将usernamepassword放入属性中,则无需使用Authenticator Here is a working example of method that is able to send multiple emails.这是能够发送多封电子邮件的方法的工作示例。
Note: it requires javax.mail library注意:它需要javax.mail

Method parameters:方法参数:
from - is your email address (from gmail) from - 是您的电子邮件地址(来自 gmail)
pass - your password pass - 您的密码
to - an array of recipients to - 一组收件人
subject - message title subject - 消息标题
body - a message body body - 消息正文

public static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }
}

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

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