使用java邮件api发送电子邮件失败
绝望地试图给自己发送电子邮件:使用java邮件api发送电子邮件失败,java,jakarta-mail,Java,Jakarta Mail,绝望地试图给自己发送电子邮件: 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;
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和没有@web.de)。密码是我正常登录邮件帐户时使用的密码。
不要获取它使用smtp用户和密码设置以下属性
props.put("mail.smtp.user", "myuser");
props.put("mail.smtp.password", "mypwd");
参考:
使用您的用户名和密码创建如下javax.mail.Session对象
密码:-
参考教程:-在过去,我解决了类似的问题,添加了以下内容:
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”。这可能有助于您调用Transport.send静态方法,该方法将忽略您创建并连接的传输实例。改为调用sendMessage方法,或者跳过创建自己的传输实例,并调用接受用户名和密码的静态send方法
这是其中一个。我已经试过了。我试了很多。很多根据教程。什么都不管用,然后像阿米特提到的那样试试
Session Session=Session.getInstance(props,new javax.mail.Authenticator(){protected PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication(username,password);}})代码>有。也没有mail.smtp.password属性。
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);