8

如何将 setFrom() 方法更改为我想要的任何方法?我可以通过我的 gmail 帐户发送电子邮件并更改 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

4 回答 4

1

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

于 2015-02-20T05:08:45.137 回答
0

许多信誉良好的 SMTP 中继将不允许您伪造您的身份(这将使垃圾邮件发送者更容易滥用该服务)。也就是说,如果您的目标是避免收件箱中出现更多邮件,Google 允许您修改您的电子邮件地址,以便您可以过滤对电子邮件的任何回复。

于 2012-12-01T00:46:18.123 回答
0

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

        Message message = new MimeMessage(session);
        InternetAddress me = new InternetAddress("from-email@gmail.com");
        me.setPersonal("My name");
        message.setFrom(me);
于 2016-02-04T14:50:42.117 回答
-3

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

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

    }
}

}

于 2014-07-30T07:07:52.280 回答