javamail发送hotmail邮件

hotmail使用tls加密发送发送;开始会应该没有使用MailSSLSocketFactory 导致验证无法通过。
发送代码:
try{

String host = "smtp.live.com";//Ip address of your system smtp-mail.outlook.com
String user = "*****";//email address you configured in hmail server
String pwd = "*****";//password of email address
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.ssl.enable","false");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.checkserveridentity", "false");
props.put("mail.smtp.ssl.socketFactory", sf);
MyAuthenticator authenticator = null;
authenticator = new MyAuthenticator(user,pwd);
Session ses = Session.getInstance(props,authenticator);
ses.setDebug(true);

MimeMessage message = new MimeMessage(ses);
message.setFrom(new InternetAddress(user));
String[] to = new String[1];
to[0] = "*******";
InternetAddress[] sendTo = new InternetAddress[1];
for (int j = 0; j < 1 ; j++)
{
sendTo[j] = new InternetAddress(to[j]);
}
message.setRecipients(Message.RecipientType.TO,sendTo);
message.setSubject("JAVA MAIL");

message.setText("This is a test mail");
Transport.send(message);
System.out.println("done");



}
catch (Exception e){
System.out.println(e.toString());

}
身份验证代码:

import javax.mail.*;

public class MyAuthenticator extends Authenticator{
String userName=null;
String password=null;

public MyAuthenticator(){
}
public MyAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName, password);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值