一、今天做發郵件功能測試一直報: authentication failed 的錯誤。搜索了下資料才知道,一般的企業內部郵箱不需要認證,但是QQ的已經需要認證才能用。原來好像QQ郵件不需要認證,所以網上很多資料都不行。然后多方找資料后找到個,分享出來。具體步驟如下
寫個簡單的main發郵件,代碼如下
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; public class Email { public void send(ApplicationContext ctx) { JavaMailSender sender = (JavaMailSender) ctx.getBean("javaMailSender"); SimpleMailMessage mail = new SimpleMailMessage(); try { mail.setTo("222222222@qq.com");// 接受者 mail.setFrom("111111111@qq.com");// 發送者, mail.setSubject("測試主題");// 主題 mail.setText("發送郵件內容測試");// 郵件內容 sender.send(mail); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring-servlet.xml"); Email e = new Email(); e.send(ctx); } }
spring-servlet.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:email.properties</value> <!--要是有多個配置文件,只需在這里繼續添加即可 --> </list> </property> </bean> <!-- 啟用spring mvc 注解 --> <context:annotation-config /> <!-- 設置使用注解的類所在的jar包 --> <context:component-scan base-package="mail"></context:component-scan> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.smtp.host}" /> <property name="port" value="${mail.smtp.port}" /> <property name="username" value="${mail.smtp.username}" /> <property name="password" value="${mail.smtp.password}" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop> <prop key="mail.smtp.auth">${mail.smtp.auth}</prop> <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop> <prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> <prop key="mail.smtp.socketFactory.fallback">false</prop> </props> </property> </bean> <!-- 對轉向頁面的路徑解析。prefix:前綴, suffix:后綴 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/" p:suffix=".jsp" /> </beans>
email.properties
mail.smtp.host=smtp.qq.com mail.smtp.auth=true mail.smtp.timeout=50000 mail.smtp.username=1111111@qq.com
#這個是QQ郵箱的授權碼,獲取的方式,打開你的郵箱(設置 -> 賬戶 -> POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務 -> POP3/SMTP服務 ,點擊開啟,短信驗證后就會獲得授權碼)
mail.smtp.password=iklnzunrgdzkcbab mail.smtp.port=465 mail.smtp.starttls.enable=true