首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用java.mailx查找无效和未发送的电子邮件地址

使用java.mailx查找无效和未发送的电子邮件地址
EN

Stack Overflow用户
提问于 2016-03-30 09:47:30
回答 1查看 1.2K关注 0票数 0

我有以下代码

代码语言:javascript
复制
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());

} 

`

如果我尝试上面的代码只有有效的电子邮件,它是发送电子邮件,但当我开始包括其他错误的域名和无效的电子邮件地址,所有的电子邮件失败。以下是调试信息

代码语言:javascript
复制
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

我的要求,发送电子邮件到有效的电子邮件地址和标志所有的电子邮件是无效的,与适当的域名,也找出埃米姆斯与错误的领域。在另一台机器上测试的相同代码根本不抛出异常。

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2016-03-30 09:59:32

在向邮件中添加电子邮件之前。你必须检查它是否是一个有效的地址。例如,通过使用这个regex:

( ^_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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36305168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档