网络SMTP:JavaMail+腾讯SMTP服务

说明

版本一不能整合到Spring,版本二已经整合到Springphp

版本1

package com.ddouble.util.email;
/**
 * 	
 * 	SMTP(simple mail transfer protocol)
 * 	邮件发送工具
 * 	@author Ddouble
 *     copyright all reserved  2016 by author in  AHU(CN)
 *     @desc
 *     一、【问题】不能整合到spring框架,如今已经迁移使用spring自带的JavaMailSender
 *     二、【问题】由于从jaee5开始jdk集成JavaMail和JavaAction,本身引用新的这两个包
 *                            会有版本冲突(尤为在集成到框架并部署到服务器上)
 *     三、【问题】SSL认证问题。jdk8会影响邮箱服务的认证,须要更换现有jdk的jce环境
 *     					详情参考:http://www.cnblogs.com/LUA123/p/6039954.html
 *     四、【问题】腾讯SMTP开启,自行百度。
 */
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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.MimeMultipart;


public class EmailUtil {
	private String host = "smtp.qq.com";//smtp 服务器 邮箱服务器 地址
	private String user = "23530087";//smtp服务器的account
	private String pswd = "goeqsdnqmntfbbgg";//smtp服务的受权码
	private String from = "23530087@qq.com";//发送者的邮箱
	private String to ="235555555@qq.com";//接受者的邮箱
	private String subject = "Ddouble校验码";
	
	public void setAddress(String from, String to, String subject){
		this.from = from;
		this.to = to;
		this.subject = subject;
	}
	
	@SuppressWarnings("static-access")
	public void send(String text){
		Properties properties = new Properties();
		properties.put("mail.smtp.protocol", "smtps");
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.port", "587");//465   ||  587腾讯smtp服务器的smtp服务端口
	    properties.put("mail.smtp.starttls.enable", "true");
	    properties.put("mail.smtp.ssl.trust", host);
		Session session = Session.getDefaultInstance(properties);
		session.setDebug(true);
		MimeMessage message = new MimeMessage(session);

		try {
			//sender who send this email
			message.setFrom(new InternetAddress(from));
			//receiver who will get this email
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			//title of this email
			message.setSubject(subject);
			//multipart of this email
			Multipart multipart = new MimeMultipart();
			BodyPart bodyPart = new MimeBodyPart();
			bodyPart.setContent(text, "text/html;charset=utf-8");
			multipart.addBodyPart(bodyPart);
			message.setContent(multipart);
			//save email
			message.saveChanges();
			//send email
			Transport transport = session.getTransport("smtp");
			transport.connect(host, user, pswd);
			transport.sendMessage(message, message.getAllRecipients());//(message, message.getAllRecipients());
			transport.close();
		} catch (AddressException e) {		
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}		
	}
	
//	public static void main(String args[]){
//		EmailUtil util = new EmailUtil();
//		util.send("ceddddhi"); 
//		System.out.println(11);
//	}
}

版本2 SpringSMTP服务

1 引入pom.xml依赖

<!-- mail -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>

2 applicationContext.xml中配置mail的bean,smtp服务的设置

<bean id="mailMessage"  class ="org.springframework.mail.SimpleMailMessage">
        <property name="from" value="9XXXXXX6@qq.com"/>
    </bean>
    
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.qq.com"/>
        <property name="port" value="587"/>
        <property name="username" value="9XXXXXXXX6"/>
        <property name="password" value="hhdiphpvfivvbfih"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.timeout">25000</prop>
                <prop key="mail.debug">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
     			<prop key="mail.smtp.ssl.trust">smtp.qq.com</prop>
            </props>
        </property>	
    </bean>

3使用

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import com.ddouble.mapper.CommUsrRgrTblMapper;
import com.ddouble.pojo.CommUsrRgrTbl;
import com.ddouble.service.UserService;
import com.ddouble.util.encrypt.GeneralRandomString;
@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
    
    @Autowired
    private CommUsrRgrTblMapper commUsrRgrTblMapper;
    
    @Autowired
    SimpleMailMessage mailMessage;
    
    @Autowired
    MailSender mailSender;
    
    @Override
    public String getRegisterCheck(String email) {
        mailMessage.setSubject("Ddouble注册码");
        String check = GeneralRandomString.getRandomStringByLength(4);
        mailMessage.setText(check);
        mailMessage.setTo(email);
        mailSender.send(mailMessage);
        //TODO 把验证码加入数据库		
        CommUsrRgrTbl record = new CommUsrRgrTbl();
        record.setCheck(check);
        record.setEmail(email);
        record.setStatus(0);
        commUsrRgrTblMapper.insert(record);
        return "请检查您的邮箱是否收到验证码,注册成功后此邮箱为登录帐号。";
    }
}
相关文章
相关标签/搜索