Web发送邮件
1、首先注册一个163邮箱
自己的邮箱地址是xyqq769552629@163.com
登陆的密码是自己设定
使用邮箱发邮件,邮件必须开启pop和smtp服务,登陆邮件
开启SMTP服务,这个时候提示你必须设置授权的密码,
pom依赖:
<dependency> <groupId>activation</groupId> <artifactId>activation</artifactId> <version>1.0.2</version> </dependency>
<dependency> <groupId>activation</groupId> <artifactId>activation</artifactId> <version>1.0.2</version> </dependency>
实现代码如下:
1、controller:
@RequestMapping("/RegisterServlet") public ModelAndView RegisterServlet(String username,String password,String email) throws Exception { ModelAndView mv = new ModelAndView(); try{ usermail user = new usermail(); user.setEmail(email); user.setPassword(password); user.setUsername(username); System.out.println("把用户信息注册到数据库中"); //用户注册成功之后就使用用户注册时的邮箱给用户发送一封Email //发送邮件是一件非常耗时的事情,因此这里开辟了另一个线程来专门发送邮件 Sendmail send = new Sendmail(user); //启动线程,线程启动之后就会执行run方法来发送邮件 send.start(); //注册用户 //new UserService().registerUser(user); mv.addObject("message", "恭喜您,注册成功,我们已经发了一封带了注册信息的电子邮件,请查收,如果没有收到,可能是网络原因,过一会儿就收到了!!"); mv.setViewName("message"); }catch (Exception e) { e.printStackTrace(); mv.addObject("message", "注册失败!!"); mv.setViewName("message"); } return mv; }
2、Sendmail
package com.sihai.utils; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.sihai.pojo.usermail; /** * @ClassName: Sendmail * @Description: Sendmail类继承Thread,因此Sendmail就是一个线程类,这个线程类用于给指定的用户发送Email * @author: xyq * @date: 2015-1-12 下午10:43:48 * */ public class Sendmail extends Thread { //用于给用户发送邮件的邮箱 private String from = "xxxx@163.com"; //邮箱的用户名 private String username = "xxxxx"; //邮箱的密码//163的授权密码 private String password = "xxxxxxxx"; //发送邮件的服务器地址 private String host = "smtp.163.com"; private usermail user; public Sendmail(usermail user){ this.user = user; } /* 重写run方法的实现,在run方法中发送邮件给指定的用户 * @see java.lang.Thread#run() */ @Override public void run() { try{ Properties prop = new Properties(); prop.setProperty("mail.host", host); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.smtp.auth", "true"); Session session = Session.getInstance(prop); session.setDebug(true); Transport ts = session.getTransport(); ts.connect(host, username, password); Message message = createEmail(session,user); ts.sendMessage(message, message.getAllRecipients()); ts.close(); }catch (Exception e) { throw new RuntimeException(e); } } /** * @Method: createEmail * @Description: 创建要发送的邮件 * @Anthor:孤傲苍狼 * * @param session * @param user * @return * @throws Exception */ public Message createEmail(Session session,usermail user) throws Exception{ MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); message.setSubject("用户注册邮件"); String info = "恭喜您注册成功,您的用户名:" + user.getUsername() + ",您的密码:" + user.getPassword() + ",请妥善保管,如有问题请联系网站客服!!"; message.setContent(info, "text/html;charset=UTF-8"); message.saveChanges(); return message; } }
3、usermail
package com.sihai.pojo; public class usermail { private String username; private String password; private String email; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
4、register.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>注册页面</title> </head> <body> <form action="user/RegisterServlet.action" method="post"> 用户名:<input type="text" name="username"><br/> 密码:<input type="password" name="password"><br/> 邮箱:<input type="text" name="email"><br/> <input type="submit" value="注册"> </form> </body> </html>
5、message.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>消息提示页面</title> </head> <body> ${message} </body> </html>
到这里基本就实现了发送邮件