客户端未通过身份验证以在MAIL FROM期间发送匿名邮件 [英] Client was not authenticated to send anonymous mail during MAIL FROM

查看:498
本文介绍了客户端未通过身份验证以在MAIL FROM期间发送匿名邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在调用此方法的Java应用程序中发送电子邮件:

I am trying to send an email in a java application calling this method:

public static void sendEmail() {

    // Create object of Property file
    Properties props = new Properties();

    // this will set host of server- you can change based on your requirement 
    props.put("mail.smtp.host", "smtp.office365.com");

    // set the port of socket factory 
    //props.put("mail.smtp.socketFactory.port", "587");

    // set socket factory
    //props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

    // set the authentication to true
    props.put("mail.smtp.auth", "true");

    // set the port of SMTP server
    props.put("mail.smtp.port", "587");

    // This will handle the complete authentication
    Session session = Session.getDefaultInstance(props,

            new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication("xx@mail.com", "xx");


                }

            });

    try {

        // Create object of MimeMessage class
        Message message = new MimeMessage(session);

        // Set the from address
        message.setFrom(new InternetAddress("xx@mail.com"));

        // Set the recipient address
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("yy@mail.com"));

                    // Add the subject link
        message.setSubject("Testing Subject");

        // Create object to add multimedia type content
        BodyPart messageBodyPart1 = new MimeBodyPart();

        // Set the body of email
        messageBodyPart1.setText("This is message body");

        // Create object of MimeMultipart class
        Multipart multipart = new MimeMultipart();

        // add body part 2
        multipart.addBodyPart(messageBodyPart1);

        // set the content
        message.setContent(multipart);

        // finally send the email
        Transport.send(message);

        System.out.println("=====Email Sent=====");

    } catch (MessagingException e) {

        throw new RuntimeException(e);

    }

}

但是由于某种原因,当调试命中Transport.send()时,出现了此异常:

but for some reason when the debug hits Transport.send(), I got this exception:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [DB6PR0802CA0037.eurprd08.prod.outlook.com]

即使使用了Authenticator(),为什么会发生这种情况?

why is this happening even though I used the Authenticator()?

推荐答案

如果您通过端口587连接,则最初会启动纯连接,在该连接中,必须通过显式发送STARTTLS命令来启动TLS.您必须告诉JavaMail这样做,否则它将尝试在不安全的情况下继续进行.除非建立TLS连接,否则SMTP服务器不会发送任何身份验证机制信息,因此JavaMail假定不需要身份验证,并尝试不发送任何邮件.

If you connect via port 587 you initially start a plain connection where you have to start TLS by explicitly sending the STARTTLS-command. You have to tell JavaMail to do that, otherwise it will try to proceed unsecured. The SMTP-server doesn't send any authentication-mechanism-informations unless the TLS-connection is established, so JavaMail is assuming that no authentication is needed and tries to send the mail without.

在属性中添加以下条目:

Add the following entry to the properties:

props.put("mail.smtp.starttls.enable", "true");

在尝试进行身份验证之前,JavaMail应该尝试切换到TLS.

WIth that JavaMail should try to switch to TLS before trying to authenticate.

如果失败,则需要通过设置属性来启用调试

If that fails, you need to enable debugging by setting the property

props.put("mail.debug", "true");

并将输出发布到此处.

这篇关于客户端未通过身份验证以在MAIL FROM期间发送匿名邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆