当我尝试向 gmail 帐户发送示例邮件时,收到 com.sun.mail.smtp.SMTPAddressFailedException。以下是我编写的代码...请有人帮我解决这个问题吗?
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(例如:someone@test.com)而不是 gmail id 时,我会收到电子邮件。