设置"来自" Java MimeMessage中的头字段无法正常工作

时间:2014-04-11 21:06:52

标签: java mime-message

对于我正在处理的网络应用程序,我提出了一种发送电子邮件通知的方法。该消息必须来自特定帐户,但我希望"来自"标题字段读取为完全不同的电子邮件地址。这是我的代码(我已将实际的电子邮件地址更改为假的):

public static boolean sendEmail(List<String> recipients, String subject, String content){
    String header = "This is an automated message:<br />"+"<br />";
    String footer = "<br /><br />unsubscribe link here";
    content = header + content + footer;

    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() {
            //This is where the email account name and password are set and can be changed
            return new PasswordAuthentication("ACTUAL.ADRESS@gmail.com", "PASSWORD");
        }
      });
    try{
         MimeMessage message = new MimeMessage(session);
         try {
            message.setFrom(new InternetAddress("FAKE.ADDRESS@gmail.com", "FAKE NAME"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
         message.setReplyTo(new Address[]{new InternetAddress("no-reply@gmail.com")});
         for(String recipient: recipients){
             message.addRecipient(Message.RecipientType.BCC,new InternetAddress(recipient));
         }
         message.setSubject(subject);
         message.setContent(content,"text/html");
         Transport.send(message);
         return true;
      }catch (MessagingException mex) {
         mex.printStackTrace();
         return false;
      }
}

对于上述方法,使用它发送电子邮件将具有以下电子邮件标题:

from:    FAKE NAME <ACTUAL.ADRESS@gmail.com>

我想要阅读:

from:    FAKE NAME <FAKE.ADRESS@gmail.com>

我做错了什么?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

您要做的事情被称为&#34;欺骗。&#34;看起来您使用的是Google的SMTP服务器,如果是这种情况,您将无法成功完成此操作。出于安全考虑,Google只会允许&#34;来自&#34;地址是经过身份验证的电子邮件地址。

See this related question