2014-06-13 81 views
3

需要從本地主機發送電子郵件到外部帳戶,如Gmail和雅虎。現在我有一個程序,它可以通過本地電子郵件服務器發送和接收來自本地域的郵件,例如([email protected] < - > [email protected])。但問題是,當我嘗試從本地域發送到Gmail或雅虎帳戶,我無法做到這一點,例如([email protected] - > [email protected])。需要幫助Javamail - 將郵件從本地主機發送到外部帳戶

PS。我使用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 = "[email protected]"; 
       String from = "[email protected]";    
       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年6月13日15:18:01.022」, 「TCP」,「SMTPDeliverer - 消息?13 - 連接失敗:主機名:74.125.25.27,消息:無連接可以作出,因爲目標機器積極地拒絕它」

沒我在這裏錯過什麼,爲什麼遠程機器的連接被拒絕?我不想用Gmail的SMTP服務器發送message.all我需要的是我想我自己的smtp服務器運行發送和接收

+0

你確定你的本地hmail服務器可以將郵件中繼到其他域? – user748316

+0

你確定沒有防火牆阻止你的郵件服務器連接到Gmail SMTP服務器嗎? –

+0

我敢肯定,我完全卸載了防火牆,並且我也通過與我的isp交談解除了端口25的阻塞。 –

回答

0

最後我能夠破解這個,我在其中可以得到我們的工作做好2電子郵件服務器,Apache的詹姆斯和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 [email protected] 
5. under setting->protocold->smtp->delivery of email add "example.com" 

現在運行下面的程序,我們是好去。

Apache的詹姆斯Apache的詹姆斯 - 3.0 BETA4

如上但這沒有任何GUI配置大體相同,這也運行在Linux重量輕命令行的電子郵件服務器。

應用相同的過程,但它有特定的命令行來創建域和帳戶,在此之前,您需要登錄到管理員帳戶來創建用戶。

您將面臨使用Apache james的障礙是它可以很好地運行在32位,但它將有64位服務器啓動問題,因爲它使用的「Wrapper.exe」從tanuki。在那裏你不支持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 = "[email protected]"; // [email protected] 
     String toAddress = "[email protected]"; 


     // 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. 
1

試試這個。完美地工作!將您的Gmail ID設爲[email protected],並將您的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("[email protected]","Password"); 
     } 
    }); 

    try { 

     SMTPMessage message = new SMTPMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
           InternetAddress.parse("[email protected]")); 

     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);   
    } 
    } 
} 
+1

以上不做Karthick想要的。它還包含一些[常見的JavaMail錯誤](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes)。 –

+0

是的,謝謝你的帳單,我不是在尋找使用谷歌SMTP服務器,我想主持自己的本地電子郵件服務器,現在我已經聽說了關於JBoss郵件服務,我正在努力,將讓你更新任何解決方案,同時表示讚賞。 –

相關問題