ssh+jmail实现,注册完发送邮件,点击链接之后完成注册。
来源:互联网 发布:刷钻软件免费版 编辑:程序博客网 时间:2024/06/05 17:30
最近在做一个项目,需要发送邮件到注册邮箱,并且点击链接之后方可完成注册。直接上代码:
发送邮件的类:
- <span style="color: rgb(51, 51, 51);">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","4515079");
- 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);
- }
- }
- </span><span style="color: rgb(255, 102, 102);">
- </span>
第一个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 FinishregisterActionextends ActionSupport {
- /**
- *
- */
- private staticfinallong 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:
- <span style="color: rgb(51, 51, 51);">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 RegisterActionextends 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","两次密码输入不一致,请重新输入!");
- }
- }
- }
- </span>
对应方法调用不列出了。
struts.xml里面的配置:
- <actionname="register"class="RegisterAction">
- <resultname="success"type="redirect">/qiantai/go_check.jsp</result>
- <resultname="input">/qiantai/register.jsp</result>
- </action>
- <actionname="listAllUsers"class="ListAllUsers">
- <resultname="success">/list.jsp</result>
- </action>
- <actionname="imageCode"class="com.guang.action.ImageCodeAction">
- <resultname="success"type="stream">
- <paramname="inputName">imageStream</param>
- <paramname="bufferSize">2048</param>
- </result>
- </action>
- <actionname="checkcode"class="com.guang.action.ImageCodeAction"method="checkCode">
- <result></result>
- </action>
- <actionname="checkemail"class="checkemail">
- <result></result>
- </action>
- <actionname="login"class="login_">
- <resultname="success">/qiantai/goodslist.jsp</result>
- <resultname="input">/qiantai/login.jsp</result>
- </action>
- <actionname="go_register"class="com.guang.action.MiddleAction">
- <resultname="success">/qiantai/register.jsp</result>
- </action>
- <actionname="go_login"class="com.guang.action.MiddleAction"method="login">
- <resultname="login">/qiantai/login.jsp</result>
- </action>
- <actionname="registbyEmail"class="finishregister">
- <resultname="success"type="redirect">/qiantai/registerbyEmail.jsp</result>
- </action>
spring的配置文件(部分):
- <beanid="userservice"class="com.guang.service.impl.UserServiceImpl">
- <propertyname="userdao"ref="userdao"></property>
- <propertyname="sendemails"ref="sendmail"></property>
- </bean>
- <bean id="RegisterAction"class="com.guang.action.RegisterAction"scope="prototype">
- <propertyname="userService"ref="userservice"></property>
- </bean>
- <beanid="ListAllUsers"class="com.guang.action.UserAction"scope="prototype">
- <propertyname="userService"ref="userservice"></property>
- </bean>
- <beanid="checkemail"class="com.guang.action.checkEmailAction"scope="prototype">
- <propertyname="userService"ref="userservice"></property>
- </bean>
- <beanid="login_"class="com.guang.action.LoginAction"scope="prototype">
- <propertyname="userService"ref="userservice"></property>
- </bean>
- <beanid="finishregister"class="com.guang.action.FinishregisterAction"scope="prototype">
- <propertyname="userService"ref="userservice"></property>
- </bean>
基本完成了。
希望对你有帮助。
- ssh+jmail实现,注册完发送邮件,点击链接之后完成注册。
- ssh+jmail实现,注册完发送邮件,点击链接之后完成注册。
- ssh+jmail实现,注册完发送邮件,点击链接之后完成注册。
- asp Jmail发送邮件 注册提示
- Jmail实现邮件发送
- 注册后自动发送邮件,通过链接验证注册成功
- jmail 发送邮件jmail组件注册成功仍然报错,报8007005
- java使用163邮箱完成发送邮件完成注册
- .NET实现注册发送邮件激活账户
- phpmailer实现发送注册验证邮件
- 教大家修改注册实现注册发送邮件的功能
- C# 发送注册邮件
- 注册发送激活邮件
- 注册邮件发送
- 注册页面完成。自动给所有邮箱发送激活邮件
- jMail实现电子 邮件发送
- asp.net 实现邮箱验证(注册的时候发送邮件激活链接的那种)
- 点击邮箱中的链接即可完成最后注册的思路
- win7下 取消自动播放
- sql注入词&语法
- C++ 运算符重载
- mysql事务处理
- 求质数(素数)算法思想
- ssh+jmail实现,注册完发送邮件,点击链接之后完成注册。
- 如何向android的framework里添加新类,新方法
- 整合FLEX+GAE+JAVA+BlazeDS
- Myeclipse快捷方式操作
- SD卡挂载流程浅析链接
- 启动OEM服务的方法
- 图形统计图
- 2012-7-14 搜索相关
- what is RRI