JavaMail_3 支持SSL(GMail)

来源:互联网 发布:把脸变丑的软件 编辑:程序博客网 时间:2024/06/11 17:21

随着各个Mail服务器对于安全的重视,纷纷采用基于SSL的Mail登陆方式进行发送和接收电子邮件。例如GMail等。当使用JavaMail发送电子邮件时,需要根据SSL设定,增加安全验证的功能。

这里使用JDK提供的JavaMail工具进行发送电子邮件

1、增加用户名密码验证

需要实现抽象类Authenticator的抽象方法PasswordAuthentication,代码如下:

public class MailAuthenticator extends Authenticator {

private String userName;

private String password;


public MailAuthenticator(String userName, String password) {
   super();
   this.userName = userName;
   this.password = password;
}



@Override
protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication(userName, password);
}


}

这里的UserName和Password是用户名和密码。

在Mail的Session初始化时调用:

Session session = Session.getDefaultInstance(props, auth);

2、修改SMTP端口

需要重新的设定SMTP的端口:

props.put("mail.smtp.socketFactory.port", port);

其中port是SMTP的端口,默认是25,而安全验证的Mail一般使用465

3、设定STARTTLS

当SMTP需要SSL验证时,需要设定,如果不设定,会出现如下异常

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

设定为props.put("mail.smtp.starttls.enable","true");这个是关键

在这三点之后,其他的同普通的发送电子邮件服务。

全部代码如下:

public class MailSend {

//日志
private static Logger log = Logger.getLogger(MailSend.class);
//保存参数
private Properties props = new Properties();

//发送邮件服务器
private String server;
//发送邮件地址
private String from;

//认证信息
private Authenticator auth = null;

public void setServer(String server){
   this.server = server;
   props.put("mail.smtp.host", server);
}

public void setForm(String from){
   this.from = from;
}

public void setSSLSecurity(String port){
   props.put("mail.smtp.socketFactory.port", port);
//   props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//   props.put("mail.smtp.socketFactory.fallback", "false");
   props.put("mail.smtp.starttls.enable","true");

}

//set security user name and password
public void setAuth(String userName, String userPassword){
   auth = new MailAuthenticator(userName, userPassword);
   props.put("mail.smtp.auth","true");
}


public boolean sendMail(String toAddr, String ccAddr, String subject, String message){
   if(toAddr == null){
    throw new MailException(MailException.NOTOADDR);
   }
   if(from == null){
    throw new MailException(MailException.NOTOADDR);
   }
   if(server == null){
    throw new MailException(MailException.NOSERVER);
   }
   try {
    Session session = Session.getDefaultInstance(props, auth);
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] tos = InternetAddress.parse(toAddr);
    msg.setRecipients(Message.RecipientType.TO, tos);
    if(ccAddr != null){
     InternetAddress[] cc = InternetAddress.parse(ccAddr);
     msg.setRecipients(Message.RecipientType.CC, cc);
    }
    msg.setSubject(subject);
    msg.setText(message);
    Transport.send(msg);
    return true;
   } catch (AddressException e) {
    log.error(e.getMessage(), e);
   } catch (MessagingException e) {
    log.error(e.getMessage(), e);
   }
   return false;
}
}

 

转自互联网。

原创粉丝点击