我正在尝试使用我从这里获取的一些已知方法从我的 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 帐户,所以我认为这不是数据解析问题,而且我确信我尝试的所有凭据都是正确的。
这可能是一个礼仪问题。
因此,如果有人能帮我解决这个问题,我将不胜感激。
提前致谢,
马加布罗