阿里云上的使用QQ邮箱发送邮件

直接上代码了 由于阿里云禁用了25端口所以替换成465端口并修改协议为ssl才能发送邮件

参考我的另一篇博客

http://blog.csdn.net/u013412790/article/details/54912506
public class SendEmail {

    private static final Logger logger = LogManager.getLogger(SendEmail.class);

    private final static String smtpHost = "SMTP.qq.com";//配置Email session对象
    private final static String messageType = "text/html;charset=UTF-8";//相应内容类型,编码类型
    private final static String subject = "Imax(民大二手平台) 密码修改";//主题
    private final static String fromEmail = "[email protected]";//发送邮件的邮箱
    private final static String password = "thblmuldeba";//密码

    @SuppressWarnings("static-access")
    public boolean sendMessage(String to, String id, String username) {
        try {
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";//SSL加密
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            String messageText = appendString(id, username);
            //第一步:配置javax.mail.Session对象
            Properties props = new Properties();   // 创建Properties 类用于记录邮箱的一些属性

            props.put("mail.smtp.socketFactory.class", SSL_FACTORY);

            props.put("mail.smtp.host", smtpHost);  //此处填写SMTP服务器
            props.put("mail.smtp.starttls.enable", "true");//使用 STARTTLS安全连接
            props.put("mail.smtp.port", "465");             //google使用465或587端口
            props.put("mail.smtp.auth", "true");       // 表示SMTP发送邮件,必须进行身份验证
            props.put("mail.debug", "true");      //开启调试模式
            props.put("mail.transport.protocol", "SMTP");     // 发送邮件协议名称
            props.setProperty("mail.smtp.socketFactory.port", "465");
            Session mailSession = Session.getInstance(props, new MyAuthenticator(fromEmail, password));//此处填写你的账号和口令(16位口令)
            props.put("mail.smtp.ssl.socketFactory", sf);
            //第二步:编写消息
            InternetAddress fromAddress = new InternetAddress(fromEmail);// 设置发件人的邮箱
            InternetAddress toAddress = new InternetAddress(to); // 设置收件人的邮箱
            MimeMessage message = new MimeMessage(mailSession);
            message.setFrom(fromAddress);
            message.addRecipient(RecipientType.TO, toAddress);
            message.setSentDate(Calendar.getInstance().getTime());
            message.setSubject(subject);   // 设置邮件标题
            message.setContent(messageText, messageType);// 设置邮件的内容体

            // 第三步:发送消息
            Transport transport = mailSession.getTransport("smtp");
            transport.connect(smtpHost, fromEmail, password);
            transport.send(message, message.getRecipients(RecipientType.TO)); // 发送邮件啦

            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("邮件发送错误" + e.getMessage());
            return false;
        } catch (Exception e) {
            logger.error("邮件发送错误" + e.getMessage());
            return false;
        }
    }

    /**
     * @Author: Ming
     * @param: id  用户id
     * @Description: 添加发送的短信的信息
     * @Date: 17:12 2017/2/6
     */
    public static String appendString(String id, String username) {
        int i;
        String t = " ";
        String messageText = "";
        for (i = 0; i < 4; i++) messageText += t;
        messageText += "欢迎使用民大二手市场平台~
"
; for (i = 0; i < 12; i++) messageText += t; messageText += "请点击:http://112.74.169.239/user/" + username + "/activation/" + id; return messageText; } }

class MyAuthenticator extends Authenticator {
private String userName=”“;
private String password=”“;

public MyAuthenticator(String userName,String password){
    this.userName=userName;
    this.password=password;
}
protected PasswordAuthentication getPasswordAuthentication(){
    return new PasswordAuthentication(userName, password);
}

}

你可能感兴趣的:(java,阿里云,qq邮箱)