简体   繁体   English

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

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

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. 所以我试图让Java Mail工作,因为我在这个网站上看到的其他答案都没有用,我不得不假设在过去一年左右的时间里有些变化。 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: 以下是我的代码:

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", "myEmail@gmail.com");
        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("myEmail@gmail.com"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("myEmail@gmail.com"));

        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,"myEmail@gmail.com","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. 这没有意义,因为我将mail.smtp.auth设置为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! 谢谢!

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! 它是2014年11月,目前适用于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. 您第一次尝试时会收到来自Google的电子邮件,它会指导您更改设置。

Thanks and good luck! 谢谢,祝你好运!

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, none of the mail.smtp.* properties apply. 请注意,由于您使用的是“smtps”协议,因此不会应用mail.smtp。*属性。 But that's ok because you don't need them anyway due to the way you're calling the connect method. 但这没关系,因为你无论如何都不需要它们,因为你正在调用connect方法。

暂无
暂无

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

相关问题 com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 需要身份验证 - com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required com.sun.mail.smtp.SMTPSendFailedException:530 5.7.1客户端未通过身份验证 - com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated com.sun.mail.smtp.SMTPSendFailedException Javamail - com.sun.mail.smtp.SMTPSendFailedException Javamail 如何解决com.sun.mail.smtp.SMTPSendFailedException在Java中? - how to solve com.sun.mail.smtp.SMTPSendFailedException in java ? com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0必须首先发出STARTTLS命令 - com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first MailSendException:失败的消息:com.sun.mail.smtp.SMTPSendFailedException - MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException Spring Boot 邮件发件人错误:com.sun.mail.smtp.SMTPSendFailedException - Error at Spring Boot mail sender: com.sun.mail.smtp.SMTPSendFailedException 获取问题com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0必须首先发出STARTTLS命令。 dg12sm142339333pac.47 - gsmtp - Getting Issue com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. dg12sm142339333pac.47 - gsmtp com.sun.mail.smtp.SMTPSendFailedException:452 4.4.5磁盘空间不足; 稍后再试 - com.sun.mail.smtp.SMTPSendFailedException: 452 4.4.5 Insufficient disk space; try again later 错误:com.sun.mail.smtp.SMTPSendFailedException:451 4.3.0 错误:队列文件写入错误 - Error: com.sun.mail.smtp.SMTPSendFailedException: 451 4.3.0 Error: queue file write error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM