相关疑难解决方法(0)

无法使用SMTP发送电子邮件(获取javax.mail.MessagingException:无法将套接字转换为TLS;)

我编写了以下代码,用于通过SMTP作为TLS使用javamail API发送电子邮件,因为不支持SSL,但我最终遇到以下异常.请参阅下面的代码.我使用过调试模式,在代码下面你也可以找到异常.

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailTLS {

    public static void main(String[] args) {

        final String username = "---------@mydomain.com";
        final String password = "***********";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "mail.mydomain.com");
        props.put("mail.smtp.debug", "true");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });
        session.setDebug(true);

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new 
                  InternetAddress("---------@mydomain.com")); …
Run Code Online (Sandbox Code Playgroud)

java ssl smtp jakarta-mail

15
推荐指数
2
解决办法
7万
查看次数

标签 统计

jakarta-mail ×1

java ×1

smtp ×1

ssl ×1