javax.mail 报错 501 mail from address must be same as authorization user 解决方法

从一个邮箱发送邮件

从一个邮箱发送邮件报错 501 mail from address must be same as authorization user,是由于获取Session时的账号和Message中设置的邮箱地址setFrom不一致引起的
代码如下:

/**
 * 服务器邮箱登录验证
 */
class MailAuthenticator extends Authenticator {
    private String user;
    private String pwd;

    public MailAuthenticator(String user, String pwd) {
        this.user = user;
        this.pwd = pwd;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, pwd);
    }
}


public void sendMail() {

        String to = "xxxx@xxxx.com";// 收信邮箱
        String subject = "javaMail测试发送";// 邮件主题
        String text = "测试邮件";// 邮件内容

        _LOG.debug("发送邮箱服务器配置信息加载...");
        Properties properties = new Properties();// 创建Properties对象
        //方法一:手动添加配置信息
//      properties.setProperty("mail.transport.protocol", "smtp");// 设置传输协议
//      properties.put("mail.smtp.host", "smtp.qq.com");// 设置发信邮箱的smtp地址
//      properties.setProperty("mail.smtp.auth", "true"); // 验证
//      String from = "11111111@qq.com";// 发信邮箱
//      Authenticator auth = new MailAuthenticator(from, "11111111"); // 使用验证,创建一个Authenticator

        //方法二:读取配置文件
        String propertiesFilePath = "conf/jmail.properties";
        try {
            InputStream in = JavaMailSenderDemo.class.getClassLoader().getResourceAsStream(propertiesFilePath);
            properties.load(in);//读取配置信息
            in.close();
        } catch (IOException e1) {
            _LOG.error("路径:"+propertiesFilePath+"读取失败!", e1);
        }
        String from = properties.getProperty("mail.userName");// 发信邮箱
        Authenticator auth = new MailAuthenticator(from, properties.getProperty("mail.password")); // 使用验证,创建一个Authenticator

        _LOG.debug("发送邮箱服务器配置信息加载完毕,创建session注册配置");
        Session session = Session.getDefaultInstance(properties, auth);// 根据Properties,Authenticator创建Session
        try {
            Message message = new MimeMessage(session);// Message存储发送的电子邮件信息
            message.setFrom(new InternetAddress(from));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));// 设置收信邮箱
            message.setSubject(subject);// 设置主题
            message.setText(text);// 设置内容
            Transport.send(message);// 发送
            _LOG.debug("发送完毕!");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

如果 new MailAuthenticator(userName, password);
message.setFrom(new InternetAddress(fromAddress));
userNamefromAddress不一致将会报出
501 mail from address must be same as authorization user的错误

从多个邮箱发送邮件

现在要在一个进程中同时由多个邮箱发送邮件。此事不但要注意上面描述的情况外,还要注意一点,在同一个进程中Session.getDefaultInstance得到的是一个单例的Session对象,就是第一次getDefaultInstance得到的Session对象。看Session对象中源码就一目了然

// The default session.
private static Session defaultSession = null;

public static synchronized Session getDefaultInstance(Properties props,
                Authenticator authenticator) {
    if (defaultSession == null)
        defaultSession = new Session(props, authenticator);
    else {
        // have to check whether caller is allowed to see default session
        if (defaultSession.authenticator == authenticator)
        ;   // either same object or both null, either way OK
        else if (defaultSession.authenticator != null &&
            authenticator != null &&
            defaultSession.authenticator.getClass().getClassLoader() ==
            authenticator.getClass().getClassLoader())
        ;   // both objects came from the same class loader, OK
        else
        // anything else is not allowed
        throw new SecurityException("Access to default session denied");
    }

    return defaultSession;
}

也就是说程序中我们要切换发送方的邮箱账户,我们就不能用Session.getDefaultInstance获取Session,我们必须用Session.getInstance,使我们每次切换账户得到的都是一个新的Session对象。

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值