当我尝试将示例邮件发送到 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 时,我会收到电子邮件。
最佳答案
首先,您需要将代码部分更改为:
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 = "youremail@gmail.com";
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。
希望能有所帮助。
问候
关于java - 无法使用 javamail api 将邮件发送到 gmail 帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8646754/