天天看點

基于Javamail發送郵件(QQ/網易郵件伺服器)

一. 使用QQ郵箱作為smtp郵件伺服器發送郵件

步驟1.開啟QQ郵箱的POP3/SMTP服務:

基于Javamail發送郵件(QQ/網易郵件伺服器)

開啟後會得到一個16位授權碼, 作為第三方使用郵件伺服器的登入憑證.

注意: 修改郵箱密碼後, 授權碼會失效, 需要重新擷取.

步驟2: 編寫配置檔案applicationContext-email.xml(此處使用xml配置方式):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd"
	default-lazy-init="false">
	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host">
			<!-- qq的SMTP郵箱伺服器位址, 若使用163網易則改為:smtp.163.com -->
			<value>smtp.qq.com</value>
		</property>
<!-- 	<property name="port">
			SMTP郵箱伺服器端口(465或587), 建議不要配置, 使用預設就行 
			<value>建議不要配置!!部落客配置反而釋出出去!!</value>
		</property> -->
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
				<!-- 連接配接逾時時間 -->
				<prop key="mail.smtp.timeout">25000</prop>
			</props>
		</property>
		<!-- 你的郵箱賬号 -->
		<property name="username">
			<value>[email protected]</value>
		</property>
		<!-- 16位授權碼, 注意不是登入密碼! -->
		<property name="password">
			<value>qazcrslpoghcbahh</value>
		</property>
		<property name="defaultEncoding">
			<value>UTF-8</value>
		</property>
	</bean>

	<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
		<!-- 發件人資訊, 需要和上面username值一樣 -->
		<property name="from" value="[email protected]" />
	</bean>

</beans>
           

步驟3: 編寫測試類:

package emailtest;

import java.util.Date;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-email.xml")
public class EmailTest {

	@Resource
	private JavaMailSender javaMailSender;

	@Resource
	private SimpleMailMessage simpleMailMessage;
	
	@Test
	public void sendMail() throws MessagingException{
		sendMail("[email protected]","驗證碼:6666","密碼找回");
	}
	
	public void sendMail(String email, String content, String subject) throws MessagingException {
		MimeMessage message = javaMailSender.createMimeMessage();
		MimeMessageHelper messageHelper;
		messageHelper = new MimeMessageHelper(message, true, "UTF-8");
		messageHelper.setFrom(StringUtils.trimAllWhitespace(simpleMailMessage.getFrom()));
		messageHelper.setTo(email);
		messageHelper.setSubject(subject);
		messageHelper.setText(content, true);
		messageHelper.setSentDate(new Date());
		// 發送郵件
		javaMailSender.send(messageHelper.getMimeMessage());
		
	}
}

           

二. 使用網易郵箱作為smtp郵件伺服器發送郵件

1.相似的, 先打開網易郵箱的POP3/SMTP服務, 設定授權碼.

基于Javamail發送郵件(QQ/網易郵件伺服器)

2.修改上述applicationContext.xml中配置資訊:

伺服器位址改為smtp.163.com
	username更改為你的網易郵箱賬号
	password則是你在開啟POP3/SMTP服務時設定的授權碼
	from的值和username值一樣.