• 2022-09-30
宇哥博客 后端开发 Java使用mail.jar发送邮件,异常550 Mailbox not found or access denied

Java使用mail.jar发送邮件,异常550 Mailbox not found or access denied

Java使用mail.jar发送邮件,发件邮箱用的是腾讯企业邮箱,当收件人或者抄送人中有邮箱地址(腾讯企业邮箱)不存在的情况时,程序就会异常。

javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 Mailbox not found or access denied

可以获取到无效的邮箱地址,去掉无效的,再次发送邮件。

public static void main(String[] args)
{
    String to = "ad@ygbks.com";
    String cc = "12345@ygbks.com,admin@ygbks.com,user@ygbks.com,123cvd12@ygbks.com";
    String title = "标题";
    String content = "内容";
    try
    {
        sendEmail(to, title, content, cc);
    }
    catch(SendFailedException e2)
    {
        e2.printStackTrace();
        Address invalidAddresses[] = e2.getInvalidAddresses();
        for(int i = 0; i < invalidAddresses.length; i++)
        {
            System.out.println("===无效的邮箱地址==" + invalidAddresses[i]);
            cc = cc.replaceAll(invalidAddresses[i].toString(), "");
        }
        cc = cc.replaceAll(",{1,}", ",");
        cc = cc.replaceAll("^,", "");
        cc = cc.replaceAll(",$", "");
        System.out.println("抄送地址:" + cc);
        //再发送一次...
        sendEmail(to, title, content, cc);
        /*
			//这个没啥意义
			Address validUnsentAddresses[]=e2.getValidUnsentAddresses();
			for (int i = 0; i < validUnsentAddresses.length; i++) {
				System.out.println("===未发送成功的(有效)邮箱地址==" + validUnsentAddresses[i]);
			}
			*/
    }
    catch(AddressException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch(MessagingException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
ad
本文来自网络,不代表本站立场,转载请注明出处。http://www.ygbks.com/1161.html

发表评论

您的电子邮箱地址不会被公开。

返回顶部