Javamail-将邮件从本地主机发送到外部帐户

Javamail-将邮件从本地主机发送到外部帐户,java,localhost,jakarta-mail,james,hmail-server,Java,Localhost,Jakarta Mail,James,Hmail Server,需要从localhost向gmail和yahoo等外部帐户发送电子邮件。现在我有一个程序,可以通过本地电子邮件服务器发送和接收来自本地域的邮件(admin@ib-status.comdevteam@ib-status.com)。但问题是,当我试图从本地域发送到gmail或雅虎帐户时,我无法做到这一点(admin@ib-status.com->myaccount@gmail.com). 需要帮忙吗 另外,我正在使用Hmailserver作为emailserver public class JMai

需要从localhost向gmail和yahoo等外部帐户发送电子邮件。现在我有一个程序,可以通过本地电子邮件服务器发送和接收来自本地域的邮件(admin@ib-status.comdevteam@ib-status.com)。但问题是,当我试图从本地域发送到gmail或雅虎帐户时,我无法做到这一点(admin@ib-status.com->myaccount@gmail.com). 需要帮忙吗

另外,我正在使用Hmailserver作为emailserver

public class JMailer {

          private static String HOSTNAME = "localhost";
            private static String USERNAME = "admin";
            private static String PASSWORD = "Mylocaldomainpassword";

            public static void main(String[] args) {
            try {  
                String to = "MygmailAccount@gmail.com";
                String from = "admin@ib-status.com";               
                Properties properties = System.getProperties();

                properties.setProperty("mail.smtp.host",HOSTNAME);
                Session session = Session.getInstance(properties, new Authenticator() {                
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(USERNAME, PASSWORD);
                    }
                });
                    MimeMessage message = new MimeMessage(session);
                    message.setFrom(new InternetAddress(from));
                    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                    message.setSubject("My Subject!");
                    message.setText("Here Goes My Message");                    
                    Transport.send(message);
                    System.out.println("Message Sending Completed");
                } catch (MessagingException mex) {
                    mex.printStackTrace();
                }
            }
}
下面是Hmailserver日志中的错误

“SMTPC”4508 0“2014-06-13 15:18:01.022“TCP”SMTPDeliver-消息13-连接失败:主机名:74.125.25.27,消息:由于目标计算机主动拒绝,无法建立连接“


我错过了什么吗?为什么远程机器的连接被拒绝?我不想使用gmail的SMTP服务器发送邮件。我需要的是我自己的SMTP服务器运行以发送和接收邮件。工作完美!将您的Gmail ID置于
sender@gmail.com
,并在密码处输入您的Gmail密码

import com.sun.mail.smtp.SMTPMessage;
import java.util.Properties;
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 SendmailSSl {
public static void main(String[] args) {
    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", "805");

    Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("sender@gmail.com","Password");
        }
    });

    try {

        SMTPMessage message = new SMTPMessage(session);
        message.setFrom(new InternetAddress("sender@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                                 InternetAddress.parse( "reciver@gmail.com" ));

        message.setSubject("Testing Subject");
        message.setText("This is Test mail");
        message.setContent("This Is my First Mail Through Java");
        message.setNotifyOptions(SMTPMessage.NOTIFY_SUCCESS);
        int returnOption = message.getReturnOption();
        System.out.println(returnOption);        
        Transport.send(message);
        System.out.println("sent");

    }
     catch (MessagingException e) {
        throw new RuntimeException(e);         
     }
  }
}

最后,我能够破解这个问题,我在两个电子邮件服务器上进行了测试,这两个服务器可以完成我们的工作,ApacheJames和hmailserver。Hmailserver非常容易运行和配置,因为它有gui来实现这一点

HmailServer 5.4.2

 1. Configure as per the documentation 
 2. Do not use localhost and make sure you change it in C:\Windows\System32\drivers\etc\hosts from "127.0.0.1 localhost" -> "127.0.0.1 example.com"
 3. In add domain of hmailserver give "example.com"
 4. In Domain created add accounts eg.user@example.com
 5. under setting->protocold->smtp->delivery of email add "example.com"
现在运行下面的程序,我们可以开始了

Apache-James-Apache-James-3.0-beta4

基本上与上面相同,但这没有任何GUI可配置,这是一个轻量级的命令行电子邮件服务器,也运行在Linux上

应用相同的过程,但它有特定的命令行来创建域和帐户,在此之前,您需要登录admin帐户来创建用户

使用ApacheJames面临的障碍是它在32位时运行良好,但在64位时会出现服务器启动问题,因为它使用的“Wrapper.exe”来自。你不支持64位版本的wrapper.exe,所以我不得不试用许可证,添加64位wrapper.exe并修改james.bat

除此之外,它工作得很好

package com.javabp.jmailer;


import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JMailer {
    public static void main(String[] args) 
    {
        /***** CHANGE THESE FOUR VARIABLE VALUES TO REFLECT YOUR ENVIRONMENT ******/
        String user = "user";   // Newly created user on JAMES Server
        String password = "password"; // user password

        String fromAddress = "user@example.com"; // newlycreateduser@localhost
        String toAddress = "myaccount@gmail.com";


        // Create a mail session
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", "example.com");
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.username", user);
        properties.put("mail.smtp.password", password);
        Session session = Session.getDefaultInstance(properties, null);

        try 
        {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(fromAddress));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));

            message.setSubject("Email From my Own Server");
            message.setText("Test Mail sent from My Apache James Server!!");
            Transport.send(message);

            System.out.println("Email sent successfully");
        }
        catch (MessagingException e) 
        {
            e.printStackTrace();
        }
    }
}
以上代码适用于Hmailserver和apachejames

电子邮件指针

   * if your sending to external accounts be sure you see you mail at spam folders unless you have registered domain and hosted.
   * once you send a mail to those server there is been chance your IP or domain will be blocked especially gmail. so it is better to have dynamic IP so you can restart your internet connection to send a mail again and also make sure you change your domain before sending even you changed your IP.

你确定你的本地hmail服务器可以将邮件转发到其他域吗?你确定没有防火墙阻止你的邮件服务器连接到Gmail SMTP服务器吗?我确定我已完全卸载了防火墙,并且通过与我的isp对话,我已解除了对端口25的阻止。上述情况并不能满足Karthick的要求。它还包括一些。是的,谢谢比尔,我不想使用谷歌SMTP服务器,我想拥有自己的本地电子邮件服务器,现在我已经听说了JBoss邮件服务,我正在努力工作,我会让你们随时更新任何解决方案,同时非常感谢。