gpt4 book ai didi

java - 使用 SSL 发送电子邮件在我的 Java 应用程序中不起作用,TLS 起作用

转载 作者:太空宇宙 更新时间:2023-11-03 14:11:20 24 4
gpt4 key购买 nike

这是 SendMailTLS.java。 这项工作。

    Properties props = new Properties();

props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

props.put("mail.smtp.starttls.enable", "true");
//props.put("mail.smtp.ssl.trust", "smtp.gmail.com"); //if avast is enabled

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

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient));
message.setSubject("TLS");
message.setText("TLS");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}

这是 SendMailSSL.java。 这行不通。

Properties props = new Properties();

props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");

props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

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

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient));
message.setSubject("SSL");
message.setText("SSL");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}

两个类都导入:

    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;

我不明白代码的作用,如果有人能帮我解释一下。我在 SSL 文件中做错了什么吗?如果我使用的是 java 邮件 API,我是否需要担心 TLS 版本?

最佳答案

首先修复这些 common mistakes .然后解释“不起作用”是什么意思,也许包括 JavaMail debug output .

由于您无法弄清楚如何修复 common mistakes你自己,尝试将代码更改为:

Properties props = new Properties();

props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");

Session session = Session.getInstance(props, null);
session.setDebug(true);

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient));
message.setSubject("SSL");
message.setText("SSL");

Transport.send(message, username, password);

System.out.println("Done");

} catch (MessagingException e) {
System.out.println(e);
throw new RuntimeException(e);
}

如果这不起作用,请发布 debug output (在上面启用)并尝试 connection debugging tips并报告结果。

关于java - 使用 SSL 发送电子邮件在我的 Java 应用程序中不起作用,TLS 起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29725873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com