线程“main”中的异常javax.mail.AuthenticationFailedException:服务器和客户端均不支持任何身份验证机制

时间:2016-09-22 07:41:54

标签: java web-services email javamail

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时,我收到此错误:

  

线程“main”中的异常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)

1 个答案:

答案 0 :(得分:0)

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

另请参阅此common JavaMail mistakes列表。