【问题标题】:Exception in thread "main" javax.mail.AuthenticationFailedException: No authentication mechanisms supported by both server and client线程“主”javax.mail.AuthenticationFailedException 中的异常:服务器和客户端均不支持身份验证机制
【发布时间】:2016-09-25 16:39:04
【问题描述】:
import java.util.Properties; 
import javax.mail.Message;
import javax.mail.MessagingException; 
import javax.mail.Session;
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class ExchangeSend {

    public static void main(String[] args) throws MessagingException {

        String to = "o@red.com";
        String cc = "j@gmail.com";
        String msgSubject = "Test";

        String msgText = "<i>Greetings!</i><br>";
        msgText += "<b>Wish you a nice day!</b><br>";
        msgText += "<font color=red>Duke</font>";

        //String msgText = "Automated test message from Enlite Admin. Message Succeeded";

        ExchangeSend sendM = new ExchangeSend();
        sendM.sendMessage(to, cc, msgSubject, msgText);
    }

    boolean transactional = false;

    public void sendMessage(String to, String cc, String msgSubject, String msgText) throws MessagingException {
        String host = "mail.red.com";
        String username = "e.red";
        String password = "red1";
        String from = "random.admin@red.com";
        String port = "25";

        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", "true");

        Transport transport = null;

        try {
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setContent(message, "text/html; chartset=utf-8");

            InternetAddress to_address = new InternetAddress(to);
            message.addRecipient(Message.RecipientType.TO, to_address);

            InternetAddress cc_address = new InternetAddress(cc);
            message.addRecipient(Message.RecipientType.CC, cc_address);

            message.setSubject(msgSubject);
            message.setText(msgText);

            transport = session.getTransport("smtp");
            transport.connect();
            transport.sendMessage(message, message.getAllRecipients());
        } finally {
            if (transport != null) {
                try {
                    transport.close();
                } catch (MessagingException logOrIgnore) {
                }
            }
        }
    }
}

根据我发布的上述内容,我可以发送带有 smtp false 的短信,但是当我将 smtp auth 设为 true 时,我收到此错误:

线程“主”javax.mail.AuthenticationFailedException 中的异常: 服务器和客户端均不支持任何身份验证机制 com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:829) 在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:730) 在 javax.mail.Service.connect(Service.java:388)

【问题讨论】:

标签: java web-services email jakarta-mail


【解决方案1】:

您的服务器可能希望您在发送密码之前建立安全连接。将mail.smtp.ssl.enable 设置为true。如果这不起作用,请发布JavaMail debug output

另外,请参阅common JavaMail mistakes 列表。

【讨论】:

  • 比尔,错误在于 mail.smtp.ssl.enable 为 true。使用 false 它可以正常工作
  • 好的,正如我上面所说,发布调试输出
猜你喜欢
  • 2020-05-19
  • 2011-11-04
  • 2013-03-08
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 2015-01-10
相关资源
最近更新 更多