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);
}
}

你可能感兴趣的:(java)