当前位置:   article > 正文

java多个账号发送邮件 501错误_501 invalid eml

501 invalid eml

从一个邮箱发送邮件

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

  1. /**
  2. * 服务器邮箱登录验证
  3. */
  4. class MailAuthenticator extends Authenticator {
  5. private String user;
  6. private String pwd;
  7. public MailAuthenticator(String user, String pwd) {
  8. this.user = user;
  9. this.pwd = pwd;
  10. }
  11. @Override
  12. protected PasswordAuthentication getPasswordAuthentication() {
  13. return new PasswordAuthentication(user, pwd);
  14. }
  15. }
  16. public void sendMail() {
  17. String to = "xxxx@xxxx.com";// 收信邮箱
  18. String subject = "javaMail测试发送";// 邮件主题
  19. String text = "测试邮件";// 邮件内容
  20. _LOG.debug("发送邮箱服务器配置信息加载...");
  21. Properties properties = new Properties();// 创建Properties对象
  22. //方法一:手动添加配置信息
  23. // properties.setProperty("mail.transport.protocol", "smtp");// 设置传输协议
  24. // properties.put("mail.smtp.host", "smtp.qq.com");// 设置发信邮箱的smtp地址
  25. // properties.setProperty("mail.smtp.auth", "true"); // 验证
  26. // String from = "11111111@qq.com";// 发信邮箱
  27. // Authenticator auth = new MailAuthenticator(from, "11111111"); // 使用验证,创建一个Authenticator
  28. //方法二:读取配置文件
  29. String propertiesFilePath = "conf/jmail.properties";
  30. try {
  31. InputStream in = JavaMailSenderDemo.class.getClassLoader().getResourceAsStream(propertiesFilePath);
  32. properties.load(in);//读取配置信息
  33. in.close();
  34. } catch (IOException e1) {
  35. _LOG.error("路径:"+propertiesFilePath+"读取失败!", e1);
  36. }
  37. String from = properties.getProperty("mail.userName");// 发信邮箱
  38. Authenticator auth = new MailAuthenticator(from, properties.getProperty("mail.password")); // 使用验证,创建一个Authenticator
  39. _LOG.debug("发送邮箱服务器配置信息加载完毕,创建session注册配置");
  40. Session session = Session.getDefaultInstance(properties, auth);// 根据Properties,Authenticator创建Session
  41. try {
  42. Message message = new MimeMessage(session);// Message存储发送的电子邮件信息
  43. message.setFrom(new InternetAddress(from));
  44. message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));// 设置收信邮箱
  45. message.setSubject(subject);// 设置主题
  46. message.setText(text);// 设置内容
  47. Transport.send(message);// 发送
  48. _LOG.debug("发送完毕!");
  49. } catch (MessagingException e) {
  50. e.printStackTrace();
  51. }
  52. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

如果 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对象中源码就一目了然

  1. // The default session.
  2. private static Session defaultSession = null;
  3. public static synchronized Session getDefaultInstance(Properties props,
  4. Authenticator authenticator) {
  5. if (defaultSession == null)
  6. defaultSession = new Session(props, authenticator);
  7. else {
  8. // have to check whether caller is allowed to see default session
  9. if (defaultSession.authenticator == authenticator)
  10. ; // either same object or both null, either way OK
  11. else if (defaultSession.authenticator != null &&
  12. authenticator != null &&
  13. defaultSession.authenticator.getClass().getClassLoader() ==
  14. authenticator.getClass().getClassLoader())
  15. ; // both objects came from the same class loader, OK
  16. else
  17. // anything else is not allowed
  18. throw new SecurityException("Access to default session denied");
  19. }
  20. return defaultSession;
  21. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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

本文转载自:http://blog.csdn.net/zhangshaoxu/article/details/48291047

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/657899
推荐阅读
相关标签
  

闽ICP备14008679号