对于正在使用的Web应用程序,我制定了一种发送电子邮件通知的方法。该消息必须来自特定帐户,但是我希望“发件人”标头字段作为完全不同的电子邮件地址来读取。这是我的代码(我已将实际的电子邮件地址更改为假的电子邮件地址):
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>
我究竟做错了什么?任何帮助表示赞赏!
您想要做的就是所谓的“欺骗”。似乎您正在使用Google的SMTP服务器,如果是这种情况,您将无法成功执行此操作。为了安全起见,Google仅允许将“发件人”地址作为经过身份验证的电子邮件地址。