我在我的web-app(Spring MVC)中使用JavaMailSenderImpl。我从一些acc发送邮件,例如“sender@gmail.com”。我需要setFrom到另一个acc,例如“sender2@gmail.com”。我怎么能这样做?我尝试setFrom("sender2@gmail.com")
,但用户收到来自发件人:“sender@gmail.com”的邮件。我究竟做错了什么?
我的配置:
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
try {
javaMailSenderImpl.setHost("smtp.gmail.com");
javaMailSenderImpl.setPort(567);
javaMailSenderImpl.setUsername("sender@gmail.ua");
javaMailSenderImpl.setPassword("*********");
javaMailSenderImpl.getJavaMailProperties().put("mail.smtp.host", "smtp.gmail.com");
javaMailSenderImpl.getJavaMailProperties().put("mail.smtp.port", "465");
javaMailSenderImpl.getJavaMailProperties().put("mail.debug", "true");
javaMailSenderImpl.getJavaMailProperties().put("mail.smtp.auth", "true");
javaMailSenderImpl.getJavaMailProperties().put("mail.smtp.starttls.enable", "true");
javaMailSenderImpl.getJavaMailProperties().setProperty("mail.smtp.socketFactory.port", "465");
javaMailSenderImpl.getJavaMailProperties().setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
javaMailSenderImpl.getJavaMailProperties().setProperty("mail.smtp.socketFactory.fallback", "false");
} catch (Exception e) {
logger.error(e.getMessage());
}
return javaMailSenderImpl;
}
我发送电子邮件的代码:
MimeMessage message2 = javaMailSenderImpl.createMimeMessage();
Address from = new InternetAddress("sender2@gmail.com");
message2.setFrom(from);
MimeMessageHelper helper = new MimeMessageHelper(message2, true);
helper.setFrom("sender2@gmail.com");
/*adding body,subject etc*/
javaMailSenderImpl.send(message2);
答案 0 :(得分:0)
您使用身份验证。
我想编辑一些代码。
样品。
(pom.xml中)
<!-- java mail api -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- send mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
(测试mian类)
public class MyAuthentication extends Authenticator {
PasswordAuthentication pa;
private final static String id = "test@gmail.com";
private final static String pw = "1234";
public MyAuthentication() {
pa = new PasswordAuthentication(id, pw);
}
// authentication
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
public static void main(String[] args) {
Properties p = System.getProperties();
p.put("mail.smtp.starttls.enable", "true");
p.put("mail.smtp.host", "smtp.gmail.com");
p.put("mail.smtp.auth", "true");
p.put("mail.smtp.port", "587");
Authenticator auth = new MyAuthentication();
Session session = Session.getDefaultInstance(p, auth);
MimeMessage msg = new MimeMessage(session);
try {
msg.setSentDate(new Date());
InternetAddress from = new InternetAddress();
from = new InternetAddress("test<" + id + ">");
// sender
msg.setFrom(from);
// receiver
InternetAddress to = new InternetAddress("test123@gmail.com");
msg.setRecipient(Message.RecipientType.TO, to);
// title
msg.setSubject("test send email", "UTF-8");
// context
msg.setText("context bla~bla~", "UTF-8");
// header
msg.setHeader("content-Type", "text/html");
// send msg
javax.mail.Transport.send(msg);
System.out.println("send email");
} catch (AddressException addr_e) {
addr_e.printStackTrace();
} catch (MessagingException msg_e) {
msg_e.printStackTrace();
}
}
}
答案 1 :(得分:0)
请参阅此Gmail帮助页面,了解sending mail from a different address or alias。