代码是在网上抄的别人今年写的,但是我运行起来一直报错,也不知道什么原因,请大神帮忙指点一下;;谢谢!!!
以下是我的代码
public class Sendmail8 { public static void main(String[] args) throws MessagingException, GeneralSecurityException { Properties props = new Properties(); // 开启debug调试 props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "true"); // 设置邮件服务器主机名 props.setProperty("mail.host", "smtp.qq.com"); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); Session session = Session.getInstance(props); Message msg = new MimeMessage(session); msg.setSubject("seenews 错误"); StringBuilder builder = new StringBuilder(); builder.append("url = " + "http://blog.csdn.net/never_cxb/article/details/50524571"); builder.append("\n页面爬虫错误"); // builder.append("\n时间 " + TimeTool.getCurrentTime()); msg.setText(builder.toString()); msg.setFrom(new InternetAddress("90***867@qq.com")); Transport transport = session.getTransport(); transport.connect("smtp.qq.com", "90***867@qq.com", "ymyrj*****mybgab"); transport.sendMessage(msg, new Address[] { new InternetAddress("lu*****23@sina.com") }); transport.close(); } }
然后运行报的异常
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465; nested exception is: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at com.oracel.mail01.Sendmail8.main(Sendmail8.java:48) Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure at sun.security.ssl.Alerts.getSSLException(Unknown Source) at sun.security.ssl.Alerts.getSSLException(Unknown Source) at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source) at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927) ... 4 more
你所请求的接收方邮箱没有开启SMTP服务,请手动去邮箱设置里设置,或者请求@126的默认开启SMTP服务的邮箱。