我使用SMTP错误发送电子邮件。我收到身份验证失败。用户名和密码是正确的。我做错了什么。
错误日志
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;
public class EmailSender{
public static void main(String args[]) {
String to = "ssss@xxx.om"; // sender email
String from = "dddd@xxx.com"; // receiver email
String host = "dkdkdd.xxx.com"; // mail server host
String login="dkkdkd";
String pass="dkkdkd";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.user", login);
properties.setProperty("mail.smtp.password", pass);
properties.setProperty("mail.smtps.ssl.enable", "true");
// properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties); // default session
try {
MimeMessage message = new MimeMessage(session); // email message
message.setFrom(new InternetAddress(from)); // setting header fields
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Test Mail from Java Program"); // subject line
// actual mail body
message.setText("You can send mail from Java program by using");
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, login, pass);
Transport.send(message);
System.out.println("Email Sent successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
错误是
DEBUG SMTP:AUTH NTLM失败 线程" main"中的例外情况javax.mail.AuthenticationFailedException:535 5.7.3身份验证失败
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
答案 0 :(得分:4)
有同样的问题。这是您收到的MS Exchange错误。您可能不被允许使用您的电子邮件通过中继发送电子邮件。 Exchange服务器的管理员需要授予执行此操作的权限。
它与Java端的配置问题无关。
答案 1 :(得分:0)
您好我以前遇到过同样的问题。 所以要解决它我必须连接我的outlook或exchage的webmail,我注意到这些连接被服务器停止,所以在内部我确认这些交易是我的。 因此,在我的情况下,通常每2个月也会这样做。
问题不在代码中。出现问题是因为我认为邮箱的配置有问题。
答案 2 :(得分:0)
通过在Outlook邮件管理员的Exchange控制面板(ECP)中启用发件人电子邮件帐户的IMAP,可以解决同一问题。
答案 3 :(得分:0)
答案 4 :(得分:-1)
看起来你在如何处理会话部分的问题......
尝试这样做:
private Properties emailPorperties;
... ...
emailPorperties = new Properties();
emailPorperties.put("mail.smtp.host", "your host");
emailPorperties.put("mail.smtp.socketFactory.port", "your port");
emailPorperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
emailPorperties.put("mail.smtp.auth", "true");
emailPorperties.put("mail.smtp.port", "your port");
emailPorperties.put("mail.smtp.ssl.enable", "true");
emailSession = Session.getInstance(emailPorperties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Authenticating");
return new PasswordAuthentication(USER_NAME, PASSWORD);
}
});