import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* Created by Healist on 2017/1/29.
*/
public class Email {
public static void main(String[] args) throws MessagingException {
// å建Properties ç±»ç¨äºè®°å½é®ç®±çä¸äºå±æ§
Properties props = new Properties();
// 表示SMTPåéé®ä»¶ï¼å¿
é¡»è¿è¡èº«ä»½éªè¯
props.put("mail.smtp.auth", "true");
//æ¤å¤å¡«åSMTPæå¡å¨
props.put("mail.smtp.host", "smtp.qq.com");
//端å£å·ï¼QQé®ç®±ç»åºäºä¸¤ä¸ªç«¯å£ï¼ä½æ¯å¦ä¸ä¸ªä¸ç®¡ç¨ï¼è¿ä¸ª587å¨è¿æ¯ç®¡ç¨ç
props.put("mail.smtp.port", "587");
// æ¤å¤å¡«åä½ çè´¦å·
props.put("mail.user", "[email protected]");
// QQé®ç®±çææç ï¼å»é®ç®±èªå·±è·å
props.put("mail.password", "dffajaehrb");
// æ建ææä¿¡æ¯ï¼ç¨äºè¿è¡SMTPè¿è¡èº«ä»½éªè¯
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// ç¨æ·åãå¯ç
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使ç¨ç¯å¢å±æ§åææä¿¡æ¯ï¼å建é®ä»¶ä¼è¯
Session mailSession = Session.getInstance(props, authenticator);
// å建é®ä»¶æ¶æ¯
MimeMessage message = new MimeMessage(mailSession);
// 设置å件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// 设置æ¶ä»¶äººçé®ç®±
InternetAddress to = new InternetAddress("[email protected]");
message.setRecipient(MimeMessage.RecipientType.TO, to);
// 设置é®ä»¶æ é¢
message.setSubject("æµè¯é®ä»¶");
// 设置é®ä»¶çå
容ä½
message.setContent("è¿æ¯ä¸å°æµè¯é®ä»¶", "text/html;charset=UTF-8");
// åéé®ä»¶
Transport.send(message);
}
}
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<context:component-scan base-package="com.healist.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!-- å¼å
¥é
ç½®æ件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dev.properties</value>
<!--å¦ææå¤ä¸ªé
ç½®æ件ï¼å¨è¿é继ç»æ·»å -->
</list>
</property>
</bean>
<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>
<tx:annotation-driven />
</beans>