发送电子邮件:
public class SendMail {
public void sendEmail(final String from, final String password,String toAddress) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddress));
message.setSentDate(new Date());
message.setSubject("Account Activation for T20home");
message.setContent("Hello", "text/html");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", from, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(MessagingException mex)
{
}
}
}
我可以发送这样的电子邮件:
SendMail send = new SendMail()
send.sendEmail("mailFrom1@gmail.com", "pass1","to1")
SendMail send1 = new SendMail()
send1.sendEmail("mailFrom2@gmail.com", "pass2","to1")
它工作得很好,但是从 mailFrom1@gmail.com 收到两封信到电子邮件“to1”。
必须来自“mailFrom1@gmail.com”的一封信和来自“mailFrom2@gmail.com”的一封信
setFrom 不工作