2011-12-27 136 views
0

当我试图发送一个样本邮件到Gmail帐户,得到com.sun.mail.smtp.SMTPAddressFailedException。以下是我写的代码...请有人可以帮我解决这个问题吗?不能发送邮件到Gmail邮箱帐户使用javamail api

public class MultiMimes { 

public static void main(String[] args) throws Exception{ 

    Properties props = System.getProperties(); 
    props.setProperty("mail.smtp.host", "mailservername"); 
    props.put("mail.debug", "true"); 
    Session session = Session.getDefaultInstance(props,null); 
    Message message = new MimeMessage(session); 
    try{ 
     message.setSubject("I am a multipart text/html email"); 
     Address toAddress =new InternetAddress("my gmail address"); 
     Address fromAddress =new InternetAddress("my organization address"); 
     message.setFrom(fromAddress); 
     message.addRecipient(Message.RecipientType.TO, toAddress); 

     MimeMultipart multipart1 = new MimeMultipart("alternative"); 

     // Create text message part 
     MimeBodyPart textPart = new MimeBodyPart(); 
     textPart.setContent("am text", "text/plain"); 
     textPart.setHeader("MIME-Version" , "1.0"); 
     textPart.setHeader("Content-Type" , textPart.getContentType()); 
      System.out.println("textPart.getContentType():"+textPart.getContentType()); 
     // Create html part 
     MimeBodyPart htmlPart = new MimeBodyPart(); 
     htmlPart.setContent("<html><body><b>am html</b></body></html>", "text/html"); 
     htmlPart.setHeader("MIME-Version" , "1.0"); 
     htmlPart.setHeader("Content-Type" , "text/html"); 
     System.out.println("htmlPart.getContentType():"+htmlPart.getContentType()); 
     //adding multiparts to message 
     multipart1.addBodyPart(htmlPart); 
     multipart1.addBodyPart(textPart); 
     message.setContent(multipart1); 

     //sending message 
     Transport.send(message); 
     System.out.println("mail sent successfully"); 
    }catch(AddressException ae){ 
     System.out.println("address exception"); 
     ae.printStackTrace(); 
    } 
    catch(MessagingException e){ 
     System.out.println("message exception"); 
     e.printStackTrace(); 
    } 


} 

当我使用的电子邮件ID从同一域中(例如:[email protected])取代Gmail ID,我收到的电子邮件。

+0

向我们展示完整的堆栈跟踪。 – 2011-12-27 16:18:33

+0

您的邮件服务器是否配置为允许将电子邮件发送到除本地域以外的其他域? – kosa 2011-12-27 16:19:33

+0

您是否检查过垃圾邮件文件夹? – 2011-12-27 16:19:41

回答

1

首先,部分这样的:

private static final String SMTP_HOST_NAME = "smtp.gmail.com"; 

Properties prop = new Properties(); 
prop.put("mail.smtp.host", SMTP_HOST_NAME); 
prop.put("mail.smtp.starttls.enable", "true"); 
prop.put("mail.smtp.port", "587"); 
prop.put("mail.smtp.auth", "true"); 

Authenticator auth = new SMTPAuthenticator(); 
Session session = Session.getDefaultInstance(prop, auth); 

session.setDebug(debug); 

这里是SMTPAuthenticator类:

import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 

/** 
    * SimpleAuthenticator is used to do simple authentication 
    * when the SMTP server requires it. 
    */ 

public class SMTPAuthenticator extends Authenticator 
{ 
    private static final String SMTP_AUTH_USER = "[email protected]"; 
    private static final String SMTP_AUTH_PASSWORD = "yourpassword"; 

    public PasswordAuthentication getPasswordAuthentication() 
    { 
    String username = SMTP_AUTH_USER; 
    String password = SMTP_AUTH_PASSWORD; 

    return new PasswordAuthentication(username, password); 
    } 

}

,并请不要改变你的地址,Gmail和解决任何事情,因为你的SMTP认证是由Gmail中完成的,所以这部分必须属于到Gmail。

希望这可能有帮助。

问候

+1

JavaMail FAQ有示例使用Gmail的代码。另外,使用Session.getInstance而不是Session.getDefaultInstance。 – 2011-12-27 23:41:02

+0

其实这是一个完整的工作代码,我在这里使用,为我的网站联系我的页面。有了这个,我可以发送电子邮件给任何人。问候 – 2011-12-28 04:40:22

+0

@BillShannon:Ahha,今天当我获得了一些关于'JavaMail'的更多知识时,我明白了你的评论的实际优势:-)。请原谅我,因为我很愚蠢,当时我太年轻了,根据我对JavaMail的了解,我写了这个答案。我会很快更新我的答案,因为我测试并清除了同样的疑问。 – 2013-01-16 18:17:27

1

感谢所有......至于我是新来satckoverflow ......我无法找到添加适当的意见准确的方式....

在这里,我并不想通过Gmail服务器发送邮件......我只是想发送给一些Gmail用户。使用此代码,我可以将邮件发送给组织内的任何人......但不能发送给组织外的人。

任何办法......我找到了解决我的问题,“预言等待”的建议...我作如下修改我的代码:

package com.trx; 

    import java.util.Properties; 

    import javax.activation.DataHandler; 
    import javax.activation.DataSource; 
    import javax.activation.FileDataSource; 
    import javax.mail.Address; 
    import javax.mail.Authenticator; 
    import javax.mail.BodyPart; 
    import javax.mail.Message; 
    import javax.mail.MessagingException; 
    import javax.mail.Multipart; 
    import javax.mail.Session; 
    import javax.mail.Transport; 
    import javax.mail.internet.AddressException; 
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeBodyPart; 
    import javax.mail.internet.MimeMessage; 
    import javax.mail.internet.MimeMultipart; 

    public class MultiMimes { 

public static void main(String[] args) throws Exception{ 

    Properties props = System.getProperties(); 
    props.setProperty("mail.smtp.host", "mymailserver"); 
    props.put("mail.smtp.port", "25"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.debug", "true"); 
    Authenticator auth = new SMTPAuthenticator(); 
    Session session = Session.getDefaultInstance(props, auth); 
    Message message = new MimeMessage(session); 
    try{ 
     message.setSubject("I am a multipart text/html email"); 
     Address toAddress =new InternetAddress("my gmail address"); 
     Address fromAddress =new InternetAddress("my organization address"); 
     message.setFrom(fromAddress); 
     message.addRecipient(Message.RecipientType.TO, toAddress); 
     MimeMultipart multipart1 = new MimeMultipart("alternative"); 

     // Create text message part 
     MimeBodyPart textPart = new MimeBodyPart(); 
     textPart.setContent("am text", "text/plain"); 
     textPart.setHeader("MIME-Version" , "1.0"); 
     textPart.setHeader("Content-Type" , textPart.getContentType()); 
      System.out.println("textPart.getContentType():"+textPart.getContentType()); 
     // Create html part 
     MimeBodyPart htmlPart = new MimeBodyPart(); 
     htmlPart.setContent("<html><body><b>am html</b></body></html>", "text/html"); 
     htmlPart.setHeader("MIME-Version" , "1.0"); 
     htmlPart.setHeader("Content-Type" , "text/html"); 
     System.out.println("htmlPart.getContentType():"+htmlPart.getContentType()); 
     //adding multiparts to message 

     multipart1.addBodyPart(textPart); 
     multipart1.addBodyPart(htmlPart); 
     message.setContent(multipart1); 

     //sending message 
     Transport.send(message); 
     System.out.println("mail sent successfully"); 
    }catch(AddressException ae){ 
     System.out.println("address exception"); 
     ae.printStackTrace(); 
    } 
    catch(MessagingException e){ 
     System.out.println("message exception"); 
     e.printStackTrace(); 
    } 


     } 

    } 

,我用他所提供的同一类SMTPAuthenticator .. 。现在我能够将邮件从我的组织邮件服务器发送到任何电子邮件ID ...再次感谢。

+0

很高兴知道它为你工作。 +1。问候 – 2011-12-28 10:03:19

相关问题