javax.mail.AuthenticationFailedException: No authentication mechanisms supported by both server and

原文

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) {
                }
            }
        }
    }
}

 

报错

Exception in thread "main" javax.mail.AuthenticationFailedException: No authentication mechanisms supported by both server and client at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:829) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:730) at javax.mail.Service.connect(Service.java:388)

 

解决办法

设置相应的SSL证书校验

 mail.smtp.ssl.enable to true.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值