实现邮件发送功能

  • model类:MailServer.java

package com.mp.model.sys;

public class MailServer {
	String mailServerHost;
	String mailServerPort;
	String mailServerUserName;
	String mailServerPasswor;
	
	String title;
	String content;
	String receiveMail;
	
	
	
 
	public String getReceiveMail() {
		return receiveMail;
	}
	public void setReceiveMail(String receiveMail) {
		this.receiveMail = receiveMail;
	}
	public String getMailServerHost() {
		return mailServerHost;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public void setMailServerHost(String mailServerHost) {
		this.mailServerHost = mailServerHost;
	}
	public String getMailServerPort() {
		return mailServerPort;
	}
	public void setMailServerPort(String mailServerPort) {
		this.mailServerPort = mailServerPort;
	}
	public String getMailServerUserName() {
		return mailServerUserName;
	}
	public void setMailServerUserName(String mailServerUserName) {
		this.mailServerUserName = mailServerUserName;
	}
	public String getMailServerPasswor() {
		return mailServerPasswor;
	}
	public void setMailServerPasswor(String mailServerPasswor) {
		this.mailServerPasswor = mailServerPasswor;
	}
	
	
}

  • 配置文件

generator.properties

mailServerHost=smtp.163.com
mailServerPort=25
mailServerUserName=
mailServerPassword=



BaseGenerator.java

package com.mp.platform.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mp.util.ConfigurationHelper;

/**
 * 统一配置文件 地址
 */
public class BaseGenerator {

	private final Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private ConfigurationHelper configurationHelper;
	public String mailServerHost;
	public String mailServerPort;
	public String mailServerUserName;
	public String mailServerPassword;
	
	public String fblServer;
	
	private void init(){
		if(mailServerHost==null){
			mailServerHost = configurationHelper.getProperty("mailServerHost");
		}
		if(mailServerPort==null){
			mailServerPort = configurationHelper.getProperty("mailServerPort");
		}
		if(mailServerUserName==null){
			mailServerUserName = configurationHelper.getProperty("mailServerUserName");
		}
		if(mailServerPassword==null){
			mailServerPassword = configurationHelper.getProperty("mailServerPassword");
		}
		if(fblServer==null){
			fblServer = configurationHelper.getProperty("fblServer");
		}
	}

	public String getMailServerHost() {
		return mailServerHost;
	}

	public void setMailServerHost(String mailServerHost) {
		this.mailServerHost = mailServerHost;
	}

	public String getMailServerPort() {
		return mailServerPort;
	}

	public void setMailServerPort(String mailServerPort) {
		this.mailServerPort = mailServerPort;
	}

	public String getMailServerUserName() {
		return mailServerUserName;
	}

	public void setMailServerUserName(String mailServerUserName) {
		this.mailServerUserName = mailServerUserName;
	}

	public String getMailServerPassword() {
		return mailServerPassword;
	}

	public void setMailServerPassword(String mailServerPassword) {
		this.mailServerPassword = mailServerPassword;
	}

	public String getFblServer() {
		return fblServer;
	}

	public void setFblServer(String fblServer) {
		this.fblServer = fblServer;
	}

}

  • service

/**
  * 服务发送邮件
  * @return
  */
 public int emailsend( MailServer mailServer);

  • serviceimpl

package com.mp.service.sys.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Callable;

import javax.mail.Authenticator;
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 com.mp.common.PageDataUtils;
import com.mp.dao.sys.ISysAccountDao;
import com.mp.model.sys.MailServer;
import com.mp.model.sys.SysAccount;
import com.mp.model.sys.SysInvite;
import com.mp.service.base.impl.BaseServiceImpl;
import com.mp.service.sys.ISysAccountService;
import com.mp.util.Utils;

/**
 * @Description:实现类
 * @author
 * @version 1.0
 * @created
 */
public class SysAccountServiceImpl extends BaseServiceImpl<SysAccount, ISysAccountDao> implements ISysAccountService {
 protected static final String mailMessage = null;
 
 public int emailsend(final MailServer mailServer) {
  int ret=1;
  
  // 正式发送邮件
  try {
   Properties props = new Properties();
   props.put("mail.smtp.host", mailServer.getMailServerHost());//Smtp服务器地址
   props.put("mail.smtp.port", mailServer.getMailServerPort());//需要校验
   props.put("mail.smtp.auth", "true");//需要校验
   Session session = Session.getDefaultInstance(props,
    new Authenticator() {
     protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(mailServer .getMailServerUserName(), mailServer .getMailServerPasswor());// 登录用户名/密码
     }
    });
   session.setDebug(true);
   MimeMessage mimeMessage = new MimeMessage(session);

   mimeMessage.setFrom(new InternetAddress(mailServer .getMailServerUserName()));
   Multipart multipart = new MimeMultipart();
   mimeMessage.setRecipients(Message.RecipientType.TO, mailServer.getReceiveMail());// 收件人邮件
   mimeMessage.setSubject(mailServer.getTitle(), "GBK");

   // 正文
   MimeBodyPart body = new MimeBodyPart();
   body.setContent(mailServer.getContent(), "text/html;charset=GBK");
   multipart.addBodyPart(body);// 发件内容

   mimeMessage.setContent(multipart);
   mimeMessage.setSentDate(new Date());
   Transport.send(mimeMessage);
  } catch (Exception e) {
   e.printStackTrace();
   ret=-1;
  }
  
//	 ExecutorService exec=Executors.newSingleThreadExecutor();
//	 Future<Integer> future =exec.submit(new MallSendTools(content,receiveMail,props,mailServer));
//	 try {
//	 ret=future.get();
//	 } catch (InterruptedException e) {
//	 e.printStackTrace();
//	 } catch (ExecutionException e) {
//	 e.printStackTrace();
//	 }
//	 exec.shutdown();
  return ret;
 }
}

class MallSendTools implements Callable<Integer>{
 String content; 
 String receiveMail; 
 Properties props; 
 MailServer mailServer;
 int ret=1;
 MallSendTools(String content, String receiveMail, Properties props, final MailServer mailServer){
  this.content=content;
  this.receiveMail=receiveMail;
  this.props=props;
  this.mailServer=mailServer;
 }
 
 private int sendMail() {
  // 正式发送邮件
  try {
   Session session = Session.getDefaultInstance(props,
     new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
       return new PasswordAuthentication(mailServer .getMailServerUserName(), mailServer .getMailServerPasswor());// 登录用户名/密码
      }
     });
   session.setDebug(true);
   MimeMessage mimeMessage = new MimeMessage(session);

   mimeMessage.setFrom(new InternetAddress(mailServer .getMailServerUserName()));
   Multipart multipart = new MimeMultipart();
   mimeMessage.setRecipients(Message.RecipientType.TO, receiveMail);// 收件人邮件
   mimeMessage.setSubject("激活你的翻部落帐号", "GBK");

   // 正文
   MimeBodyPart body = new MimeBodyPart();
   body.setContent(content, "text/html;charset=GBK");
   multipart.addBodyPart(body);// 发件内容

   mimeMessage.setContent(multipart);
   mimeMessage.setSentDate(new Date());
   Transport.send(mimeMessage);
  } catch (Exception e) {
   e.printStackTrace();
   ret=-1;
  }
  return ret;
 }

 public Integer call() throws Exception {
  return sendMail();
 }
}

  • 功能实现

//邮件内容
 private String mailContent(String email,String ticket){
  StringBuffer content=new StringBuffer();
  content.append("<div id=\"mailContentContainer\" class=\"qmbox qm_con_body_content\"><style> ");
  content.append("	.mmsgLetter	 { width:580px;margin:0 auto;padding:10px;color:#333;background:#fff;border:0px solid #aaa;border:1px solid #aaa\\9;border-radius:5px;-webkit-box-shadow:3px 3px 10px #999;-moz-box-shadow:3px 3px 10px #999;box-shadow:3px 3px 10px #999;font-family:Verdana, sans-serif; } "); 
  content.append("	.mmsgLetter a:link, ");
  content.append("	.mmsgLetter a:visited {	color:#407700; } ");
  content.append("	.mmsgLetterContent {	text-align:left;padding:30px;font-size:14px;line-height:1.5;} ");
  content.append("	.mmsgLetterContent h3	{ color:#000;font-size:20px;font-weight:bold; margin:20px 0 20px;border-top:2px solid #eee;padding:20px 0 0 0;font-family:\"微软雅黑\",\"黑体\", \"Lucida Grande\", Verdana, sans-serif;} ");
  content.append("	.mmsgLetterContent p {	margin:20px 0;padding:0; } ");
  content.append("	.mmsgLetterContent .salutation { font-weight:bold;} ");
  content.append("	.mmsgLetterContent .mmsgMoreInfo { } ");
  content.append("	.mmsgLetterContent a.mmsgButton	 {	display:block;float:left;height:40px;text-decoration:none;text-align:center;cursor:pointer;} ");
  content.append("	.mmsgLetterContent a.mmsgButton	span {	display:block;float:left;padding:0 25px;height:40px;line-height:36px;font-size:14px;font-weight:bold;color:#fff;text-shadow:1px 0 0 #235e00;} ");
  content.append("	.mmsgLetterContent a.mmsgButton:link, ");
  content.append("	.mmsgLetterContent a.mmsgButton:visited { background:#338702 url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_btn.png) no-repeat right -40px; } ");
  content.append("	.mmsgLetterContent a.mmsgButton:link span, ");
  content.append("	.mmsgLetterContent a.mmsgButton:visited span { background:url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_btn.png) no-repeat 0 0; } ");
  content.append("	.mmsgLetterContent a.mmsgButton:hover, ");
  content.append("	.mmsgLetterContent a.mmsgButton:active { background:#338702 url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_btn.png) no-repeat right -120px; } ");
  content.append("	.mmsgLetterContent a.mmsgButton:hover span, ");
  content.append("	.mmsgLetterContent a.mmsgButton:active span { background:url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_btn.png) no-repeat 0 -80px; } ");
  content.append("	.mmsgLetterInscribe {	padding:40px 0 0;} ");
  content.append("	.mmsgLetterInscribe .mmsgAvatar	{	float:left; } ");
  content.append("	.mmsgLetterInscribe .mmsgName	{ margin:0 0 10px; } ");
  content.append("	.mmsgLetterInscribe .mmsgSender	{ margin:0 0 0 54px;} ");
  content.append("	.mmsgLetterInscribe .mmsgInfo	{ font-size:12px;margin:0;line-height:1.2; } ");
  content.append("	.mmsgLetterHeader	 {	height:23px;background:url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_bg_topline.png) repeat-x 0 0; } ");
  content.append("	.mmsgLetterFooter {	margin:16px;text-align:center;font-size:12px;color:#888; text-shadow:1px 0px 0px #eee;} ");
  content.append("	.mmsgLetterClr { clear:both;overflow:hidden;height:1px; } ");
  content.append("	 .mmsgLetterUser { padding:10px 0; } ");
  content.append("	 .mmsgLetterUserItem { padding:0 0 20px 0;} ");
  content.append("	 .mmsgLetterUserAvatar { height:40px;border:1px solid #ccc;padding:2px;display:block;float:left; } ");
  content.append("	 .mmsgLetterUserAvatar img { width:40px;height:40px; } ");
  content.append("	 .mmsgLetterInfo { margin-left:48px; } ");
  content.append("	 .mmsgLetterName { display:block;color:#5fa207;font-weight:bold;margin-left:10px; } ");
  content.append("	 .mmsgLetterDesc { font-size:12px;float:left;height:43px;background:url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_chat_right.gif) no-repeat right top; } ");
  content.append("	 .mmsgLetterDesc div{ white-space:nowrap;float:left;height:43px;padding:0 20px;line-height:40px;background:url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_chat_left.gif) no-repeat left top; } ");
  content.append("	 .mmsgLetterUser {} ");
  content.append("	 .mmsgLetterAvatar { float:left;} ");
  content.append("	 .mmsgLetterInfo { margin:0 0 0 60px; } ");
  content.append("	 .mmsgLetterNickName { font-size:14px;font-weight:bold;} ");
  content.append("	 .mmsgLetterUin { font-size:12px;color:#666;} ");
  content.append("	 .mmsgLetterUser { padding:10px 0; } ");
  content.append("	 .mmsgLetterUserItem { padding:0 0 20px 0;} ");
  content.append("	 .mmsgLetterUserAvatar { height:40px;border:1px solid #ccc;padding:2px;display:block;float:left; } ");
  content.append("	 .mmsgLetterUserAvatar img { width:40px;height:40px; } ");
  content.append("	 .mmsgLetterInfo { margin-left:48px; } ");
  content.append("</style> ");
  content.append("<div style=\"background-color:#d0d0d0;background-image:url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_bg.png);text-align:center;padding:40px;\"> ");
  content.append("	<div class=\"mmsgLetter\" style=\"width:580px;margin:0 auto;padding:10px;color:#333;background-color:#fff;border:0px solid #aaa;border-radius:5px;-webkit-box-shadow:3px 3px 10px #999;-moz-box-shadow:3px 3px 10px #999;box-shadow:3px 3px 10px #999;font-family:Verdana, sans-serif; \"> ");
  content.append("	 <div class=\"mmsgLetterHeader\" style=\"height:23px;background:url(http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_bg_topline.png) repeat-x 0 0;\"> ");
  content.append("	 </div> ");
  content.append("	 <div class=\"mmsgLetterContent\" style=\"text-align:left;padding:30px;font-size:14px;line-height:1.5;\"> ");
  content.append("	 <div> ");
  content.append("	 <p>你好!</p> ");
  content.append("	 <p> ");
  content.append("	 感谢你注册翻部落平台。 <br> ");
  content.append("	 你的登录邮箱为:<a href=\"mailto:"+email+"\" target=\"_blank\">"+email+"</a>。请点击以下链接激活帐号: ");
  content.append("	 </p> ");
  content.append("	 <p style=\"word-wrap:break-word;word-break:break-all;\"> ");
  content.append("	 <a href=\""+ticket+"\" target=\"_blank\">"+ticket+"</a> ");
  content.append("	 </p> ");
  content.append("	 <p> ");
  content.append("	 如果以上链接无法点击,请将上面的地址复制到你的浏览器(如IE)的地址栏激活您的账号。 (该链接在48小时内有效,48小时后需要重新注册) ");
  content.append("	 </p> ");
  content.append("	 </div>	 ");
//	 content.append("	 <div class=\"mmsgLetterInscribe\" style=\"padding:40px 0 0;\"> ");
//	 content.append("	 <img class=\"mmsgAvatar\" src=\"http://weixin.qq.com/zh_CN/htmledition/images/weixin/letter/mmsgletter_2_avatar_01.png\" style=\"float:left;\"> ");
//	 content.append("	 <div class=\"mmsgSender\" style=\"margin:0 0 0 54px;\"> ");
//	 content.append("	 <p class=\"mmsgName\" style=\"margin:0 0 10px;\">Claire Wang</p> ");
//	 content.append("	 <p class=\"mmsgInfo\" style=\"font-size:12px;margin:0;line-height:1.2;\"> ");
//	 content.append("	 微信产品经理<br> ");
//	 content.append("	 <a href=\"mailto:[email protected]\" style=\"color:#407700;\" target=\"_blank\">[email protected]</a> ");
//	 content.append("	 </p> ");
//	 content.append("	 </div> ");
//	 content.append("	 </div>	 ");
  content.append("	 </div> ");
  content.append("	 <div class=\"mmsgLetterFooter\" style=\"margin:16px;text-align:center;font-size:12px;color:#888;text-shadow:1px 0px 0px #eee;\"> ");
  content.append("	 </div> ");
  content.append("	</div> ");
  content.append("</div> ");
  content.append("</div> ");
  return content.toString();
 }

//发送邮件
  MailServer mailServer= new MailServer();
  mailServer.setMailServerUserName(baseGenerator.getMailServerUserName());
  mailServer.setMailServerPasswor(baseGenerator.getMailServerPassword());
  mailServer.setTitle("翻部落帐号激活");
  mailServer.setContent(mailContent(sysAccount.getMail(),baseGenerator.getFblServer()+"activateemail?email="+sysAccount.getId()+"&ticket="+ticket));
  mailServer.setReceiveMail(sysAccount.getMail());
  mailServer.setMailServerHost(baseGenerator.getMailServerHost());
  mailServer.setMailServerPort(baseGenerator.getMailServerPort());
  int email = sysAccountService.emailsend(mailServer);

你可能感兴趣的:(实现邮件发送功能)