线程" main"中的例外情况javax.mail.SendFailedException:无效的地址;

时间:2015-03-17 09:48:06

标签: java email

我试图从我的SMTP邮件服务器发送邮件。在使用基于弹簧的代码时,它工作得很好。但是当我试图通过java代码发送时,这是我的java代码:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Test1 {

  Properties emailProperties= new Properties();
  Session mailSession;
  MimeMessage emailMessage;

  public static void main(String args[]) throws AddressException,
      MessagingException {

    Test1 javaEmail = new Test1();
    javaEmail.setMailServerProperties();
    javaEmail.createEmailMessage();
    javaEmail.sendEmail();
  }

  public void setMailServerProperties() {

    String emailPort = "587";
    String host = "mysmtphost";
    emailProperties.put("mail.smtp.host", host);
    emailProperties.put("mail.smtp.socketFactory.port", "465");
    emailProperties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    emailProperties.put("mail.smtp.auth", "true");
    emailProperties.put("mail.smtp.port", emailPort);

  }

  public void createEmailMessage() throws AddressException,
      MessagingException {
    String[] toEmails = { "tomail.com" };
    String emailSubject = "Java Email";
    String emailBody = "This is an email sent by JavaMail api.";


    mailSession = Session.getDefaultInstance(emailProperties, null);
    System.out.println("mailSession"+mailSession);
    emailMessage = new MimeMessage(mailSession);

    for (int i = 0; i < toEmails.length; i++) {
      emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
    }

    emailMessage.setSubject(emailSubject);
    emailMessage.setContent(emailBody, "text/html");//for a html email
    //emailMessage.setText(emailBody);// for a text email

  }

  public void sendEmail() throws AddressException, MessagingException {

    String emailHost = "mysmtphost";
    String fromUser = "myusername";
    String fromUserEmailPassword = "mypassword";

    Transport transport = mailSession.getTransport("smtp");

    transport.connect(emailHost, fromUser, fromUserEmailPassword);
    System.out.println("connected to server");
    transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
    transport.close();
    System.out.println("Email sent successfully.");
  }
}

当我作为java应用程序运行时,我得到以下异常:

Exception in thread "main" javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    class javax.mail.SendFailedException: 554 5.7.1 <unknown[172.18.140.85]>: Client host rejected: Access denied
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:926)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:389)
at com.cmc.test.Test1.sendEmail(Test1.java:76)
at com.cmc.test.Test1.main(Test1.java:28)

我在控制台中获取“连接到服务器”的行。

请帮助我,如果有人遇到过这类问题。 等待回复。

由于

2 个答案:

答案 0 :(得分:0)

信息很清楚:

class javax.mail.SendFailedException: 554 5.7.1 <unknown[172.18.140.85]>: Client host rejected: Access denied

您必须添加身份验证信息。

答案 1 :(得分:0)

感谢您的快速回复。 但我也尝试了身份验证信息。这是完整的代码:

public class TestMail {

    public static void main(String[] args) {

        final String username = "myuser";
        final String password = "mypassword";

        boolean debug = false;

        Properties props = new Properties();
          props.put("mail.smtp.host", "myhost");
          props.put("mail.smtp.socketFactory.port", "465");
          props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.port", "587");


        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(myuser, mypassword);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("frommail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("tomail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

添加上述代码时获得相同的异常