现在的发件人地址是risingsuntest@hotmail.com我想用risingsuntest@hotmail.comrisingsuntest@hotmail.com进行登陆,而发件人的地址是yanlong137@hotmail.com ,大家有会的吗?
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Authenticator;
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;
import javax.mail.internet.MimeUtility;
public class SendMail2 {
public static void sendMail(String toEmail, String title, String content) {
String host = "smtp.live.com";
String from = "wangjunlong2010@126.com";
String to = toEmail;
final String username = "risingsuntest@hotmail.com";
final String password = "skysnow";
// 创建Properties 对象
Properties props = System.getProperties();
// 添加smtp服务器属性 添加了SSL验证
props.put("mail.smtp.starttls.enable", "true");
props.setProperty("mail.smtp.socketFactory.fallback", "true");
// props.setProperty("mail.smtp.socketFactory.class",
// "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", username);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "25"); // gmail smtp port 587
props.put("mail.smtp.auth", "true");
// 创建邮件会话
Session session = Session.getDefaultInstance(props,
new Authenticator() { // 验账账户
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
session.setDebug(true);
// 定义邮件信息
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("yanlong137@hotmail.com"));// GB2312编码,防止接收后标题乱码
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject(title);
message.setText(content);
// 发送消息
// session.getTransport("smtp").send(message); //也可以这样创建Transport对象
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SendMail2.sendMail("495924217@qq.com", "测试", "发送成功");
}
}