2013-02-20 208 views
0

我一直在嘗試使用javamail api發送電子郵件。來自smtp服務器(smtp.live.com)的調試顯示爲550 5.3.4未採取請求操作;要繼續發送消息,請登錄您的帳戶。通過Javamail發送電子郵件

它似乎創建的消息很好,但不允許它發送。任何想法爲什麼?

try 
    { 
    // Setup properties for e-mail server 
    Properties props = System.getProperties(); 
    props.put("mail.smtp.host", mConfig.getEmailHost()); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "587"); 

    // Get a Session object 
    Session session = Session.getInstance(props, new MyAuthenticator()); 
    session.setDebug(true); 
    Transport transport = session.getTransport("smtp"); 

    // Create message 
    MimeMessage message = new MimeMessage(session); 

    // Add the to/from fields 
    message.setFrom(new InternetAddress(mFromAddr, mFromName)); 
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(mToAddr)); 
    if (mCCAddrs != null) 
     { 
     for (int i=0; i<mCCAddrs.length; i++) 
      message.addRecipient(Message.RecipientType.CC, new InternetAddress(mCCAddrs[i])); 
     } 
    // Add Subject 
    message.setSubject(mEmailSubject); 

    // Setup multipart message for including the attachment 
    Multipart multipart = new MimeMultipart(); 

    // Create message body 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    messageBodyPart.setText(mEmailBody); 
    multipart.addBodyPart(messageBodyPart); 

    if (mAttachmentName != null) 
     { 
     // Create message attachment 
     BodyPart messageAttachmentPart = new MimeBodyPart(); 
     messageAttachmentPart.setDataHandler(new DataHandler(new ByteArrayDatasource(data))); 
     messageAttachmentPart.setFileName(mAttachmentName); 
     multipart.addBodyPart(messageAttachmentPart); 
     } 

    // Send message 
    message.setContent(multipart); 
    transport.connect(mConfig.getEmailHost(), mConfig.getEmailUser(), mConfig.getEmailPassword()); 
    transport.sendMessage(message, message.getAllRecipients()); 
    transport.close(); 
    } 
    catch (Exception ex) 
    { 
    ex.printStackTrace(); 
    throw new Exception("Failed to send e-mail: " + ex.getMessage()); 
    } 
    ` 

回答

4

您需要在您的身份驗證器中實際包含一些憑證信息。服務器向您表明它不允許匿名發送電子郵件。

new MyAuthenticator() 

     ^-------- fill this with some credentials 

需要注意的是,除非你的郵件服務器有一些特殊的要求,這是通常足夠使用standard password authenticator


編輯/更新

我接過細看在您的錯誤消息,根據您的反饋。在我看來,Hotmail要求您在使用它發送電子郵件之前登錄到您已設置的帳戶並進行驗證。在使用該帳戶之前,您可能需要使用Web瀏覽器登錄並檢查它們的激活鏈接。

+0

我已經有了,私有類MyAuthenticator擴展身份驗證 { 公衆的PasswordAuthentication的getPasswordAuthentication(){ 返回 新的PasswordAuthentication(mConfig.getEmailUser(),mConfig.getEmailPassword()); } } – AC3112 2013-02-20 12:36:07

+0

您是否爲'emailUser','emailPassword'調試了'mConfig'值。當驗證者被創建時它們是否存在? – Perception 2013-02-20 12:38:03

+0

我已經改變了我的每個會話的會話= Session.getInstance(道具,新javax.mail.Authenticator(){ 保護的PasswordAuthentication 的getPasswordAuthentication(){ 返回新的PasswordAuthentication(mConfig.getEmailUser(),mConfig.getEmailPassword() ); } });我仍然得到同樣的錯誤。 – AC3112 2013-02-20 12:43:48

1

嘗試下面的代碼。

import java.util.Date; 
    import java.util.Properties; 
    import javax.mail.Address; 
    import javax.mail.Message; 
    import javax.mail.PasswordAuthentication; 
    import javax.mail.Session; 
    import javax.mail.Transport; 
    import javax.mail.internet.AddressException; 
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeMessage; 

    public class TestJavaMail { 
    private String SMTP_PORT = "465"; 
    private String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 
    private String SMTP_HOST_NAME = "smtp.gmail.com"; 
    private String contentType = "text/html"; 
    private Properties smtpProperties; 
    public TestJavaMail(){ 
    initProperties(); 
    } 

    private void initProperties(){ 
    smtpProperties = new Properties(); 
    smtpProperties.put("mail.smtp.host", SMTP_HOST_NAME); 
    smtpProperties.put("mail.smtp.auth", "true"); 
    smtpProperties.put("mail.debug", "true"); 
    smtpProperties.put("mail.smtp.port", SMTP_PORT); 
    smtpProperties.put("mail.smtp.socketFactory.port", 
    SMTP_PORT); 
    smtpProperties.put ("mail.smtp.socketFactory.class", 
    SSL_FACTORY); 
    smtpProperties.put("mail.smtp.socketFactory.fallback", 
    "false"); 
    } 
    public static void main(String args[]) { 
    String to= "sendToMailAddress"; 
    String from ="sender email_id"; 
    String pwd = "Sender password"; 
    String subject= "Java Mail"; 
    String body= "Testing to write Doc on java mail."; 
    send(to, from, pwd , subject, body); 
    } 
    TestJavaMail.java Continued… 
    public static void send(String to, final String from 
    , final String pwd, String subject,String body){ 
    TestJavaMail tjm = new TestJavaMail(); 
    try 
    { 
    Properties props = tjm.getSmtpProperties() ; 
    // -- Attaching to default Session, or we could start 
    a new one -- 
    Session session = Session.getDefaultInstance(props, 
    new javax.mail.Authenticator() { 
    protected PasswordAuthentication 
    getPasswordAuthentication() { 
    return new PasswordAuthentication(from, pwd); 
    } 
    }); 
    // -- Create a new message -- 
    Message msg = new MimeMessage(session); 
    // -- Set the FROM and TO fields -- 
    msg.setFrom(new InternetAddress(from)); 
    msg.setRecipients(Message.RecipientType.TO, 
    InternetAddress.parse(to, false)); 
    // -- Set the subject and body text -- 
    msg.setSubject(subject); 
    msg.setText(body); 
    msg.setSentDate(new Date()); 
    // -- Send the message – 
    Transport.send(msg); 
    System.out.println("Message sent OK."); 
    } 
    catch (Exception ex) 
    { 
    ex.printStackTrace(); 
    } 
    } 
    public Properties getSmtpProperties() { 
    return smtpProperties; 
    } 
    public void setSmtpProperties(Properties smtpProperties) { 
    this.smtpProperties = smtpProperties; 
    } 
    } 
    } 

希望它能幫助你。