【问题标题】:Finding invalid and unsent email addresses using java.mailx使用 java.mailx 查找无效和未发送的电子邮件地址
【发布时间】:2016-03-30 09:47:30
【问题描述】:

我有以下代码

boolean debug = true;

// Set the host smtp address
Properties props = new Properties();
props.put( "mail.smtp.host", "xxxxx" );


// Create some properties and get the default Session
Session session = Session.getDefaultInstance( props, new Authenticator(){
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("preddy@indsci.com", "<PWD>");
    }
});


session.setDebug( debug );



// Create a message
MimeMessage message_= new MimeMessage( session );

message_.setSubject( "Test Sub");
message_.setContent( "Hello", "text/plain" );


InternetAddress from = new InternetAddress( "xxxx<yyyy@zzzz>" );
InternetAddress replyTo = new InternetAddress( "xxxx<yyyy@zzzz>" );

  InternetAddress[] to = new InternetAddress[3];
  to[0] = new InternetAddress( "valid addredd" ); 
  to[1] = new InternetAddress( "Wrong Domain email" );
  to[2] = new InternetAddress( "Prasad Wrong Email " );


  try
  {
     Transport.send( message_ );
     System.out.println("*********************** No exception ");
  }
   catch (SendFailedException ex)
  {
  // Build the list of invalid addresses.
  List<Address> invalidEmailList = new ArrayList<Address>();

  // first, get the email list from the exception
  if (ex.getInvalidAddresses() != null
      && ex.getInvalidAddresses().length > 0)
  {
    invalidEmailList.addAll( Arrays.asList( ex.getInvalidAddresses() ) );
  }

  if (ex.getValidUnsentAddresses() != null
          && ex.getValidUnsentAddresses().length > 0)
      {
        invalidEmailList.addAll( Arrays.asList( ex.getValidUnsentAddresses()      ) );
      }

    // then tack on the invalid email addresses we passed in.
    if (invalidAddresses_.size() > 0)
    {
      invalidEmailList.addAll( invalidAddresses_ );
    }

    System.out.println(invalidEmailList);

    ex.printStackTrace();

  // Throw the exception up to higher handlers
  //throw ex;

  } catch(javax.mail.MessagingException mx) {

    System.out.println(mx.getMessage());

  } catch (Exception ex) {

    System.out.println(ex.getMessage());

} 

`

如果我仅使用有效电子邮件尝试上述代码,它正在发送电子邮件,但是当我开始包含其他错误域和无效电子邮件地址时,所有电子邮件都失败了。这是调试信息

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning            javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "xxxxx", port 25, isSSL   false
220 xxxxxx Microsoft ESMTP MAIL Service ready at Wed, 30     Mar 2016 05:36:58 -0400
DEBUG SMTP: connected to host "XXXXXXX", port: 25


250-xxxxxxx- Hello [172.17.36.139]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM LOGIN
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250-XRDST
250 XSHADOW
DEBUG SMTP: Found extension "SIZE", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "NTLM LOGIN"
DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "XEXCH50", arg ""
DEBUG SMTP: Found extension "XRDST", arg ""
DEBUG SMTP: Found extension "XSHADOW", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<preddy@predictivesolutions.com>
250 2.1.0 Sender OK
RCPT TO:<preddy@indsci.com>
250 2.1.5 Recipient OK
RCPT TO:<preddy@indsci1.com>
550 5.7.1 Unable to relay
RCPT TO:<preddy123@indsci.com>
250 2.1.5 Recipient OK
DEBUG SMTP: Valid Unsent Addresses
DEBUG SMTP:   Prasad Reddy <preddy@indsci.com>
DEBUG SMTP:   Prasad Wrong Email <preddy123@indsci.com>
DEBUG SMTP: Invalid Addresses
DEBUG SMTP:   Prasad Wrong Domain <preddy@indsci1.com>
DEBUG SMTP: Sending failed because of invalid destination addresses
RSET
250 2.0.0 Resetting
DEBUG SMTP: MessagingException while sending, THROW: 
 javax.mail.SendFailedException: Invalid Addresses;
 nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1862)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.dbo2.pipeline.util.MailHandler.sendMessage(MailHandler.java:583)
at     com.dbo2.pipeline.util.MailHandler.postMailImplementation(MailHandler.java:321)
at com.dbo2.pipeline.util.MailHandler.postMail(MailHandler.java:165)
at com.dbo2.pipeline.util.MailHandler.main(MailHandler.java:568)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to    relay

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1715)
... 7 more
QUIT
221 2.0.0 Service closing transmission channel
   [Prasad Wrong Domain <preddy@indsci1.com>, Prasad Reddy <preddy@indsci.com>,       Prasad Wrong Email <preddy123@indsci.com>]
  *********************** Exception***********************
 javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1862)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.dbo2.pipeline.util.MailHandler.sendMessage(MailHandler.java:583)
at    com.dbo2.pipeline.util.MailHandler.postMailImplementation(MailHandler.java:321)
at com.dbo2.pipeline.util.MailHandler.postMail(MailHandler.java:165)
at com.dbo2.pipeline.util.MailHandler.main(MailHandler.java:568)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1715)
... 7 more

我要求将电子邮件发送到有效的电子邮件地址,并标记所有具有正确域的无效电子邮件,并找出具有错误域的 emils。在另一台机器上测试的相同代码根本不会抛出异常

感谢任何帮助

【问题讨论】:

    标签: java email


    【解决方案1】:

    在您的邮件中添加电子邮件地址之前。你必须检查它是否是一个有效的地址。例如通过使用这个正则表达式:

    ^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)@[A-Za-z0-9-]+(\. [A-Za-z0-9]+)(\.[A-Za-z]{2,})$;

    还有一个:

    "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$"

    这样可以确保地址格式正确。

    这里还有一篇文章展示了如何验证电子邮件地址: Java regex email

    【讨论】:

    • 我已经有了验证码。所有电子邮件均有效。我有像 prasadreddykalki@gmail.com 这样的地址,这是有效的电子邮件。我添加了其他电子邮件地址,例如 prasadreddykalki@gmail123.com、prasadreddy12345678900000@gmail.com。所有都是用于验证的有效电子邮件。当我通过上述所有电子邮件时,一切都会失败。如果我只包含第一个,那么它正在发送电子邮件
    • 不是基本验证,而是从 SendFailedException 中查找无效邮件
    • 我使用了“mail.smtp.partialSend”属性,然后它开始向有效的电子邮件发送电子邮件。
    • 各位,知道为什么它不能在我朋友的本地机器上运行吗?
    猜你喜欢
    • 1970-01-01
    • 2020-09-01
    • 2010-11-15
    • 1970-01-01
    • 2014-07-20
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多