package EMAIL;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

/**
* Created by jianghuiwen on 17/3/5.
*/
public class SendEmail {

static ApplicationContext actx = new ClassPathXmlApplicationContext(
"email.xml");
static MailSender sender = (MailSender) actx.getBean("mailSender");
static SimpleMailMessage mailMessage = (SimpleMailMessage) actx.getBean("mailMessage");

public static void main(String[] args){
for(int i =0;i<1;i++){
mailMessage.setSubject("你好");
mailMessage.setText("<html><head></head><body><h1>hello!!spring image html mail</h1>\"\n" +
"\t\t\t\t\t+\"<a href=http://www.baidu.com>baidu</a>\"\t+ \"<img src='https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png'/></body></html>");
mailMessage.setTo("15255123122@163.com");
sender.send(mailMessage);
}

}
}
package EMAIL;

import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;

/**
* Created by jianghuiwen on 17/3/5.
*/
public class SendHtmlMail {
static ApplicationContext actx = new ClassPathXmlApplicationContext(
"email.xml");
static MailSender sender = (MailSender) actx.getBean("mailSender");
static SimpleMailMessage mailMessage = (SimpleMailMessage) actx.getBean("mailMessage");

public static void main(String[] args) throws MessagingException, javax.mail.MessagingException {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"email.xml");
JavaMailSenderImpl sender = (JavaMailSenderImpl) ctx
.getBean("mailSender");
MimeMessage mailMessage = sender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true);
messageHelper.setFrom("test@163.com.com");
messageHelper.setTo("test111@163.com");

messageHelper.setSubject("测试邮件中嵌套图片!!");
// true 表示启动HTML格式的邮件
messageHelper.setText(
"<html><head></head><body><h1>hello!!spring image html mail</h1>"
+"<a href=http://www.baidu.com>baidu</a>" + "<img src='https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top_ca79a146.png'/></body></html>", true);

// FileSystemResource img = new FileSystemResource(new File("单.png"));
//
// messageHelper.addInline("image", img);//跟cid一致

sender.send(mailMessage);
System.out.println("邮件发送成功...");

}
}

发送配置

mail.host=smtp.163.com
mail.username=test@163.com
mail.password=123456,
mail.from=test@163.com
mail.port=465

spring配置

<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<value>${mail.from}</value>
</property>
<!-- 查看SimpleMailMessage源码还可以注入标题,内容等 -->
</bean>
<!-- 申明JavaMailSenderImpl对象 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="defaultEncoding" value="UTF-8" />
<property name="host" value="${mail.host}" />
<property name="username" value="${mail.username}" />
<property name="port" value="${mail.port}"/>
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<!-- 设置认证开关 -->
<prop key="mail.smtp.auth">true</prop>
<!-- 启动调试开关 -->
<prop key="mail.debug">true</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<!-- 设置发送延时 -->
<prop key="mail.smtp.timeout">0</prop>
</props>
</property>
</bean>