package com.nseer.trade.utils; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.transaction.Transaction; import org.apache.struts2.ServletActionContext; import com.sun.org.apache.regexp.internal.recompile; /** * 邮件发送的工具类2017-6-6 * @author Administrator * */ public class MailUtils { /** * account_safety.jsp * 发送验证邮件 * @param to 验证者的邮箱 * @param code 验证码 * @throws Exception */ public static void verifyMail(String to,String code) throws Exception{ HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); String url=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+request.getContextPath(); String url1=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); //1.创建连接对象,连接到邮箱服务器 Properties props=new Properties(); props.put("mail.smtp.host","smtp.exmail.qq.com"); props.put("mail.transport.protocol","smtp"); props.put("mail.smtp.auth","true"); Session session=Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("houjianjun@yhbest.com.cn", "Hjj123456"); } }); // session.setDebug(true); //2.创建邮件对象 Message message = new MimeMessage(session); //2.1设置发件人 message.setFrom(new InternetAddress("houjianjun@yhbest.com.cn")); //2.2设置收件人 message.setRecipient(RecipientType.TO, new InternetAddress(to)); //2.3设置邮件的主题 message.setSubject("邮箱验证邮件"); //2.4设置邮件的正文 message.setContent("<table border=‘0‘ cellpadding=‘0‘ cellspacing=‘0‘ width=‘100%‘><tr><td style=‘padding: 10px 0 30px 0;‘>" + "<table align=‘center‘ border=‘0‘ cellpadding=‘0‘ cellspacing=‘0‘ width=‘600‘ style=‘border: 1px solid #2f74c1; border-collapse: collapse;‘><tr>" + "<td align=‘left‘ bgcolor=‘#FFFFFF‘ style=‘padding: 20px 0 20px 30px; font-size: 28px; font-weight: bold; font-family: Arial, sans-serif;‘><a href=‘"+url1+"‘><img src=‘http://www.gxsme.org.cn/r/cms/www/cms/img20160622/top_title.png‘ alt=‘银河贝思特logo‘ width=‘397‘ height=‘72‘ style=‘display: block;‘ /></a></td></tr>" + "<tr><td align=‘left‘ bgcolor=‘#2f74c1‘ style=‘padding: 0px 0 0px 0px; height: 60px; font-size: 16px; font-weight: bold; font-family: Arial, sans-serif;‘><ul style=‘list-style: none;padding: 0px;text-align: center;‘>" + "<li style=‘float: left;margin-top: -10px;color: #FFFFFF;padding:0px 15px;list-style-type:none;line-height:60px ;‘><a href=‘"+url+"/web/toproductlist.action?‘ style=‘text-decoration: none;color: #FFFFFF;‘>找服务</a></li>" + "<li style=‘float: left;margin-top: -10px;color: #FFFFFF;padding:0px 15px;list-style-type:none;line-height:60px ;‘><a href=‘"+url+"/web/towebUnit.action?‘ style=‘text-decoration: none;color: #FFFFFF;‘>找机构</a></li>" + "<li style=‘float: left;margin-top: -10px;color: #FFFFFF;padding:0px 15px;list-style-type:none;line-height:60px ;‘><a href=‘"+url+"/web/demandFileList.action‘ style=‘text-decoration: none;color: #FFFFFF;‘>找需求</a></li>" + "<li style=‘float: left;margin-top: -10px;color: #FFFFFF;padding:0px 15px;list-style-type:none;line-height:60px ;‘><a href=‘"+url+"/web/listFrom.action?‘ style=‘text-decoration: none;color: #FFFFFF;‘>找活动</a></li>" + "<li style=‘float: left;margin-top: -10px;color: #FFFFFF;padding:0px 15px;list-style-type:none;line-height:60px ;‘><a href=‘"+url+"/web/expertList.action‘ style=‘text-decoration: none;color: #FFFFFF;‘>找专家</a></li>" + "<li style=‘float: left;margin-top: -10px;color: #FFFFFF;padding:0px 15px;list-style-type:none;line-height:60px ;‘><a href=‘"+url+"/web/zwfwproject.action‘ style=‘text-decoration: none;color: #FFFFFF;‘>项目申报</a></li>" + "<li style=‘float: left;margin-top: -10px;color: #FFFFFF;padding:0px 15px;list-style-type:none;line-height:60px ;‘><a href=‘"+url1+"/index.htm‘ style=‘text-decoration: none;color: #FFFFFF;‘>政策信息</a></li></ul></td></tr>" + "<tr bgcolor=‘#FFFFFF‘ style=‘padding: 30px 30px 30px 30px;‘><td bgcolor=‘#ffffff‘ style=‘padding: 30px 30px 30px 30px;‘><table border=‘0‘ cellpadding=‘0‘ cellspacing=‘0‘ width=‘100%‘><tr>" + "<td style=‘color: #333333; font-family: Arial, sans-serif; font-size: 16px;‘>亲爱的<b>"+to+"</b>:</td></tr><tr><td style=‘padding: 10px 0 0px 0; color: #333333; font-family: Arial, sans-serif; font-size: 16px; line-height: 32px;‘>" + "<p style=‘text-align: justify;text-indent: 2em;‘>您好!您的验证码为:<span style=‘color:#E19F4F;‘><b>"+code+"</b></span> 您正在进行修改安全邮箱操作,请将此验证码输入验证码输入框,以完成邮箱校验。30分钟以内有效,超过30分钟则需重新获取验证码。</p><p style=‘text-align: justify;text-indent: 2em;‘>" + "您在广西中小企业公共服务平台发布服务需求或申请服务项目是需要通过输入帐户名和密码登录的,因此请您牢记注册信息。</p><p style=‘text-align: right;‘>广西中小企业公共服务平台团队敬上</p></td></tr></table></td></tr>" + "<tr><td bgcolor=‘#FFFFFF‘ style=‘padding: 20px 30px 30px 20px;border-top:1px solid #2f74c1 ;‘><table border=‘0‘ cellpadding=‘0‘ cellspacing=‘0‘ width=‘100%‘><tr><td style=‘color: #333333; font-family: Arial, sans-serif; font-size: 16px;‘ width=‘100%‘>" + "<p>本邮件由广西中小企业公共服务平台系统自动发出,<span style=‘color:#FF0000;‘>请勿直接回复</span>。</p><p>如果您有任何疑问或建议,请联系我们或致电客服电话,400-990-4618</p></td></tr></table></td></tr></table></td></tr></table>", "text/html;charset=UTF-8"); //3.发送一封激活邮件 Transport transport = session.getTransport(); transport.connect(); message.saveChanges(); transport.sendMessage(message, message.getRecipients(RecipientType.TO)); transport.close(); } }