【发布时间】:2015-06-19 06:53:22
【问题描述】:
我在使用 Google 的 SMTP 服务器发送电子邮件时遇到问题。我一直在寻找解决方案,但我还没有找到。这是代码。
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
...
...
...
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
Properties props = new Properties();
props.put("true", "mail.smtp.auth");
props.put("true","mail.smtp.starttls.enable");
props.put("smtp.gmail.com", "mail.smtp.host");
props.put("587", "mail.smtp.port");
Session sess=Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("myemail@gmail.com", "mypassword");
}
}
);
try{
Message message= new MimeMessage(sess);
message.setFrom(new InternetAddress("myemail@gmail.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("myemail@gmail.com"));
message.setSubject("message");
message.setText("text");
Transport.send(message);
JOptionPane.showMessageDialog(null, "Sent!");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
每次按下按钮时,都会显示此错误:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
我尝试了 3 个端口: 25, 465, 587 ,但它总是给我这个错误。我什至为防火墙设置的端口 25 制定了新规则,但这似乎不起作用。有什么想法我在做什么错吗?休眠会导致问题吗?因为我在这个项目中使用它。另外,我已经安装了mysql。
【问题讨论】:
-
@STTR 谢谢,我会调查的。