1 package com.xxx.util; 2 3 import java.util.Date; 4 import java.util.Properties; 5 6 import javax.mail.Address; 7 import javax.mail.BodyPart; 8 import javax.mail.Message; 9 import javax.mail.MessagingException; 10 import javax.mail.Multipart; 11 import javax.mail.Session; 12 import javax.mail.Transport; 13 import javax.mail.internet.InternetAddress; 14 import javax.mail.internet.MimeBodyPart; 15 import javax.mail.internet.MimeMessage; 16 import javax.mail.internet.MimeMultipart; 17 18 public class SimpleMailSender { 19 public boolean sendTextMail(MailSenderInfo mailInfo) { 20 // 判斷是否須要身份認證 21 MyAuthenticator authenticator = null; 22 Properties pro = mailInfo.getProperties(); 23 if (mailInfo.isValidate()) { 24 // 若是須要身份認證,則建立一個密碼驗證器 25 authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 26 } 27 // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session 28 Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 29 try { 30 // 根據session建立一個郵件消息 31 Message mailMessage = new MimeMessage(sendMailSession); 32 // 建立郵件發送者地址 33 Address from = new InternetAddress(mailInfo.getFromAddress()); 34 // 設置郵件消息的發送者 35 mailMessage.setFrom(from); 36 // 建立郵件的接收者地址,並設置到郵件消息中 37 Address to = new InternetAddress(mailInfo.getToAddress()); 38 mailMessage.setRecipient(Message.RecipientType.TO,to); 39 // 設置郵件消息的主題 40 mailMessage.setSubject(mailInfo.getSubject()); 41 // 設置郵件消息發送的時間 42 mailMessage.setSentDate(new Date()); 43 // 設置郵件消息的主要內容 44 String mailContent = mailInfo.getContent(); 45 mailMessage.setText(mailContent); 46 // 發送郵件 47 Transport.send(mailMessage); 48 return true; 49 } catch (MessagingException ex) { 50 ex.printStackTrace(); 51 } 52 return false; 53 } 54 55 /** 56 * 以HTML格式發送郵件 57 * @param mailInfo 待發送的郵件信息 58 */ 59 public static boolean sendHtmlMail(MailSenderInfo mailInfo){ 60 // 判斷是否須要身份認證 61 MyAuthenticator authenticator = null; 62 Properties pro = mailInfo.getProperties(); 63 //若是須要身份認證,則建立一個密碼驗證器 64 if (mailInfo.isValidate()) { 65 authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 66 } 67 // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session 68 Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 69 try { 70 // 根據session建立一個郵件消息 71 Message mailMessage = new MimeMessage(sendMailSession); 72 // 建立郵件發送者地址 73 Address from = new InternetAddress(mailInfo.getFromAddress()); 74 // 設置郵件消息的發送者 75 mailMessage.setFrom(from); 76 // 建立郵件的接收者地址,並設置到郵件消息中 77 Address to = new InternetAddress(mailInfo.getToAddress()); 78 // Message.RecipientType.TO屬性表示接收者的類型爲TO 79 mailMessage.setRecipient(Message.RecipientType.TO,to); 80 // 設置郵件消息的主題 81 mailMessage.setSubject(mailInfo.getSubject()); 82 // 設置郵件消息發送的時間 83 mailMessage.setSentDate(new Date()); 84 // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象 85 Multipart mainPart = new MimeMultipart(); 86 // 建立一個包含HTML內容的MimeBodyPart 87 BodyPart html = new MimeBodyPart(); 88 // 設置HTML內容 89 html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); 90 mainPart.addBodyPart(html); 91 // 將MiniMultipart對象設置爲郵件內容 92 mailMessage.setContent(mainPart); 93 // 發送郵件 94 Transport.send(mailMessage); 95 return true; 96 } catch (MessagingException ex) { 97 ex.printStackTrace(); 98 } 99 return false; 100 } 101 }
1 package com.xxx.util; 2 3 import java.util.Properties; 4 5 public class MailSenderInfo { 6 7 // 發送郵件的服務器的IP和端口 8 private String mailServerHost; 9 private String mailServerPort = "25"; 10 11 // 郵件發送者的地址 12 private String fromAddress; 13 14 // 郵件接收者的地址 15 private String toAddress; 16 17 // 登錄郵件發送服務器的用戶名和密碼 18 private String userName; 19 private String password; 20 21 // 是否須要身份驗證 22 private boolean validate = false; 23 24 // 郵件主題 25 private String subject; 26 27 // 郵件的文本內容 28 private String content; 29 30 // 郵件附件的文件名 31 private String[] attachFileNames; 32 /** 33 * 得到郵件會話屬性 34 */ 35 public Properties getProperties(){ 36 Properties p = new Properties(); 37 p.put("mail.smtp.host", this.mailServerHost); 38 p.put("mail.smtp.port", this.mailServerPort); 39 p.put("mail.smtp.auth", validate ? "true" : "false"); 40 return p; 41 } 42 public String getMailServerHost() { 43 return mailServerHost; 44 } 45 public void setMailServerHost(String mailServerHost) { 46 this.mailServerHost = mailServerHost; 47 } 48 public String getMailServerPort() { 49 return mailServerPort; 50 } 51 public void setMailServerPort(String mailServerPort) { 52 this.mailServerPort = mailServerPort; 53 } 54 public boolean isValidate() { 55 return validate; 56 } 57 public void setValidate(boolean validate) { 58 this.validate = validate; 59 } 60 public String[] getAttachFileNames() { 61 return attachFileNames; 62 } 63 public void setAttachFileNames(String[] fileNames) { 64 this.attachFileNames = fileNames; 65 } 66 public String getFromAddress() { 67 return fromAddress; 68 } 69 public void setFromAddress(String fromAddress) { 70 this.fromAddress = fromAddress; 71 } 72 public String getPassword() { 73 return password; 74 } 75 public void setPassword(String password) { 76 this.password = password; 77 } 78 public String getToAddress() { 79 return toAddress; 80 } 81 public void setToAddress(String toAddress) { 82 this.toAddress = toAddress; 83 } 84 public String getUserName() { 85 return userName; 86 } 87 public void setUserName(String userName) { 88 this.userName = userName; 89 } 90 public String getSubject() { 91 return subject; 92 } 93 public void setSubject(String subject) { 94 this.subject = subject; 95 } 96 public String getContent() { 97 return content; 98 } 99 public void setContent(String textContent) { 100 this.content = textContent; 101 } 102 }
以上是兩個工具類,下面是action裏面的使用html
1 //找回密碼 2 public String findpwd(){ 3 try { 4 boolean result = userService.checkEmail(item.getEmail()); 5 int randNum= (int) (((Math.random()+1)*1000000)); 6 if (result) { 7 MailSenderInfo mailInfo = new MailSenderInfo(); 8 mailInfo.setMailServerHost("smtp.126.com"); 9 mailInfo.setMailServerPort("25"); 10 mailInfo.setValidate(true); 11 mailInfo.setUserName("chuguozhan@126.com"); 12 mailInfo.setPassword("60514895a");//您的郵箱密碼 13 mailInfo.setFromAddress("chuguozhan@126.com"); 14 mailInfo.setToAddress(item.getEmail()); 15 mailInfo.setSubject("密碼找回:"); 16 mailInfo.setContent("您的重置密碼是:"+String.valueOf(randNum)); 17 18 SimpleMailSender sms = new SimpleMailSender(); 19 if(sms.sendHtmlMail(mailInfo)){ 20 User user = userService.getUserByEmail(item.getEmail()); 21 user.setPassword(String.valueOf(randNum)); 22 userService.doModifyPwd(user); 23 log.info("findpwd success!!!"); 24 resultMap.put("retcode", RetCode.SUCCESS); 25 resultMap.put("retmsg", "重置密碼已發送到了您的註冊郵箱"); 26 }else{ 27 log.info("findpwd fail!!!"); 28 resultMap.put("retcode", RetCode.FAIL); 29 resultMap.put("retmsg", "密碼找回失敗"); 30 } 31 }else{ 32 log.info("checkPwd fail!!!"); 33 resultMap.put("retcode", RetCode.CHECK); 34 resultMap.put("retmsg", "郵箱不存在,請確認您輸入的郵箱是否正確!"); 35 } 36 } catch (Exception e) { 37 log.error("findpwd is bug",e); 38 resultMap.put("retcode", RetCode.UNKOWN_WRONG); 39 resultMap.put("retmsg", "密碼找回出現錯誤!"); 40 } 41 return SUCCESS; 42 }
Dao層和Service層功能實現就不貼了java