[size=large][b]Action类[/b][/size]
设置好邮箱的一些基本内容:
[size=large][b]工具类[/b][/size]
实现发送邮件
源代码文件 如下
[color=red][size=large][b]javaEmaile所需jar包[url]http://pan.baidu.com/share/link?shareid=1993178199&uk=2248831455[/url][/b][/size][/color]
设置好邮箱的一些基本内容:
/**
* 发送邮箱验证
* @throws Exception
* */
public String yanzheng() throws Exception {
//得到某个邮箱的验证码
Count count = new Count();
T_user tUser = count.getbyid(emailid);
System.out.println("tUser--"+tUser.getEmail());
//加密验证码
String checkcode = MD5Util.encode2hex(tUser.getValidatecode());
HttpServletRequest request = ServletActionContext.getRequest();
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
//发送验证邮件
StringBuffer sb = new StringBuffer();
sb.append("<img src='https://passport.baidu.com/getpass/img/logo.gif'><br/>")
.append("<b>亲爱的用户:</b><br/>")
.append("您注册百度帐号 "+tUser.getEmail()+",点击以下链接,即可激活该帐号:<br/>")
.append("<a href='"+basePath+"?email=")
.append(tUser.getEmail())
.append("&validateCode=")
.append(checkcode)
.append("'>"+basePath+"?email=")
.append(tUser.getEmail()+"&validateCode="+checkcode)
.append("</a><br/>")
.append("(如果您无法点击此链接,请将它复制到浏览器地址栏后访问)<br/>")
.append("1、为了保障您帐号的安全性,请在 48小时内完成激活,此链接将在您激活过一次后失效!<br/>")
.append("2、请尽快完成激活,否则过期,即2013年 09月07日 09:30后百度将有权收回该帐号。");
System.out.println(sb.toString());
MailUtil.send(tUser.getEmail() , sb.toString());
return SUCCESS;
}
[size=large][b]工具类[/b][/size]
实现发送邮件
public class MailUtil {
public static final String HOST = "smtp.163.com";
public static final String PROTOCOL = "smtp";
public static final int PORT = 25;
public static final String FROM = "****@163.com";
public static final String PWD = "password";
/**
* 获取Session
* @return
*/
private static Session getSession() {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);//设置服务器地址
props.put("mail.store.protocol" , PROTOCOL);//设置协议
props.put("mail.smtp.port", PORT);//设置端口
props.put("mail.smtp.auth" , true);
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM, PWD);
}
};
Session session = Session.getDefaultInstance(props , authenticator);
return session;
}
public static void send(String toEmail , String content) {
Session session = getSession();
try {
// Instantiate a message
Message msg = new MimeMessage(session);
//设置 发件的邮箱 及 对方邮箱中显示的 别名
msg.setFrom(new InternetAddress(FROM,"拜拜公司"));
InternetAddress[] address = {new InternetAddress(toEmail)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("账号激活邮件");
msg.setSentDate(new Date());
msg.setContent(content , "text/html;charset=utf-8");
//发送邮件
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
源代码文件 如下
[color=red][size=large][b]javaEmaile所需jar包[url]http://pan.baidu.com/share/link?shareid=1993178199&uk=2248831455[/url][/b][/size][/color]