我正在使用spring boot的后端,我试图使用smtp向gmail帐户发送电子邮件,但我发现了一些错误。
这是我在本教程之后的代码:
应用属性:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=xxx@gmail.com
spring.mail.password=xxxpwd
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
类MailObject。java:
package interv.Web.Notifications;
import org.hibernate.validator.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class MailObject {
@Email
@NotNull
@Size(min = 1, message = "Please, set an email address to send the message to it")
private String to;
private String subject;
private String text;
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
邮件ontroller.java:
@Controller
public class MailController {
@Autowired
public EmailServiceImpl emailService;
@Autowired
public SimpleMailMessage template;
@RequestMapping(value = "/send", method = RequestMethod.POST)
public String createMail(@RequestBody MailObject mailObject) {
emailService.sendSimpleMessage(mailObject.getTo(),
mailObject.getSubject(), mailObject.getText());
return "that was succesfull";
}
}
电子邮件服务。Java语言
public interface EmailService {
void sendSimpleMessage(String to,
String subject,
String text);
void sendSimpleMessageUsingTemplate(String to,
String subject,
SimpleMailMessage template,
String templateArgs);
}
EmailServiceImpl.java
@Component
public class EmailServiceImpl implements EmailService{
@Autowired
public JavaMailSender emailSender;
@Override
public void sendSimpleMessage(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(text);
emailSender.send(message);
}
@Override
public void sendSimpleMessageUsingTemplate(String to, String subject, SimpleMailMessage template, String templateArgs) {
String text = String.format(template.getText(), templateArgs);
sendSimpleMessage(to, subject, text);
}
}
运行时,控制台中没有错误,但当我尝试发送电子邮件时,会出现以下错误:
servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target] with root cause
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141) ~[na:1.8.0_161]
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126) ~[na:1.8.0_161]
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) ~[na:1.8.0_161]
我不明白为什么我会犯这个错误:有什么想法吗?
错误明确表示“UnknownHost”。检查你的应用程序是否可以访问internet并能够解析名称。
我认为原因是,出于安全原因,Gmail默认情况下会阻止对不太安全的应用程序的访问检查这个类似的问题,它的答案可能会帮助你错误地使用Gmail发送电子邮件
我想通过单击JFrame netbeans中的按钮将邮件从我的电子邮件地址发送到另一个电子邮件地址。这是代码, 但它给了我以下错误, javax.mail.MessagingException:无法将套接字转换为TLS;嵌套异常是:java.io.lOException:使用SSL套接字工厂类启动TLS中的异常null: host, portsmtp.gmail.com,587;异常:java.l
我收到了错误:org.springframework.mail。MailSendException:邮件服务器连接失败;嵌套的异常是javax.mail。MessaginException:无法将套接字转换为TLS;嵌套的异常是:javax.net.ssl。SSLHandshakeException:没有合适的协议(协议被禁用或密码套件不合适)。失败的消息:javax.mail。MessaginE
我用Javax发送邮件有问题。我们使用gmail大约4年了,用我们的Java软件发送邮件。现在我得到了以下错误: 以下是我的gmail设置: 我还尝试了其他邮件服务器(O365、Strato),但收到了相同的错误。
问题内容: 我编写了以下代码,用于通过SMTP作为TLS使用javamail API通过SMTP发送电子邮件,因为不支持SSL,但最终出现以下异常。请在下面查看我的代码。我使用了调试模式,在代码下面也可以找到异常。 异常跟踪 谁能帮我调试一下吗?提前致谢! 问题答案: 我通过评论以下属性解决了这个问题 并且代码执行时没有错误或警告,或者只是从上述源代码中删除了这一行。到现在为止,它就像是一种魅力。
我编写了以下代码,用于通过SMTP使用javamail API发送电子邮件,因为SSL不受支持,但最终出现以下异常。请看下面我的代码。我使用了调试模式,在代码下面你也可以找到异常。 异常跟踪 有人能帮我调试一下吗?提前感谢!
我正在尝试通过Gmail SMTP服务器使用JavaMail发送电子邮件。代码如下: 返回此错误: 无法将套接字转换为TLS; 完整的堆栈跟踪: 异常线程"main"javax.mail.RuntimeException:ervice.connectMessagingException:无法将套接字转换为TLS;嵌套异常是:ervice.java:176SSLHandshakeException: