2009-12-22 72 views
3

我需要使用JavaMail發送簡單的html消息。當我試圖在互聯網上找到一些很好的例子和解釋,每一個下一個例子都讓我更加生氣和憤怒。如何配置環境以使用JavaMail?

所有這些愚蠢的例子包含複製和粘貼它們的差別僅在評論和一個不錯的聲明,首先,你應該配置你的SMTP和POP3服務器的Java代碼。

據我所知,沒有人願意讓一個做廣告的一些具體產品,但配置的服務器是恕我直言最難的部分。那麼,任何人都可以給我一些關於配置具體服務器(例如Kerio或其他任何一個)的非常有用的信息(無需Java代碼)?

我現在有什麼是下一個異常:

250 2.0.0 Reset state 
javax.mail.SendFailedException: Invalid Addresses; 
    nested exception is: 
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Relaying to <[email protected]> denied (authentication required) 

UPD。以前所有文本的簡單重寫是:想象你有Windows,jdk,而沒有別的。你想製作java程序並在你的機器上運行它。這個程序應該發送「Hello world!」到您的Gmail帳戶。列出你的步驟。

UPD2。下面是代碼:

Properties props = new Properties(); 
props.setProperty ("mail.transport.protocol", "smtp"); 
props.setProperty ("mail.host", "smtp.gmail.com"); 
props.setProperty ("mail.user", "[email protected]"); 
props.setProperty ("mail.password", "password_from_email_above"); 

Session mailSession = Session.getDefaultInstance (props, null); 
mailSession.setDebug (true); 
Transport transport = mailSession.getTransport(); 

MimeMessage message = new MimeMessage (mailSession); 
message.setSubject ("HTML mail with images"); 
message.setFrom (new InternetAddress ("[email protected]")); 
message.setContent ("<h1>Hello world</h1>", "text/html"); 
message.addRecipient (Message.RecipientType.TO, 
     new InternetAddress ("[email protected]")); 

transport.connect(); 
transport.sendMessage (message, 
     message.getRecipients (Message.RecipientType.TO)); 

和異常是:

RSET 
250 2.1.5 Flushed 3sm23455365fge.10 
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 3sm23455365fge.10 
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829) 
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886) 
    at com.teamdev.imgmail.MailSender.main(MailSender.java:33) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    ... 
+0

你的問題太含糊回答,因爲它代表。請告訴我們更多關於您的主機及其上運行的組件的信息。繼續品牌,我們不在乎。告訴我們你想做什麼,什麼是工作,什麼不是。 – 2009-12-22 12:23:53

+0

@Carl Smotricz:我加了UPD部分。 – Roman 2009-12-22 12:29:48

+0

更新是一個*更*更好的問題。爲此,事實證明,您甚至不需要自己的SMTP服務器,因爲Google恰好爲您運行一個相當大的服務器。 – 2009-12-22 12:32:50

回答

13

如果你正在尋找一個教程配置SMTP服務器,你不應該找的JavaMail。只要尋找您所選擇的服務器上的教程(Kerio,例如...或EximSendMailApache JamesPostfix)或問上Serverfault。任何符合SMTP的服務器都可以很好地與JavaMail一起使用。

或者,你甚至可以使用任何「標準」的郵件提供商的基礎設施。例如,我使用一個Google Apps帳戶以及Google的SMTP基礎結構從我們的Java應用程序發送郵件。 Using a Gmail account無論如何,如果您不想設置自己的SMTP服務器以簡單地測試驅動器JavaMail,這是一個很好的起點。

作爲最後的選擇,你甚至可以查找的MX Records的域,並直接提供您的郵件到收件人的SMTP服務器。有一些常見的問題需要解決。

作爲最後一點,你就必須考慮如何避免您的郵件被過濾爲垃圾郵件 - 這是一個很大的話題本身。這裏有助於依靠標準提供商來處理您在託管自己的服務器時可能遇到的一些問題。

btw:關於您發佈的錯誤消息:SMTP服務器拒絕中繼消息。這是因爲如果您的SMTP服務器(認爲它)正在example.com上運行,並且您正在將[email protected]發送到[email protected],那麼您就要求SMTP服務器充當中繼。這是幾年前的慣例,直到它被垃圾郵件發送者濫用爲止。從那時起,鼓勵校長拒絕中繼。您有兩種選擇:在發送郵件之前進行身份驗證或發送到僅在您的服務器上託管的帳戶(即在example.com上,例如[email protected])。

編輯:

下面是一些代碼,以幫助您開始使用authenticationg(工作與Gmail帳戶,但對於自己的服務器應該做的一樣好)

private Session createSmtpSession() { 
    final Properties props = new Properties(); 
    props.setProperty("mail.smtp.host", "smtp.gmail.com"); 
    props.setProperty("mail.smtp.auth", "true"); 
    props.setProperty("mail.smtp.port", "" + 587); 
    props.setProperty("mail.smtp.starttls.enable", "true"); 
    // props.setProperty("mail.debug", "true"); 

    return Session.getDefaultInstance(props, new javax.mail.Authenticator() { 

    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication("[email protected]", "mypassword"); 
    } 
    }); 
} 
+0

謝謝。它的工作原理 – 2013-02-11 04:11:58

1

我可以看到你的問題的一部分。它在錯誤信息中有充分的解釋。

您要發送郵件到的SMTP服務器(即您在JavaMail配置中配置的地址之一)拒絕將郵件轉發到[email protected]。看起來像您的SMTP服務器中的配置問題。正如sfussenegger所指出的那樣,它與javamail無關。

因此,您並未在所有方面同時進行調試,因此嘗試使用已知的工作SMTP客戶端來尋址您的SMTP服務器可能是個好主意。例如,雷鳥會很好。如果您可以通過它從Thunderbird發送郵件,那麼JavaMail應該沒什麼問題。


更新:

的正確地址谷歌的SMTP服務器爲:smtp.gmail.com。這是您在JavaMail中配置的服務器嗎?你能向我們展示匹配的錯誤信息嗎?

0

這應該工作:

import java.text.MessageFormat; 
import java.util.List; 
import java.util.Properties; 

import javax.mail.Authenticator; 
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 Emailer { 

    public static void main(String[] args) { 

     String hostname = args[0]; 
     final String userName = args[1]; 
     final String passWord = args[2]; 
     String toEmail = args[3]; 
     String fromEmail = args[4]; 
     String subject = args[5]; 
     String body = ""; 
     // add rest of args as one body text for convenience 
     for (int i = 6; i < args.length; i++) { 
      body += args[i] + " "; 
     } 

     Properties props = System.getProperties(); 
     props.put("mail.smtp.host", hostname); 

     Session session = Session.getInstance(props, new Authenticator() { 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(userName, passWord); 
      } 
     }); 

     MimeMessage message = new MimeMessage(session); 
     try { 
      message.setFrom(new InternetAddress(fromEmail)); 
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); 
      message.setSubject(subject); 
      message.setText(body); 
      Transport.send(message); 

     } catch (MessagingException e) { 
      System.out.println("Cannot send email " + e); 
     } 
    } 
} 

你需要把了JavaMail的mail.jar在classpath的javax.mail依賴性。 我不確定Google是否允許您發送您想要的電子郵件。如何嘗試其他電子郵件提供商,如您的ISP?

+0

只要我指定了正確的返回地址(或者我向Google註冊的地址之一),Google就可以讓我隨時隨地發送郵件(包括通過API)。 – 2009-12-23 12:39:36

2

工作示例結合上述答案,使用活化-1.1.jar郵件1.4.1.jar和SMTP主機是的Gmail

  1. 更換[email protected]user_pw符合return new PasswordAuthentication("[email protected]", "user_pw");

  2. 而且,你想,你想收到的電子郵件的郵件地址來替換[email protected]

    package com.test.sendEmail; 
    import java.util.Properties; 
    import javax.mail.*; 
    import javax.mail.internet.*; 
    
    public class sendEmailTest { 
    
    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        sendEmailTest emailer = new sendEmailTest(); 
        //the domains of these email addresses should be valid, 
        //or the example will fail: 
        emailer.sendEmail(); 
    } 
    
    /** 
        * Send a single email. 
        */ 
    public void sendEmail(){ 
    Session mailSession = createSmtpSession(); 
    mailSession.setDebug (true); 
    
    try { 
        Transport transport = mailSession.getTransport(); 
    
        MimeMessage message = new MimeMessage (mailSession); 
    
        message.setSubject ("HTML mail with images"); 
        message.setFrom (new InternetAddress ("[email protected]")); 
        message.setContent ("<h1>Hello world</h1>", "text/html"); 
        message.addRecipient (Message.RecipientType.TO, new InternetAddress ("[email protected]")); 
    
        transport.connect(); 
        transport.sendMessage (message, message.getRecipients (Message.RecipientType.TO)); 
    } 
    catch (MessagingException e) { 
        System.err.println("Cannot Send email"); 
        e.printStackTrace(); 
    } 
    } 
    
    private Session createSmtpSession() { 
    final Properties props = new Properties(); 
    props.setProperty ("mail.host", "smtp.gmail.com"); 
    props.setProperty("mail.smtp.auth", "true"); 
    props.setProperty("mail.smtp.port", "" + 587); 
    props.setProperty("mail.smtp.starttls.enable", "true"); 
    props.setProperty ("mail.transport.protocol", "smtp"); 
    // props.setProperty("mail.debug", "true"); 
    
    return Session.getDefaultInstance(props, new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication("[email protected]", "user_pw"); 
        } 
    }); 
    } 
    
    } 
    
相關問題