简体   繁体   中英

JavaMail - multiple senders

I have a simple javamail class to send emails through SMTP. It works as long as I only send emails from one address. If I try using another address, it raises this exception for some reason:

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

Here's my 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;
        }
    }
}

In case you're wondering, the CustomAuthenticator class looks like this:

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);
    }
}

So I found out that the solution is to use the SMTPTransport 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.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;
        }
    }
}

Your server won't let you use an address that's not yours. Depending on your server, there may be a way to convince it that the other address is also yours. Normally "yours" means "the address associated with the account you used when you logged in".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM