赞
踩
从一个邮箱发送邮件报错 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
- else
- // anything else is not allowed
- throw new SecurityException("Access to default session denied");
- }
-
- return defaultSession;
- }
也就是说程序中我们要切换发送方的邮箱账户,我们就不能用Session.getDefaultInstance
获取Session,我们必须用Session.getInstance
,使我们每次切换账户得到的都是一个新的Session对象。
本文转载自:http://blog.csdn.net/zhangshaoxu/article/details/48291047
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。