从一个邮箱发送邮件

从一个邮箱发送邮件报错 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));
userName fromAddress 不一致将会报出
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
        // anything else is not allowed
        throw new SecurityException("Access to default session denied");
    return defaultSession;

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

使用不同的兩個帳戶发送email时,第一个账户可以发送成功,但到第二个账户的时候就报出了501 mail from address must be same as authorization user的错误。 具体代码如下: import java.util.Date; import java.util.List; import java.util.Properties; com.sun.mail.smtp.SMTPSenderFailedException: 501 mail from address must be same as authorization user at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.jav...
.NET后台代码利用QQ邮箱服务器发送邮件遇到的问题:”mail from address must be same as authorization user”  首先,看下我的代码实现 后台代码: public class EmailHelper /// <summary> /// 发送邮件 /// </summary> /// <param name=subject>主题</param> /// <param name=body>内容</param> public static void SendEmail(string
赠送jar包:javax.mail-1.5.6.jar; 赠送原API文档:javax.mail-1.5.6-javadoc.jar; 赠送源代码:javax.mail-1.5.6-sources.jar; 赠送Maven依赖信息文件:javax.mail-1.5.6.pom; 包含翻译后的API文档:javax.mail-1.5.6-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:com.sun.mail:javax.mail:1.5.6; 标签:sun、mailjavax、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。