邮件发送(经测试有效)

package user.controller;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMail {
public static void email(String email,Integer authcode)throws Exception {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");//发送邮件协议
properties.setProperty("mail.smtp.auth", "true");//需要验证
//properties.setProperty("mail.debug", "true");//设置debug模式 后台输出邮件发送的过程
Session session = Session.getInstance(properties);
session.setDebug(true);//debug模式
//邮件信息
Message messgae = new MimeMessage(session);
messgae.setFrom(new InternetAddress("****@aliyun.com"));//设置发送人
        messgae.setText("你的验证码为:"+authcode+"。请注意,验证码有效时间为2分钟!!!");//设置邮件内容
messgae.setSubject("邮箱验证");//设置邮件主题
//发送邮件
Transport tran = session.getTransport();
tran.connect("smtp.aliyun.com", 25, "****@aliyun.com", "****");//
      
tran.sendMessage(messgae, new Address[]{ new InternetAddress(email)});//设置邮件接收人
tran.close();
}


public static void main(String[] args) {
try {
SendMail.email("*****@qq.com",123);
} catch (Exception e) {
e.printStackTrace();
}
}
}

转裁自别处,非原创...

posted on 2018-02-24 10:44  Legend_yan  阅读(137)  评论(0编辑  收藏  举报