網絡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 "請檢查您的郵箱是否收到驗證碼,註冊成功後此郵箱爲登錄帳號。";
    }
}
相關文章
相關標籤/搜索