【问题标题】:Javamail - sending mail from localhost to external accountsJavamail - 从本地主机发送邮件到外部帐户
【发布时间】:2014-08-03 20:55:30
【问题描述】:

需要从本地主机发送电子邮件到外部帐户,如 gmail 和 yahoo。现在我有一个程序可以通过本地电子邮件服务器发送和接收来自本地域的邮件,例如(admin@ib-status.com devteam@ib-status.com)。但问题是当我尝试从本地域发送到 gmail 或 yahoo 帐户时,我无法做到这一点,例如(admin@ib-status.com -> myaccount@gmail.com)。需要这方面的帮助

PS。我正在使用 Hmailserver 作为电子邮件服务器

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”“SMTPDeliverer - 消息 13 - 连接失败:主机名:74.125.25.27,消息:无法建立连接,因为目标机器主动拒绝”

我在这里错过了什么吗?为什么远程机器的连接被拒绝?我不想使用 gmail 的 SMTP 服务器发送消息。我只需要我自己的 smtp 服务器运行发送和接收

【问题讨论】:

  • 您确定您的本地 hmail 服务器可以将邮件中继到其他域吗?
  • 您确定没有防火墙阻止您的邮件服务器连接到 Gmail SMTP 服务器吗?
  • 我确定我完全卸载了防火墙,并且我也通过与我的 isp 交谈来解锁了端口 25。

标签: java localhost jakarta-mail james hmail-server


【解决方案1】:

最后我能够破解这个问题,我在 2 个可以完成工作的电子邮件服务器上进行了测试,Apache james 和 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 上运行。

应用相同的程序,但它有特定的命令行来创建域和帐户,在此之前您需要登录到管理员帐户来创建用户。

您使用 Apache james 将面临的障碍是它在 32 位上运行良好,但在 64 位上会出现服务器启动问题,因为它使用来自 tanuki 的“Wrapper.exe”。你不支持 wrapper.exe 的 64 位版本,所以我不得不试用许可证并添加 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 和 Apache James。

电子邮件指针

   * 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.

【讨论】:

    【解决方案2】:

    试试这个。完美运行!将您的 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);         
         }
      }
    }
    

    【讨论】:

    • 以上内容并不能满足 Karthick 的要求。它还包括一些common JavaMail mistakes
    • 是的,谢谢你的账单,我不是在寻找使用 google SMTP 服务器,我想托管自己的本地电子邮件服务器,现在我听说了 JBoss Mail 服务,我正在工作在它上面,会及时更新任何解决方案,同时表示赞赏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2013-08-19
    • 2014-01-10
    • 1970-01-01
    • 2016-07-22
    相关资源
    最近更新 更多