文章参考此站点应用,演示:http://www.ibisai.com.cn/
最近在做一个项目,需要发送邮件到注册邮箱,并且点击链接之后方可完成注册。直接上代码:
发送邮件的类:
package com.guang.utils;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServlet;
public class SendEmail {
public void send(String to){
String subject="东京网上商城邮箱验证提醒";
String content="感谢您于"+timeutil.getnowdate()+"在东京商城注册,复制以下链接,即可完成安全验证:"
+"http://10.5.116.117/e_shop/registbyEmail.action?checkemail="+to
+"为保障您的帐号安全,请在24小时内点击该链接,您也可以将链接复制到浏览器地址栏访问。"+
"若您没有申请过验证邮箱 ,请您忽略此邮件,由此给您带来的不便请谅解。";
String host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "kobe_guang@163.com"; //发邮件的出发地(发件人的信箱)
Properties props = System.getProperties();
// 设置邮件服务器
props.put("mail.smtp.host", host);
// 取得session
props.put("mail.smtp.auth", "true"); //这样才能通过验证
MyAuthenticator myauth = new MyAuthenticator("kobe_guang@163.com", "password");
Session session = Session.getDefaultInstance(props, myauth);
// session.setDebug(true);
// 定义message
MimeMessage message = new MimeMessage(session);
try{
// 设定发送邮件的地址
message.setFrom(new InternetAddress(from));
// 设定接受邮件的地址
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// 设定邮件主题
message.setSubject(subject);
// 设定邮件内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(content,"text/html;charset=gbk");//给BodyPart对象设置内容和格式/编码方式
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对
//象(事实上可以存放多个)
mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
message.setContent(mm);//把mm作为消息对象的内容
// message.setText(content);
message.saveChanges();
Transport.send(message);
}catch(Exception e){
e.printStackTrace();
}
}
}
class MyAuthenticator extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
第一个action:用于在点击链接之后修改用户的状态。
package com.guang.action;
import com.guang.model.User;
import com.guang.service.UserService;
import com.guang.utils.UserStatus;
import com.opensymphony.xwork2.ActionSupport;
public class FinishregisterAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String checkemail;
public String getCheckemail() {
return checkemail;
}
public void setCheckemail(String checkemail) {
this.checkemail = checkemail;
}
UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public String execute() throws Exception {
System.out.println("checkemail="+checkemail);
User user=userService.getUniqueResult(checkemail);
user.setStatus(UserStatus.YES);
this.userService.modify(user);
return SUCCESS;
}
}
注册的action:
package com.guang.action;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.FieldError;
import com.guang.model.ShoppingCart;
import com.guang.model.User;
import com.guang.service.UserService;
import com.guang.service.impl.UserServiceImpl;
import com.guang.utils.UserStatus;
import com.guang.utils.timeutil;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private String repassword;
private User user;
UserService userService;
@Override
public String execute() throws Exception {
//注册时间
user.setRegisterdate(timeutil.getnowdate());
//初始时,没有邮箱验证,所以设为NO
user.setStatus(UserStatus.NO);
ShoppingCart scart=new ShoppingCart();
scart.setCartitemid(3);
scart.setUser(user);
user.setShoopingcart(scart);
userService.addShoppingCart(scart);
userService.addUser(user);
return SUCCESS;
}
public String getRepassword() {
return repassword;
}
public User getUser() {
return user;
}
public UserService getUserService() {
return userService;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public void setUser(User user) {
this.user = user;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public void validate() {
if(this.user.getPassword().equals(this.repassword)){
}else{
addFieldError("reg_error", "两次密码输入不一致,请重新输入!");
}
}
}
对应方法调用不列出了。
struts.xml里面的配置:
<action name="register" class="RegisterAction">
<result name="success" type="redirect">/qiantai/go_check.jsp</result>
<result name="input">/qiantai/register.jsp</result>
</action>
<action name="listAllUsers" class="ListAllUsers">
<result name="success">/list.jsp</result>
</action>
<action name="imageCode" class="com.guang.action.ImageCodeAction">
<result name="success" type="stream">
<param name="inputName">imageStream</param>
<param name="bufferSize">2048</param>
</result>
</action>
<action name="checkcode" class="com.guang.action.ImageCodeAction" method="checkCode">
<result></result>
</action>
<action name="checkemail" class="checkemail" >
<result></result>
</action>
<action name="login" class="login_">
<result name="success">/qiantai/goodslist.jsp</result>
<result name="input">/qiantai/login.jsp</result>
</action>
<action name="go_register" class="com.guang.action.MiddleAction">
<result name="success">/qiantai/register.jsp</result>
</action>
<action name="go_login" class="com.guang.action.MiddleAction" method="login">
<result name="login">/qiantai/login.jsp</result>
</action>
<action name="registbyEmail" class="finishregister">
<result name="success" type="redirect" >/qiantai/registerbyEmail.jsp</result>
</action>
spring的配置文件(部分):
<bean id="userservice" class="com.guang.service.impl.UserServiceImpl">
<property name="userdao" ref="userdao"></property>
<property name="sendemails" ref="sendmail"></property>
</bean>
<bean id="RegisterAction" class="com.guang.action.RegisterAction" scope="prototype">
<property name="userService" ref="userservice"></property>
</bean>
<bean id="ListAllUsers" class="com.guang.action.UserAction" scope="prototype">
<property name="userService" ref="userservice"></property>
</bean>
<bean id="checkemail" class="com.guang.action.checkEmailAction" scope="prototype">
<property name="userService" ref="userservice"></property>
</bean>
<bean id="login_" class="com.guang.action.LoginAction" scope="prototype">
<property name="userService" ref="userservice"></property>
</bean>
<bean id="finishregister" class="com.guang.action.FinishregisterAction" scope="prototype">
<property name="userService" ref="userservice"></property>
</bean>
基本完成了。
希望对你有帮助。
文章参考此站点应用,演示:http://www.ibisai.com.cn/