com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1 需要身份验证(Java 邮件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26728132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:29:59  来源:igfitidea点击:

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required (Java Mail)

javagmailjavamail

提问by Jared Smith

So i'm trying to get Java Mail to work and because other answers I've seen of this site are not working, I have to assume somethings have changed in the past year or so. So I apoligies if this question seems like a DUPLICATE I can't figure out why it is not working. The following is my code:

所以我试图让 Java Mail 工作,因为我在这个网站上看到的其他答案都不起作用,我不得不假设在过去一年左右的时间里发生了一些变化。因此,如果这个问题看起来像重复,我不知道为什么它不起作用。以下是我的代码:

try{
        Properties property = new Properties();
        property.setProperty("mail.smtp.host", "smtp.gmail.com");
        property.setProperty("mail.smtp.starttls.enable", "true");
        //property.setProperty("mail.smpt.port", "25");
        property.setProperty("mail.smtp.user", "[email protected]");
        property.setProperty("mail.smtp.auth", "true");

        System.out.println("Mail Check 1");

        Session session = Session.getDefaultInstance(property);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));

        System.out.println("Mail Check 2");

        message.setSubject("Oil Error Report");
        message.setText(emailMessage);

        System.out.println("Mail Check 3");

        Transport transport = session.getTransport("smtps");
        transport.connect("smtp.gmail.com",465,"[email protected]","myPassword");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

        System.out.println("Mail Sent");
    }catch(Exception ex){
        System.out.println("Mail fail");
        System.out.println(ex);
    }

I get up to Mail Check 3 then I get the following exception:

我起床进行邮件检查 3 然后我收到以下异常:

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 u3sm10254140ioi.27 - gsmtp

Which doesn't make sense because I have mail.smtp.auth set to true. I have looked at past answers and I have tried to get it to work for while before posting. I'm assuming it's something simple.. Any help would be much appreciated! Thanks!

这没有意义,因为我将 mail.smtp.auth 设置为 true。我查看了过去的答案,并尝试在发布之前让它工作一段时间。我假设这很简单.. 任何帮助将不胜感激!谢谢!

回答by Jared Smith

public class EmailSender {    
    public void sendEmail(String emailMessage){

        try{
            final String fromEmail = ""; //requires valid gmail id
            final String password = ""; // correct password for gmail id
            final String toEmail = ""; // can be any email id 

            System.out.println("TLSEmail Start");
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
            props.put("mail.smtp.port", "587"); //TLS Port
            props.put("mail.smtp.auth", "true"); //enable authentication
            props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS

                //create Authenticator object to pass in Session.getInstance argument
            Authenticator auth = new Authenticator() {
                //override the getPasswordAuthentication method
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(fromEmail, password);
                }
            };
            Session session = Session.getInstance(props, auth);

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(fromEmail));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));

            System.out.println("Mail Check 2");

            message.setSubject("Oil Error Report");
            message.setText(emailMessage);

            System.out.println("Mail Check 3");

            Transport.send(message);
            System.out.println("Mail Sent");
        }catch(Exception ex){
            System.out.println("Mail fail");
            System.out.println(ex);
        }
    }
}

This is the code needed that got it working! It is November 2014 and this currently works for gmail! I hope this code will help save someone time, took much of mine! Along with this code you have to change your email setting to allow emails such as these to be allowed. You will get an email from google the first time you attempt, and it will walk you through on changing the setting.

这是让它工作所需的代码!现在是 2014 年 11 月,这目前适用于 gmail!我希望这段代码能帮助节省一些时间,占用了我的大部分时间!除此代码外,您还必须更改电子邮件设置以允许此类电子邮件。第一次尝试时,您会收到来自 google 的电子邮件,它会指导您更改设置。

Thanks and good luck!

谢谢,祝你好运!

回答by Bill Shannon

Did you look at the URL? Did you follow all the steps there? What happened?

你看网址了吗?您是否按照那里的所有步骤操作?发生了什么?

You'll want to fix these common mistakes.

你会想要修正这些常见的错误

Note that since you're using the "smtps" protocol, noneof the mail.smtp.* properties apply. But that's ok because you don't need them anyway due to the way you're calling the connect method.

请注意,由于您使用的是“smtps”协议,因此没有任何mail.smtp.* 属性适用。但这没关系,因为由于您调用 connect 方法的方式,您无论如何都不需要它们。