Javamail API - 如何将setFrom更改为您想要的任何内容?

时间:2012-12-01 00:20:06

标签: java javamail

如何将setFrom()方法更改为我想要的任何内容?我可以通过我的gmail accoutn发送电子邮件并更改setFrom文本,但它会显示我的username电子邮件。我也试过使用我的雅虎帐户,但我收到了身份验证错误。

我想更改发件人地址。代码如下:

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailTLS {

    public static void main(String[] args) {

        final String username = "username@gmail.com";
        final String password = "password";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            }
        );

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from-email@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to-email@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

4 个答案:

答案 0 :(得分:1)

使用" smtp.gmail.com"时遇到同样的问题。 使用Mandrill,它会起作用。 设置Mandrill帐户后,请使用" smtp.mandrillapp.com"在587港口。 对于身份验证,请设置username =您的mandrill用户名和密码=您帐户中生成的API密钥。

答案 1 :(得分:0)

许多信誉良好的SMTP中继都不允许您伪造您的身份(这会让垃圾邮件发送者更容易滥用该服务)。也就是说,如果您的目标是避免在收件箱中添加更多邮件Google allows you to modify your email address,以便过滤对您电子邮件的任何回复。

答案 2 :(得分:0)

(假设我理解你的目标并且不发送垃圾邮件)我们使用的Apache Commons库是通过调用setPersonal上的InternetAddress来实现的。 http://docs.oracle.com/javaee/7/api/javax/mail/internet/InternetAddress.html#setPersonal-java.lang.String-java.lang.String-

        Message message = new MimeMessage(session);
        InternetAddress me = new InternetAddress("from-email@gmail.com");
        me.setPersonal("My name");
        message.setFrom(me);

答案 3 :(得分:-2)

这是完整的代码 您可以使用以下代码。 这对我来说很好用

class SendMail {
public static void main(String[] args) {
    System.out.println("In side main()---------------------");

    Properties props = new Properties();


    props.put("mail.smtp.host", "hostname");
    props.put("mail.smtp.port", "port");//

    props.put("mail.smtp.socketFactory.port", "port");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");



    props.put("mail.smtp.auth", "true");

    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    System.out
                            .println("In side getPasswordAuthentication------------------And Before returning PasswordAuthentication");
                    return new PasswordAuthentication("from@gmail.com",
                            "password");

                }

            });
    System.out
            .println("mail and password has been sent********************");
    try {
        System.out
                .println("we are  in try{} block.................................");
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("from@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear User," + "\n\n This is testing only!");

        Transport.send(message);

        System.out
                .println("Mail has been sent successfully..........................");

    } catch (MessagingException e) {
        System.out.println("we are in catch block...................");
        e.printStackTrace();

    }
}

}