Java连接到localhost SMTP服务器

时间:2012-05-02 11:48:58

标签: java hmail-server

这段代码有什么问题?我正在尝试使用hMailServer在我的localhost上发送电子邮件,但它无法正常工作。虽然此代码适用于Gmail SMTP服务器..我可能认为错误发生在我的hMailServer中,但我找不到它..

    try{
    String host = "127.0.0.1";
    String from = "account@127.0.0.1";
    String pass = "1q2w#E$R";
    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true"); // added this line
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "25");
    props.put("mail.smtp.auth", "true");

    String[] to = {"test@test.com"}; // added this line

    Session session = Session.getDefaultInstance(props, null);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));

    InternetAddress[] toAddress = new InternetAddress[to.length];

    // To get the array of addresses
    for( int i=0; i < to.length; i++ ) { // changed from a while loop
        toAddress[i] = new InternetAddress(to[i]);
    }
    for( int i=0; i < toAddress.length; i++) { // changed from a while loop
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
    }
    message.setSubject("sending in a group");
    message.setText("Welcome to JavaMail");
    Transport transport = session.getTransport("smtp");
    transport.connect(host, from, pass);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

这是我得到的错误:

    javax.mail.MessagingException: Could not connect to SMTP host: 127.0.0.1, port: 25;
nested exception is:
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1213)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
    at javax.mail.Service.connect(Service.java:233)
    at javax.mail.Service.connect(Service.java:134)
    at nl.company.cms.login.emailservice.TestMail.main(TestMail.java:71)

我正在使用hMailServer ..

5 个答案:

答案 0 :(得分:3)

如果您使用的是Java 7,那么我会在您启动应用程序时尝试将其添加为JVM参数:

-Djava.net.preferIPv4Stack=true

这可能是必要的,因为Java现在正在尝试使用IPv6,因此我们需要告诉它更喜欢IPv4堆栈。

答案 1 :(得分:1)

如果您正在使用Xampp,请确保Mercury正在您计算机上的计算机上运行,​​因为如果未启动水银,则端口25将无法与javaMail一起使用。

答案 2 :(得分:1)

我确认只有这个参数“-Djava.net.preferIPv4Stack = true”,写在Tomcat的catalina.bat中,帮助我从我的Spring网络应用程序发送电子邮件到我的本地hMailServer实例。 环境:Windows 8,Java 7u60。

答案 3 :(得分:0)

我可以通过实施@Quantas提供的添加参数&#34; -Djava.net.preferIPv4Stack = true&#34;的建议来确认。在&#34;运行配置&#34;在ECLIPSE中,我的异常邮件问题.Messaging Exception现已解决。

从过去2天开始,我在使用&#34; Fake SMTP mail server&#34;捕获从我在eclipse中运行的应用程序发出的邮件。

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

现在只需将这一小段代码放入&#34; VM ARGUMENTS&#34;就可以完全解决问题。在&#34;运行配置&#34; ECLIPSE LUNA

-Djava.net.preferIPv4Stack=true

如下面的快照所示

enter image description here

这表明java更喜欢使用IPV4堆栈而不是IPV6(之前更喜欢的)

答案 4 :(得分:0)

我可以确认只为Tomcat添加VM参数:

  

-Djava.net.preferIPv4Stack = true

我可以通过JavaMail API从Web应用程序在本地主机上使用“伪造的SMTP服务器”发送伪造的邮件

相关问题