使用 java mail-api 发送电子邮件失败
sending email using java mail-api fails
拼命想给自己发邮件:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.web.de");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props);
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("myveryownemail@web.de"));
msg.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("myveryownemail@web.de")});
msg.setSubject("whatever");
msg.setContent("whatever", "text/plain");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.web.de", 587, "myveryownemail", "myveryownandcorrectpassword");
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
我得到
javax.mail.AuthenticationFailedException: 连接失败,没有指定密码?
但用户名和密码是绝对正确的(尝试使用和不使用@web.de 的用户名)。密码是我用于正常登录我的邮件帐户的密码。
不明白
使用您的 smtp 用户和密码设置以下属性
props.put("mail.smtp.user", "myuser");
props.put("mail.smtp.password", "mypwd");
参考:http://www.tutorialspoint.com/java/java_sending_email.htm
Create the javax.mail.Session object like below, with your username and
password:-
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
参考教程:- http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
过去我解决了类似的问题添加这个:
if ("true".equals(emailConfig.get("mail.smtp.auth"))) {
session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailConfig.get("mail.smtp.user"), emailConfig.get("mail.smtp.password"));
}
});
}
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(emailFromAddress));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailToAdrress));
msg.setSubject(mailSubject);
msg.setText(mailMessageBody);
Transport.send(msg);
会话是 javax.mail.Session
希望您在项目中包含 "mail.jar" 和 "activation.jar"。这个link可能对你有帮助
您正在调用 Transport.send 静态方法,它会忽略您创建和连接的 Transport 实例。请改为调用 sendMessage 方法,或跳过创建您自己的 Transport 实例并调用采用用户名和密码的静态发送方法。
这是 JavaMail common mistakes 之一。
拼命想给自己发邮件:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.web.de");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props);
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("myveryownemail@web.de"));
msg.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("myveryownemail@web.de")});
msg.setSubject("whatever");
msg.setContent("whatever", "text/plain");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.web.de", 587, "myveryownemail", "myveryownandcorrectpassword");
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
我得到
javax.mail.AuthenticationFailedException: 连接失败,没有指定密码?
但用户名和密码是绝对正确的(尝试使用和不使用@web.de 的用户名)。密码是我用于正常登录我的邮件帐户的密码。
不明白
使用您的 smtp 用户和密码设置以下属性
props.put("mail.smtp.user", "myuser");
props.put("mail.smtp.password", "mypwd");
参考:http://www.tutorialspoint.com/java/java_sending_email.htm
Create the javax.mail.Session object like below, with your username and password:-
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
参考教程:- http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
过去我解决了类似的问题添加这个:
if ("true".equals(emailConfig.get("mail.smtp.auth"))) {
session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailConfig.get("mail.smtp.user"), emailConfig.get("mail.smtp.password"));
}
});
}
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(emailFromAddress));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailToAdrress));
msg.setSubject(mailSubject);
msg.setText(mailMessageBody);
Transport.send(msg);
会话是 javax.mail.Session
希望您在项目中包含 "mail.jar" 和 "activation.jar"。这个link可能对你有帮助
您正在调用 Transport.send 静态方法,它会忽略您创建和连接的 Transport 实例。请改为调用 sendMessage 方法,或跳过创建您自己的 Transport 实例并调用采用用户名和密码的静态发送方法。
这是 JavaMail common mistakes 之一。