当我试图发送一个样本邮件到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,我收到的电子邮件。
向我们展示完整的堆栈跟踪。 – 2011-12-27 16:18:33
您的邮件服务器是否配置为允许将电子邮件发送到除本地域以外的其他域? – kosa 2011-12-27 16:19:33
您是否检查过垃圾邮件文件夹? – 2011-12-27 16:19:41