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

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

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

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

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

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

(假设我理解你的目标并且不发送垃圾邮件)我们使用的Apache Commons库是通过在InternetAddress上调用setPersonal来实现的。 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); 

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

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

}