我尝试使用其他服务器上的帐户发送电子邮件,而不是 Gmail ,但发送时会出现此异常。
错误:
02-05 16:10:37.688: W/System.err(30346): javax.mail.MessagingException: Could not connect to SMTP host: smtp.suprema.net.br, port: 587;
02-05 16:10:37.688: W/System.err(30346): nested exception is:
02-05 16:10:37.688: W/System.err(30346): javax.net.ssl.SSLHandshakeException: Handshake failed
02-05 16:10:37.688: W/System.err(30346): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
02-05 16:10:37.688: W/System.err(30346): at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
02-05 16:10:37.688: W/System.err(30346): at javax.mail.Service.connect(Service.java:288)
02-05 16:10:37.688: W/System.err(30346): at com.prologica.smart_detection.Mail.Send(Mail.java:71)
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.utils.Utilities.SendMail(Utilities.java:162)
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.utils.Utilities.NotifyContatcts(Utilities.java:106)
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.services.NotificationService.NotifyContacts(NotificationService.java:106)
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.services.NotificationService.GetNotifications(NotificationService.java:79)
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.services.NotificationService.run(NotificationService.java:61)
02-05 16:10:37.689: W/System.err(30346): at java.lang.Thread.run(Thread.java:818)
02-05 16:10:37.689: W/System.err(30346): Caused by: javax.net.ssl.SSLHandshakeException: Handshake failed
02-05 16:10:37.689: W/System.err(30346): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:374)
02-05 16:10:37.689: W/System.err(30346): at com.android.org.conscrypt.OpenSSLSocketImpl.waitForHandshake(OpenSSLSocketImpl.java:598)
02-05 16:10:37.689: W/System.err(30346): at com.android.org.conscrypt.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:560)
02-05 16:10:37.689: W/System.err(30346): at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:1449)
02-05 16:10:37.689: W/System.err(30346): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1366)
02-05 16:10:37.689: W/System.err(30346): ... 9 more
02-05 16:10:37.689: W/System.err(30346): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb7617058: Failure in SSL library, usually a protocol error
02-05 16:10:37.689: W/System.err(30346): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:790 0xae73aedd:0x00000000)
02-05 16:10:37.690: W/System.err(30346): at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
02-05 16:10:37.690: W/System.err(30346): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:302)
02-05 16:10:37.690: W/System.err(30346): ... 13 more
我的方法
public Boolean Send(String localFile, String subject, String body, String recipients)
{
Boolean ret = false;
try
{
String user = MailSenderConfiguration.USER;
String pass = MailSenderConfiguration.PASSWORD;
Properties props = System.getProperties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", MailSenderConfiguration.SMTP);
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", MailSenderConfiguration.PORT);
props.put("mail.smtp.socketFactory.port", MailSenderConfiguration.PORT);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
Authenticator auth = new Autenticador(user,pass);
Session session = Session.getDefaultInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(user));
if (recipients.indexOf(',') > 0)
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
//msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipients));
msg.setSentDate(new Date());
msg.setSubject(subject);
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(body, "text/plain");
Multipart mp = new MimeMultipart();
mp.addBodyPart(textPart);
msg.setContent(mp);
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(MailSenderConfiguration.SMTP, Integer.parseInt(MailSenderConfiguration.PORT), user, pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
ret = true;
}
catch(Exception e)
{
e.printStackTrace();
}
return ret;
}
答案 0 :(得分:-2)
您的代码包含多个most common JavaMail mistakes。首先修复它们。
然后按照这些JavaMail FAQ tips for debugging。
我无法确定您发布的是您实际尝试连接的端口,但也许您正在尝试与非SSL端口建立SSL连接。或者,您可能遇到了防火墙或防病毒问题。