我正在使用此处找到的代码和 API:http://www.tutorialspoint.com/java/java_sending_email.htm
当我运行代码时,我的错误输出是:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2053)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:194)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
从第一行可以明显看出,主要问题是“无法连接到主机,端口:localhost”等等。
好吧。那么,有人知道我应该使用什么来代替本地主机吗?这完全不是我的专业领域。
(错误日志相当长,但是,有很多代码被退回。如果出于某种原因您想要整个内容,请告诉我,我会更新它)
更新:
我要感谢 StackOverflow 社区,感谢我在网站上看到的有关此主题的所有帖子,以及那些帮助我回答此问题的人。请在下面找到我完成的代码,它将接收电子邮件对象(来自另一个类)并将其发送出去!注意,我显然取出了 Gmail 帐户的用户名和密码:)
import java.util.ArrayList;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail
{
private class SMTPAuthenticator extends Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3a6a0b6a1bdb2beb693b4beb2babffdb0bcbe" rel="noreferrer noopener nofollow">[email protected]</a>", "password");
}
}
public void createAndSendEmailMessage(ArrayList<?> messageContents) throws MessagingException {
Email email = new Email();
email.setRecipient(messageContents.get(0) + "");
email.setSender("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9eebedfbecf0fff3fbdef9f3fff7f2b0fdf1f3" rel="noreferrer noopener nofollow">[email protected]</a>");
email.setSubject(messageContents.get(1) + "");
email.setMessageContent(messageContents.get(2)+"");
sendEmailMessage(email);
}
public void sendEmailMessage(Email email) throws MessagingException {
// Get system properties
Properties props = System.getProperties();
props = new Properties();
props.put("mail.smtp.user", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64111701160a050901240309050d084a070b09" rel="noreferrer noopener nofollow">[email protected]</a>");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SMTPAuthenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(false);
MimeMessage msg = new MimeMessage(session);
msg.setText(email.getMessageContent());
msg.setSubject(email.getSubject());
msg.setFrom(new InternetAddress(email.getSender()));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email.getRecipient()));
Transport transport = session.getTransport("smtps");
transport.connect("smtp.gmail.com", 465, "username", "password");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
}
因为我觉得当人们发布代码的一部分而不是调用它的代码时很烦人,所以我也将向您展示这一点!
ArrayList<String> emailInfo = new ArrayList<String>();
emailInfo.add(userEmailAddress.getText()+"@gmail.com");
emailInfo.add("An account has been created for you!");
emailInfo.add("Here is a message");
SendEmail newEmail = new SendEmail();
try {
newEmail.createAndSendEmailMessage(emailInfo);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
最佳答案
这正在尝试使用本地计算机上的 SMTP 服务器。如果您使用的是 Linux 机器,则可以安装 sendmail 并进行配置...否则您将需要考虑使用电子邮件提供商的 SMTP 服务。
这并不简单:您需要身份验证并且需要 SSL。
更新:您正在使用 gmail,因此您应该能够查找 gmail SMTP 服务器是什么以及它需要什么配置。可能是 smtp.gmail.com
或 mail.gmail.com
。它肯定需要身份验证,并且肯定需要 SSL。
首先,您需要将 localhost
更改为正确的 SMTP 服务器地址。您链接到的教程的底部介绍了身份验证。它会需要您的 Gmail 用户名和密码。
关于java - 使用 API 从 Java 发送电子邮件,而不是使用 localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935737/