com.sun.mail.smtp.SMTPSendFailedException:530 5.7.1客户端未经过身份验证

时间:2017-11-24 09:01:26

标签: java email javamail

我收到'com.sun.mail.smtp.SMTPSendFailedException:530 5.7.1客户端未经过身份验证'错误总是,有人可以告诉我在我的代码中我做错了什么吗?

Properties mailprops = new Properties();
mailprops.setProperty("mail.transport.protocol", "smtp");
mailprops.setProperty("mail.smtp.host", "MyHost");
mailprops.setProperty("mail.smtp.user", "UserName");
mailprops.setProperty("mail.smtp.password", "Password");

Session mailSession = Session.getDefaultInstance(mailprops, null);
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(mySubject);
message.addRecipient(To);
message.addFrom(from address);

try{
Transport.send(message);
}catch (SendFailedException sfe) {
}catch (MessagingException mex) {
}

4 个答案:

答案 0 :(得分:2)

尝试以下方法, 提供Authenticator对象以创建session对象

public class MailWithPasswordAuthentication {
public static void main(String[] args) throws MessagingException {
new MailWithPasswordAuthentication().run();
}
private void run() throws MessagingException {
Message message = new MimeMessage(getSession());
message.addRecipient(RecipientType.TO, new InternetAddress("to@example.com"));
message.addFrom(new InternetAddress[] { new InternetAddress("from@example.com") });
message.setSubject("the subject");
message.setContent("the body", "text/plain");
Transport.send(message);
}
private Session getSession() {
Authenticator authenticator = new Authenticator();
Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.host", "mail.example.com");
properties.setProperty("mail.smtp.port", "25");
return Session.getInstance(properties, authenticator);
}
private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator() {
String username = "auth-user";
String password = "auth-password";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
}

答案 1 :(得分:0)

尝试类似的东西:

        Transport trans = sess.getTransport("smtp");
        try {
            String host = sess.getProperty("mail.smtp.host");
            String port = sess.getProperty("mail.smtp.port");
            String user = sess.getProperty("mail.smtp.user");
            String password = sess.getProperty("mail.smtp.password");
            int prt = port == null ? -1 : Integer.parseInt(port);
            trans.connect(host, prt, user, password);
            trans.sendMessage(message,
                    message.getRecipients(Message.RecipientType.TO));
        } finally {
            trans.close();
        }

更新:

或者更确切地说:

int prt = port == null ? 25 : Integer.parseInt(port);

答案 2 :(得分:0)

  1. 启用身份验证。
  2. 启用STARTTLS。
  3. 创建Authenticator对象以传递Session.getInstance参数。
  4. 示例代码:

    props.put(" mail.smtp.auth"," true"); props.put(" mail.smtp.starttls.enable"," true");

            Authenticator auth = new Authenticator() {
                //override the getPasswordAuthentication method
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(fromEmail, password);
                }
            };
            Session session = Session.getInstance(props, auth);
    

答案 3 :(得分:0)

don't need an Authenticator正如一些答案所暗示的那样。没有mail.smtp.password属性。拨打Transport.send method that takes a username and password