【问题标题】:Java mail has connection errorsJava 邮件有连接错误
【发布时间】:2017-04-04 19:39:31
【问题描述】:

我正在尝试使用我的 java 应用程序发送电子邮件,但它总是给我一个连接错误。所以我的代码如下所示:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMail {

public static void main(String [] args) {

    String to = "something@gmail.com";
    String from = "fromsomeone@gmail.com";
    String host = "localhost";

    Properties properties = System.getProperties();
    properties.setProperty("mail.user", "fromsomeone");
    properties.setProperty("mail.password", "passwordForSomeone");
    properties.setProperty("mail.smtp.host", host);

    Session session = Session.getDefaultInstance(properties);


    try {

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("subject typed");
        message.setText("This is actual message which is just some lines");
        Transport.send(message);

    }catch (MessagingException mex) {
        mex.printStackTrace();
    }
}
}

我当然会提供有效信息(使用我的 gmail 帐户)。这是我得到的错误:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
  nested exception is:
java.net.ConnectException: Connection refused (Connection refused)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2118)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:712)
at javax.mail.Service.connect(Service.java:366)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at email.SendMail.main(SendMail.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2084)
... 12 more

谁能帮助我,我的代码有什么问题?谢谢!

【问题讨论】:

  • 您能否检查一下您的 localhost 中运行的 SMTP 服务器或 SMTP 服务器是否接受来自 localhost 的连接?
  • 我已经尝试在 cmd 行中检查这个:TELNET MAIL.THEIRDOMAIN.COM 25 Trying 141.8.225.226... 但我没有得到任何回应
  • TELNET: connect to address 141.8.225.226: Operation timed out TELNET: Unable to connect to remote host
  • SMTP 服务器是否正在运行或 25 端口被某种方式阻塞?

标签: java smtp jakarta-mail


【解决方案1】:

这将帮助您通过 gmail 帐户发送邮件;

public class MailMan {
Session session = null;

public MailMan() {
    if (session == null) {
        init();
    }
}

public void init() {

    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("EMAIL", "PASSWORD");
        }
    });
    if (session != null) {
        System.out.println("[OK]");
    } else {
        System.out.println("[NOK]");
    }
}

public void sendMail() {
    if (session == null) {
        System.exit(0);
    }
    try {
        String messageText = "";
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress("no-reply", "No Reply"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        message.setReplyTo(InternetAddress.parse("no-reply"));


        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("to_email")); 
        message.setSubject("TEST");
        message.setText(messageText);

        Transport.send(message);

        System.out.println("[OK]");

    } catch (MessagingException e) {
        e.printStackTrace();
        System.out.println("Not Sent...");
    }
 }
}

还有主类;

public class SendMail {

public static void main(String[] args) {
    MailMan ma = new MailMan();
    ma.sendMail();

  }
}

毕竟,您应该打开“访问不太安全的应用程序”。

【讨论】:

    【解决方案2】:

    你能不能换个试试:

    properties.setProperty("mail.smtp.host", host);
    

    到:

    properties.setProperty("mail.smtp.host", "smtp.gmail.com");
    

    【讨论】:

    • 这给了我一个错误:com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. d8sm19145269wmi.21 - gsmtp
    猜你喜欢
    • 2013-12-11
    • 2019-08-28
    • 2017-12-28
    • 2017-01-19
    • 2012-05-21
    • 1970-01-01
    • 2016-02-21
    • 2020-08-26
    • 2016-08-31
    相关资源
    最近更新 更多