javax.mail.MessagineException:无法连接到SMTP主机:SMTP.gmail.com,端口:465;
这是我用来发送电子邮件的代码:javax.mail.MessagineException:无法连接到SMTP主机:SMTP.gmail.com,端口:465;,java,gmail,jakarta-mail,Java,Gmail,Jakarta Mail,这是我用来发送电子邮件的代码: @Override public void sendEmail(String from, String to, String subject, String content) { //we set the credentials final String username = ConfigService.mailUserName; final String password = ConfigService.mailPassword;
@Override
public void sendEmail(String from, String to, String subject, String content) {
//we set the credentials
final String username = ConfigService.mailUserName;
final String password = ConfigService.mailPassword;
//we set the email properties
Properties props = new Properties();
props.put("mail.smtp.host", ConfigService.mailHost);
props.put("mail.smtp.socketFactory.port", ConfigService.mailSmtpSocketPort);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.port", ConfigService.mailSmtpPort);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject);
message.setText(content);
Transport.send(message);
LOG.info(" Email has been sent");
} catch (MessagingException e) {
LOG.error(" Email can not been sent");
e.printStackTrace();
}
}
当我运行此命令时,我获得下一个错误:
javax.mail.MessagineException:无法连接到SMTP主机:SMTP.gmail.com,端口:465嵌套异常为:
java.net.ConnectException:连接被拒绝
在com.sun.mail.smtp.SMTPTransport.openServer上(SMTPTransport.java:1961)
在com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)上 我在这里看到了与此相关的内容,但在这个问题上没有公认的答案。我可以ping到
smtp.gmail.com
,也可以使用凭据访问gmail帐户
这在我的机器上运行
知道问题出在哪里吗?我在使用NetBeans进行调试时,甚至在执行实际的jar文件时,都遇到过这个问题。防病毒软件会阻止发送电子邮件。您应该在调试期间暂时禁用防病毒软件,或者从扫描中排除NetBeans和实际的jar文件。就我而言,我使用的是Avast 请参阅此链接,了解如何排除:
它对我有用。可能的重复不是重复,因为你提到的问题中没有一个答案能解决我的问题。无论如何,谢谢你的链接,谢谢你的回复。我根本没有使用任何防病毒软件,我也不确定SMTP服务器和防病毒软件之间的关系。