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

xiaoxiao2021-02-28  74

从一个邮箱发送邮件

从一个邮箱发送邮件报错 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(); } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960

如果 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; } 1234567891011121314151617181920212223 1234567891011121314151617181920212223

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

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

转载请注明原文地址: https://www.6miu.com/read-27230.html

最新回复(0)