使用API​​从Java发送电子邮件,而不是使用localhost

时间:2014-11-14 17:37:40

标签: java email gmail localhost port

我使用此处的代码和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"等等等等。

好的。那么,有没有人有任何想法我应该使用INSTEAD localhost?这完全不是我的专业领域。

(错误日志相当长,但是,有很多代码被反弹。如果出于任何原因你想要整个事情,请告诉我,我会更新它<) / p>

更新

我要感谢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("username@gmail.com", "password");
        }
    }

   public void createAndSendEmailMessage(ArrayList<?> messageContents) throws MessagingException {
       Email email = new Email();
       email.setRecipient(messageContents.get(0) + "");
       email.setSender("username@gmail.com");
       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", "username@gmail.com");
            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();
        }

1 个答案:

答案 0 :(得分:3)

这是尝试在本地计算机上使用SMTP服务器。如果你在Linux机器上,你可以安装sendmail并进行配置......否则你需要考虑使用你的电子邮件提供商的SMTP服务。

这很简单:您需要身份验证,而且您需要SSL。

更新:您正在使用Gmail,因此您应该能够查找gmail SMTP服务器的内容以及所需的配置。可能是smtp.gmail.commail.gmail.com。它肯定需要身份验证,它肯定需要SSL。

您需要将localhost更改为正确的SMTP服务器地址,作为启动器。您链接到的教程底部将介绍身份验证。它需要您的Gmail用户名和密码。

相关问题