501 mail from address must be same as authorization user

做邮件发送的时候,内容一直为空。就自己写了一个测试类。看看是不是服务器的问题。

下面是代码

package org.rtx.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class test {
private static String host = "smtp.qq.com";
private static String username = "xxxx@qq.com";
private static String password = "xxxxxx";
private static int port = 25;

public static void main(String[] args) {
try {
// 3.4 接收人
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(false);
MimeMessage message = new MimeMessage(session);
message.setSubject("我的测试邮件");
// 3.2 邮件发送时间
message.setSentDate(new Date());
// 3.3设置邮箱收件人地址
message.setSender(new InternetAddress("1111@qq.com"));
message.setRecipient(RecipientType.TO, new InternetAddress("1111@qq.com"));
// 3.5内容
message.setText("你好,已经发送成功! 正文...."); // 简单纯文本邮件
message.saveChanges(); // 保存邮件(可选)
Transport transport = session.getTransport("smtp");
transport.connect(host, port, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("发送成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}

 

结果报错了  501 mail from address must be same as authorization user

看翻译好像是没有找到发送者地址,检查代码发现没有 设置发送邮件地址

再创建message后添加代码

message.setFrom(new InternetAddress("xxxx@qq.com"));

问题搞定

如果报错服务器问题,则需要开启qq邮箱的smtp 服务

posted @ 2020-04-02 13:50  奋斗的渣渣  阅读(7115)  评论(0编辑  收藏  举报