JavaMail - 多个发件人

标签 java smtp jakarta-mail

我有一个简单的 javamail 类来通过 SMTP 发送电子邮件。只要我只从一个地址发送电子邮件,它就可以工作。如果我尝试使用另一个地址,由于某种原因它会引发此异常:

com.sun.mail.smtp.SMTPSendFailedException: 550 5.1.0 Use your own address, please.

这是我的类(class):

public class EmailSender {
    private static final String HOST = "xxxx.xxxxxx.xx";
    private static final String PORT = "xx";

    public static boolean sendMail(String from, String to, String pass, String subject, String text) {
        Properties properties = new Properties();

        properties.setProperty("mail.smtp.host", HOST);
        properties.setProperty("mail.smtp.port", PORT);
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.user", from);
        properties.setProperty("mail.password", pass);

        Session session = Session.getDefaultInstance(properties, new CustomAuthenticator(from, pass));

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setText(text);

            Transport.send(message);

            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
}

如果您想知道,CustomAuthenticator 类如下所示:

public class CustomAuthenticator extends Authenticator {
    private String user;
    private String pw;

    public CustomAuthenticator(String username, String password) {
        super();
        this.user = username;
        this.pw = password;
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, pw);
    }
}

最佳答案

所以我发现解决方案是使用 SMTPTransport 类。

public class EmailSender {
    private static final String HOST = "xxxx.xxxxxx.xx";
    private static final String PORT = "xx";

    public static boolean sendMail(String from, String to, String pass, String subject, String text) {
        Properties properties = new Properties();

        properties.setProperty("mail.smtp.port", PORT);
        properties.setProperty("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(properties, new CustomAuthenticator(from, pass));

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setText(text);

            SMTPTransport tp = (SMTPTransport) session.getTransport();
            tp.connect(HOST, from, pass);
            tp.sendMessage(message, message.getAllRecipients());
            tp.close();

            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
}

关于JavaMail - 多个发件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39817492/

相关文章:

java - 找不到项目时抛出异常 - Java

java - 在 Mongo 中构建倒排列表的更快方法

java - 用于 IMAP 和 SMTP 身份验证的 Office 365 XOAUTH2 失败

java - 使用 Javamail 从 hotmail 发送邮件?

tomcat - mail.jar 和 activation.jar 应该放到 CATALINA/lib 中吗?

java - Spring Rest 服务中的可选请求 header

java - 如何获取android主类的实例?

ios - iOS 中的 Google OAuth2 问题 - "invalid_grant"错误

php - Google SMTP 连接超时

Java : Using Javamail to read email