javax.mail.SendFailedException发送邮件时无效的地址

时间:2016-12-08 19:33:59

标签: java email

发送使用java的邮件时遇到问题这是我的代码

package callcentersystem;

public class Email {

    private String topic = "my test";
    private String myMassage = "nothinge";

    public void untitledMethod(String to) {
        String host = "my ip adresse";
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);
        Session session = Session.getDefaultInstance(properties);
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(utils.Constants.From));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(topic);
            message.setText(myMassage);
            Transport transport = session.getTransport("smtp");
            transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            System.out.println("the massage didnt send");
            throw new RuntimeException(mex);
        }
    }
}

这是我的撞击按摩

the massage didnt send
Exception in thread "main" java.lang.RuntimeException:        javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 530 SMTP authentication is required.

at callcentersystem.Email.untitledMethod(Email.java:33)
at callcentersystem.CallCenterSystem.main(CallCenterSystem.java:10)
Caused by: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 530 SMTP authentication is    required.

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1446)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:736)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at callcentersystem.Email.untitledMethod(Email.java:29)
... 1 more
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 530 SMTP authentication is required.

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1344)
... 5 more

我尝试了各种方法来解决这个问题,但它有点工作,我设置了一个hmailserver管理员,我使用它解决了我的localhost问题

2 个答案:

答案 0 :(得分:0)

显然,需要此SMTP服务器的身份验证凭据,因为嵌套异常很明显:530 SMTP authentication is required

您需要为会话设置Authenticator实例:

...
Authenticatior authenticator =
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(
                  "youruser@yourhost.com", "yourpassword");
            }
         };
Session session = Session.getDefaultInstance(properties, authenticator);
...

youruser@yourhost.com yourpassword 更改为您尝试发送邮件的电子邮件服务器上的实际凭据。您需要此服务器上的有效帐户,因为它需要身份验证。

作为与问题无关的旁注,您确定要使用Session.getDefaultInstance而不是Session.getInstance吗?阅读有关影响的文档。

答案 1 :(得分:0)

我通过此代码解决了这个问题

public void untitledMethod(ArrayList to, String password, String message) {
    String host = "smtp.gmail.com";
    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", utils.Constants.From);
    props.put("mail.smtp.password", password);
    props.put("mail.smtp.port", 587);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(utils.Constants.From, password);
        }
    });
    MimeMessage mimeMessage = new MimeMessage(session);
    try {
        mimeMessage.setFrom(new InternetAddress(utils.Constants.From));
        InternetAddress[] toAddress = new InternetAddress[to.size()];
        for (int i = 0; i < to.size(); i++) {
            toAddress[i] = new InternetAddress((String) to.get(i));
        }

        for (InternetAddress toAddres : toAddress) {
            mimeMessage.addRecipient(Message.RecipientType.TO, toAddres);
            mimeMessage.setSubject("Clinic Support");
            mimeMessage.setText(message);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, 587, utils.Constants.From, password);
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            transport.close();
        }

    } catch (MessagingException me) {
        me.printStackTrace();
    }
}

但您必须确保使用您的电子邮件和密码发送邮件