2011-12-01 102 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和其他示例。 –