【问题标题】:Sending an email using Google's SMTP server使用 Google 的 SMTP 服务器发送电子邮件
【发布时间】: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。

【问题讨论】:

标签: email java mysql smtp


【解决方案1】:

您的程序正在尝试连接到本地端口,这就是它失败的原因。如果您的清单是真实代码,则您在 props.set 语句中获得了键和值。

【讨论】:

  • 是的,就是这样。谢谢。
猜你喜欢
  • 2012-06-10
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多