Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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
Java连接到本地主机SMTP服务器_Java_Hmail Server - Fatal编程技术网

Java连接到本地主机SMTP服务器

Java连接到本地主机SMTP服务器,java,hmail-server,Java,Hmail Server,这个代码有什么问题?我正在尝试使用hMailServer在本地主机上发送电子邮件,但它不起作用。当此代码用于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.getProperti

这个代码有什么问题?我正在尝试使用hMailServer在本地主机上发送电子邮件,但它不起作用。当此代码用于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();
    }

我使用的是hMailServer..

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

-Djava.net.preferIPv4Stack=true


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

如果您使用Xampp,请确保Mercury在您的机器上运行,因为如果Mercury未启动,端口25将无法与javaMail一起工作。

我确认只有此参数用Tomcat的catalina.bat编写的“-Djava.net.preferIPv4Stack=true”帮助我将一封电子邮件从SpringWeb应用发送到本地hMailServer实例。
环境:Windows 8,Java 7u60。

我可以确认,通过在ECLIPSE中的“运行配置”中仅实现@Quantas提供的添加参数“-Djava.net.preferIPv4Stack=true”的建议,我获取异常邮件的问题已经解决。消息传递异常现在已经解决

从过去两天开始,我在使用“”捕获从运行在eclipse中的应用程序发送的邮件时遇到以下异常

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
现在问题已经完全解决了,只需将这段代码放入

如下面的快照所示


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

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

-Djava.net.preferIPv4Stack=true


可以通过JavaMail API在我的本地主机上通过“fake SMTP Server”通过我的webapp发送假邮件吗?你是否尝试过使用标准客户端(例如Thunderbird)连接到本地SMTP服务器?我刚刚注意到你关于starttls.enable的行文,这对Gmail很好,因为他们有安全的服务器,你能将其设置为false吗?
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
-Djava.net.preferIPv4Stack=true