我正在使用Java EE webApplication,我希望向我的客户发送电子邮件。 所以我添加了mail.jar和activation.jar,我尝试了这个简单的代码:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendHTML {
static Properties mailServerProperties;
static Session getMailSession;
static MimeMessage generateMailMessage;
public static void main(String args[]) throws AddressException, MessagingException {
generateAndSendEmail();
System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email..");
}
public static void generateAndSendEmail() throws AddressException, MessagingException {
//Step1
System.out.println("\n 1st ===> setup Mail Server Properties..");
mailServerProperties = System.getProperties();
mailServerProperties.put("mail.smtp.port", "587"); // TLS Port
mailServerProperties.put("mail.smtp.auth", "true"); // Enable Authentication
mailServerProperties.put("mail.smtp.starttls.enable", "true"); // Enable StartTLS
System.out.println("Mail Server Properties have been setup successfully..");
//Step2
System.out.println("\n\n 2nd ===> get Mail Session..");
getMailSession = Session.getDefaultInstance(mailServerProperties, null);
generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("Recipient@gmail.com"));
generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("Recipient@gmail.com"));
generateMailMessage.setSubject("Greetings from me..");
String emailBody = "Test email by me JavaMail API example. " + "<br><br> Regards, <br>Admin";
generateMailMessage.setContent(emailBody, "text/html");
System.out.println("Mail Session has been created successfully..");
//Step3
System.out.println("\n\n 3rd ===> Get Session and Send mail");
Transport transport = getMailSession.getTransport("smtp");
// Enter your correct gmail UserID and Password
transport.connect("smtp.gmail.com", "myusername@gmail.com", "mypassword");
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
}
}
因此Step1和Step2成功运行,但Step3生成身份验证例外:
1st ===> setup Mail Server Properties..
Mail Server Properties have been setup successfully..
2nd ===> get Mail Session..
Mail Session has been created successfully..
3rd ===> Get Session and Send mail
Exception in thread "main" javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at entity.SendHTML.generateAndSendEmail(SendHTML.java:53)
at entity.SendHTML.main(SendHTML.java:24)
请有人帮我解决问题吗?
答案 0 :(得分:1)
首先,there is nothing named "JEE",您的意思是“Java EE”。
如果您使用的是Java EE,则不需要mail.jar和activation.jar,它们已经是每个Java EE服务器的一部分。
您将要在common mistakes和how to debug JavaMail applications上阅读这些JavaMail常见问题解答条目。
AuthenticationFailedException表示服务器不喜欢您的用户名和密码。 您可能遇到了here所描述的问题。