【java】Java、Spring:邮件发送不成功,会是什么问题呢?

【java】Java、Spring:邮件发送不成功,会是什么问题呢?
QQ邮箱的相关服务均已开启,代码如下:

public class Demo {

public static void main(String[] args) {

JavaMailSenderImpl sender = new JavaMailSenderImpl();

sender.setHost("smtp.qq.com");

sender.setPort(465);

sender.setUsername("********@qq.com");

sender.setPassword("********");

Properties pro = System.getProperties();

pro.put("mail.smtp.auth", "true");

pro.put("mail.smtp.timeout", "25000");

sender.setJavaMailProperties(pro);

MimeMessage message = sender.createMimeMessage();

try {

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom("********@qq.com"); // 发送人 

helper.setTo("********@126.com"); // 收件人  

helper.setSubject("Title"); // 标题

helper.setText("Content"); // 内容

sender.send(message);

System.out.println("发送完毕!");

} catch (MessagingException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

}

25秒超时后报错如下:

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465; message exception details (1) are:

Failed message 1:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465

at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:899)

at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:158)

at javax.mail.Service.connect(Service.java:233)

at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)

at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)

at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:336)

at Demo.main(Demo.java:31)

有的邮箱密码不填邮箱密码,而是填分配的授权码,我用邮箱密码、授权码都不行...

--------------------已解决,完整代码如下,还有问题可以私信我-----------------

public class Demo {

public static void main(String[] args) {

JavaMailSenderImpl sender = new JavaMailSenderImpl();

sender.setHost("smtp.qq.com");

sender.setPort(465);

sender.setUsername("*********@qq.com");

sender.setPassword("xqbhcaptnzurzbhef"); // 这里要用邀请码,不是你登录邮箱的密码

Properties pro = System.getProperties(); // 下面各项缺一不可

pro.put("mail.smtp.auth", "true");

pro.put("mail.smtp.ssl.enable", "true");

pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

sender.setJavaMailProperties(pro);

MimeMessage message = sender.createMimeMessage();

try {

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom("*********@qq.com"); // 发送人 

helper.setTo("*********@126.com"); // 收件人  

helper.setSubject("Title"); // 标题

helper.setText("Content"); // 内容

sender.send(message);

System.out.println("发送完毕!");

} catch (MessagingException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

}

回答

无法连接。

使用 telnet 试试。

(PS:尽快修改QQ密码)

您好,项目部署在阿里云上,主题和发送人中文会出现乱码,您遇到过这个问题么

测试成功,引用的包有这些:
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

我使用的是腾讯企业邮箱,密码即邮箱密码。

还是抱错:org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
nested exception is:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure. Failed messages: javax.mail.MessagingException: Exception reading response;

nested exception is:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure; message exception details (1) are:

Failed message 1:
javax.mail.MessagingException: Exception reading response;
nested exception is:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462)

at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1260)

at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)

at javax.mail.Service.connect(Service.java:275)

at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)

at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)

at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:336)

at com.phzc.pgs.biz.mail.SendTemplateMailTask.main(SendTemplateMailTask.java:50)

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.readDataRecord(Unknown Source)

at sun.security.ssl.AppInputStream.read(Unknown Source)

at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:97)

at java.io.BufferedInputStream.fill(Unknown Source)

at java.io.BufferedInputStream.read(Unknown Source)

at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:75)

at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1440)

... 7 more

你好,我现在javamail代码发送邮件的时候报com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM
这么一个东西,程序是运行完了,但是接收不到邮件,不知道什么原因,请指教。

以上是 【java】Java、Spring:邮件发送不成功,会是什么问题呢? 的全部内容, 来源链接: utcz.com/a/75534.html

回到顶部