2011-12-01 106 views
0

我想通過Java內的電子郵件發送文本消息。通過電子郵件在Java中發送SMS文本錯誤

當我通過Outlook使用以下方法執行此操作時,它工作得非常好。

SMS through Email Guide

然而,當我嘗試在使用JavaMail Java中,我得到:

java.mail.SendFailedException:無效地址

Properties props = new Properties(); 
props.put("mail.smtp.host", "mail.xxxx.org"); 
props.put("mail.transport.protocol", "smtp"); 
Session session = Session.getInstance(props, null); 

String msgBody = "You have a new message."; 

try { 
     javax.mail.Message msg = new MimeMessage(session); 
     msg.setFrom(new InternetAddress("[email protected]", "Email Name")); 
     msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("[email protected]", "Phone")); 


     msg.setSubject("New Message."); 
     msg.setContent(msgBody, "text/plain"); 
     Transport.send(msg); 

} 
catch (Exception e) { 
     // ...   
} 

當我改變行

msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("[email protected]", "Phone")); 

msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("[email protected]", "Phone")); 

它工作正常。

任何幫助或提示將不勝感激。

謝謝。

回答

1

我建議檢查SMTP。通常SMTP允許您在自己的域中發送電子郵件,即使沒有登錄,但他們也希望登錄在外部發送電子郵件。

here(http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/)你可以找到一個關於如何從Gmail發送電子郵件的例子,這需要認證。

+0

在[JavaMail FAQ](http://www.oracle.com/technetwork/java/javamail/faq/index.html)中也有Gmail和其他示例。 –