大家好。我是JavaMail API的新手,因此需要您的帮助。我正在尝试发送电子邮件,但是我想设置电子邮件从中到达的电子邮件地址。我已经成功发送了一封电子邮件,并且收到了,但是在FROM部分中不是我在代码中设置的电子邮件。我尝试将其设置为MimeMessage并将其放在属性中,但未更改。我可以使用任何帮助,谢谢您。示例:
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.from", "fromAddress@gmail.com");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userMail.getUserName(), userMail.getUserPassword());
}
});
try {
MimeMessage message = new MimeMessage(session);
InternetAddress address = new InternetAddress("fromAddress@gmail.com");
message.setFrom(address);
message.addRecipient(Message.RecipientType.TO, new InternetAddress("toAddress@gmail.com"));
message.setSubject("HTML Message");
message.setContent("<h1>sending html mail check</h1>", "text/html");
message.saveChanges();
Transport.send(message);
} catch (MessagingException ex) {
ex.printStackTrace();
}