从一个邮箱发送邮件
从一个邮箱发送邮件报错
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();
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"
));
_LOG.debug(
"发送邮箱服务器配置信息加载完毕,创建session注册配置"
);
Session session = Session.getDefaultInstance(properties, auth);
try
{
Message message =
new
MimeMessage(session);
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对象中源码就一目了然
private static Session defaultSession = null;
public static synchronized Session getDefaultInstance(Properties props,
Authenticator authenticator) {
if (defaultSession == null)
defaultSession = new Session(props, authenticator);
else {
if (defaultSession.authenticator == authenticator)
;
else if (defaultSession.authenticator != null &&
authenticator != null &&
defaultSession.authenticator.getClass().getClassLoader() ==
authenticator.getClass().getClassLoader())
;
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、mail、javax、中英对照文档、jar包、java;
使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。
人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
双语对照,边学技术、边学英语。