我有以下代码
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
我要求将电子邮件发送到有效的电子邮件地址并标记所有与正确域名无效的电子邮件,并查找错误域名的电子邮件。在另一台机器上测试的相同代码并没有抛出异常
感谢任何帮助
答案 0 :(得分:0)
在您的邮件中添加电子邮件地址之前。您必须检查它是否是有效地址。例如,通过使用此正则表达式:
^ [_ A-ZA-Z0-9 - \ +] +(\ [_ A-ZA-Z0-9 - ]。+) @ [A-ZA-Z0-9 - ] +(\。 [A-ZA-Z0-9] +)(\ [A-ZA-Z] {2,})$;
还有一个:
&#34; ^ [A-Z0-9 ._%+ - ] + @ [A-Z0-9 .-] + \ [AZ] {2,6-} $&#34;
除此之外,地址格式正确。
此处另一篇文章展示了如何验证电子邮件地址: Java regex email