gpt4 book ai didi

java - 从非 Gmail 帐户以编程方式发送电子邮件时出错

转载 作者:太空狗 更新时间:2023-10-29 13:23:49 26 4
gpt4 key购买 nike

我正在尝试使用我从此处获取的一些已知方法,以编程方式从我的 Android 应用程序发送和发送电子邮件。

该方法适用于 gmail 帐户,但当我尝试使用不同的提供商帐户(如 hotmail、yahoo、outlook)时,它会抛出此错误:

07-04 13:18:34.736: I/System.out(32140): ERROR SENDING MESSAGE: javax.mail.MessagingException: Could not connect to SMTP host: smtp-mail.outlook.com, port: 587;
07-04 13:18:34.736: I/System.out(32140): nested exception is:
07-04 13:18:34.736: I/System.out(32140): javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7912c980: Failure in SSL library, usually a protocol error
07-04 13:18:34.736: I/System.out(32140): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x7387f7d0:0x00000000)

我正在使用以下凭据发送邮件:

用户:@hotmail.com主机:smtp-mail.outlook.com端口:587

尝试使用不同的帐户(所有帐户都不是 gmail),但每次都出现相同的错误。

我的 MailSender 类是:

public class MailSender{

public synchronized static void sendMail(String[] dest, String org, String pass, String body, String port, String host){

String[] to = dest;
final String user = org;
final String password = pass;
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);

Properties props = new Properties();
props.put("mail.smtp.user", user);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

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

try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
}
message.setRecipients(MimeMessage.RecipientType.TO, addressTo);
message.setSubject("Arcas Ollé Alarm Message");
BodyPart messageBody = new MimeBodyPart();
messageBody.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBody);
message.setContent(multipart );
Transport.send(message);
System.out.println("MESSAGE SENT...");
}catch (MessagingException ex) {
System.out.println("ERROR SENDING MESSAGE: "+ ex);
}
}
}

我这样调用方法:

MailSender.sendMail(dest, org, pass, body, port, host);

正如我之前所说,该方法适用于 gmail 帐户,所以我认为这不是数据解析问题,而且我确信我尝试的所有凭据都是正确的。

这可能是礼仪问题。

所以如果有人能帮我解决这个问题,我将不胜感激。

提前致谢

马加布罗。

最佳答案

供引用:

Gmail- 主机: smtp.gmail.com,端口:465

Hotmail- 主机: smtp.live.com ,端口:587

Yahoo- 主机: smtp.mail.yahoo.com,端口:465

关于java - 从非 Gmail 帐户以编程方式发送电子邮件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24573296/

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