Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaMail API和SMTPserver_Java_Email_Jakarta Mail - Fatal编程技术网

JavaMail API和SMTPserver

JavaMail API和SMTPserver,java,email,jakarta-mail,Java,Email,Jakarta Mail,我正在尝试使用javamail api发送电子邮件,似乎必须安装SMTP服务器才能使其正常工作。我在windows 8的IIS 6上启用了默认SMTP服务器。我按照以下博客中提到的所有步骤进行设置。但当我运行JavaMail API的wiki页面上给出的代码示例时,仍然会遇到以下异常: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is:

我正在尝试使用javamail api发送电子邮件,似乎必须安装SMTP服务器才能使其正常工作。我在windows 8的IIS 6上启用了默认SMTP服务器。我按照以下博客中提到的所有步骤进行设置。但当我运行JavaMail API的wiki页面上给出的代码示例时,仍然会遇到以下异常:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at EmailTester.main(EmailTester.java:47)
    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 7 more
代码如下:

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

public class EmailTester
{
   public static void main(String [] args)
   {    
      // Recipient's email ID needs to be mentioned.
      String to = "mymail@gmail.com";//My email address

      // Sender's email ID needs to be mentioned
      String from = "blah@blah.com";// Should this be a real email address?

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      properties.put("mail.debug", "true");
      // Get the default Session object.
      Session session = Session.getInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}
所以我有很多问题: 1) 在IIS上安装SMTP服务器是否适合与JavaMail API一起使用?我做错事情了吗


2) 我应该使用第三方SMTP服务器,如Apache James吗?

不,除非电脑无法连接到internet,否则不需要安装STMP服务器即可正常工作

在线
String host=“localhost”
您需要将
localhost
更改为internet上SMTP服务器的地址

查看您的电子邮件客户端的设置,并使用您在那里的地址进行开发


但是,当应用程序处于活动状态时,您可能需要对其进行更改,因为如果您的生产环境位于与活动环境不同的网络上,则它们可能具有不同的SMTP服务器。SMTP服务器通常不喜欢处理来自国外网络的电子邮件。它被称为中继,为了控制垃圾邮件,他们禁止这种行为。

No
,使用Javamail发送电子邮件不需要安装本地SMTP服务器

由于某些原因,您收到邮件连接错误

使用示例通过Javamail使用
Gmail发送电子邮件

还有
stringfrom=”blah@blah.com";应包含您发送电子邮件的地址


如果您使用本地主机(如本例中所示)发送电子邮件,它应该类似于
String host=“127.0.0.1\”
和其他配置,但正如我所说,这是绝对不必要的

您能告诉我您尝试连接的主机是哪个吗?什么是字符串host=“localhost”?我已在笔记本电脑的IIS上启用了STMP服务器,因此我正在使用localhost连接我机器上的STMP服务器。我做错什么了吗?我已经添加了一个答案,看看它是否有助于检查您和Jurgen的解决方案。我有相同的代码,但即使在做出了答案中给出的更改后,我仍然会遇到相同的问题。为什么呢?它现在适用于gmail。但是,如果要从非gmail帐户(我公司的电子邮件帐户)发送电子邮件,我必须安装SMTP服务器?不,请参阅Jurgen的回答。不,您不必安装SMTP服务器,如果您的公司有一个已发送外发电子邮件的电子邮件服务,那么它已经有一个SMTP服务器。只需连接到它。更多信息请参见@Jurgen的答案。只是好奇,为什么当应用程序上线时,他必须更改设置?我遗漏了什么吗?如果您的生产环境与活动环境位于不同的网络上,则它们可能具有不同的SMTP服务器。SMTP服务器通常不喜欢处理来自国外网络的电子邮件。这叫做中继,他们为了控制垃圾邮件而禁止这种行为。啊,很好,我不知道,介意在答案中添加这一点,让它更具信息性:)谢谢你们的回答,伙计们