Java邮件 - 将电子邮件发送到组电子邮件ID时没有此类用户例外

时间:2016-12-01 07:37:39

标签: java email javamail

我有一个群组电子邮件ID,而我发送邮件到群组电子邮件ID与OUTLOOK它工作正常。当我尝试将它从jAVA MAIL发送到同一组电子邮件ID时,它会抛出异常没有这样的用户。我在哪里使用相同的SMTP主机。

例外:

javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
        com.sun.mail.smtp.SMTPAddressFailedException: 550 TestGropuEMAIL@sample.net... No such user

        at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1873)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120)
        at javax.mail.Transport.send0(Transport.java:195)
        at javax.mail.Transport.send(Transport.java:124)
        at com.ey.mail.SendEmail.sendMail(SendEmail.java:77)
        at com.ey.main.RunAutoMail.main(RunAutoMail.java:30)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 TestGropuEMAIL@sample.net... No such user

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

代码:

  Message message = new MimeMessage(session);
  message.setFrom(new InternetAddress(from));
  message.setHeader("X-Priority", "1");
  message.setRecipients(Message.RecipientType.TO,
  InternetAddress.parse("TestGropuEMAIL@sample.net"));
  BodyPart messageBodyPart = new MimeBodyPart();
  messageBodyPart.setContent(content,"text/html");
  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(messageBodyPart);
  messageBodyPart = new MimeBodyPart();
  String filename = "Test.XLSB";
  DataSource source = new FileDataSource(filename);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(filename);
  multipart.addBodyPart(messageBodyPart);
  message.setContent(multipart);
  Transport.send(message);

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这意味着邮件地址无效,sender addressreceiver address。我之前使用过此项目,请在此处发布我的代码以供参考:

public boolean sendMail(Properties pro) {
    String host = pro.getProperty("host");
    String userName = pro.getProperty("userName");
    String passWord = pro.getProperty("passWord");
    String subject = pro.getProperty("subject");
    String fileName = pro.getProperty("fileName");
    String receivers = pro.getProperty("receivers");
    String mailContent = pro.getProperty("mailContent");

    System.out.println(mailContent);
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "25");
    props.put("mail.debug", "true");

    MyAuthenticator myauth = new MyAuthenticator(userName, passWord);
    // Get session
    try {
        Session session = Session.getDefaultInstance(props, myauth);
        // Define message
        MimeMessage message = new MimeMessage(session);

        List list = new ArrayList();
        String[] toList = receivers.split(",");// mutilple mail receivers
        for (int i = 0; i < toList.length; i++) {
            list.add(new InternetAddress(toList[i]));
        }
        InternetAddress[] toAddress = (InternetAddress[]) list.toArray(
                new InternetAddress[list.size()]);
        InternetAddress fromAddress = new InternetAddress(userName, "",
                "UTF-8");
        message.setFrom(fromAddress);
        message.setSentDate(new Date());
        message.setRecipients(Message.RecipientType.TO, toAddress);
        message.setSubject(subject);

        Multipart multipart = new MimeMultipart();

        // mail content
        BodyPart contentBodyPart = new MimeBodyPart();
        contentBodyPart.setContent(mailContent, "text/plain;charset=utf-8");
        multipart.addBodyPart(contentBodyPart);

        // mail attachment
        BodyPart messageBodyPart = new MimeBodyPart();
        FileDataSource source = new FileDataSource(
                pro.getProperty("path") + fileName);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(MimeUtility.encodeText(fileName));
        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);
        message.saveChanges();

        Transport transport = session.getTransport("smtp");
        transport.connect(host, userName, passWord);
        transport.sendMessage(message, message.getAllRecipients());
    }
    catch (Exception e) {
        e.printStackTrace();
        logger.error("Mail Send exception:", e);
        return false;
    }
    return true;
}