您的位置:首页 > 编程语言 > Java开发

Spring Java 发送各种邮件

2015-01-04 15:36 302 查看
Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。
包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层邮件系统异常的高级别抽象。 要获得关于邮件异常层次的更丰富的信息,请参考Javadocs。

为了使用JavaMail中的一些特色, 比如MIME类型的信件, Spring提供了MailSender的一个子接口, 即org.springframework.mail.javamail.JavaMailSender。Spring还提供了一个回调接口org.springframework.mail.javamail.MimeMessagePreparator,
用于准备JavaMail的MIME信件。

这里简单的介绍了如何使用spring发送各种形式的邮件以及配置。

1、在src目录下建立mail.properties文件里边包含一下内容

mail.host=smtp.exmail.qq.com(建议使用这个,如果使用smtp.qq.com可能会出现安全认证的问题

mail.username=你的邮箱名

mail.password=你的邮箱密码

mail.from=发送方的邮箱

2、使用spring配置

<a target=_blank target="_blank" id="L1" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank target="_blank" id="L2" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank target="_blank" id="L3" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank target="_blank" id="L4" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank target="_blank" id="L5" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank target="_blank" id="L6" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank target="_blank" id="L7" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank target="_blank" id="L8" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank target="_blank" id="L9" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank target="_blank" id="L10" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank target="_blank" id="L11" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank target="_blank" id="L12" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank target="_blank" id="L13" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank target="_blank" id="L14" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank target="_blank" id="L15" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank target="_blank" id="L16" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank target="_blank" id="L17" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank target="_blank" id="L18" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank target="_blank" id="L19" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank target="_blank" id="L20" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank target="_blank" id="L21" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank target="_blank" id="L22" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank target="_blank" id="L23" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank target="_blank" id="L24" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank target="_blank" id="L25" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank target="_blank" id="L26" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank target="_blank" id="L27" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank target="_blank" id="L28" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank target="_blank" id="L29" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank target="_blank" id="L30" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank target="_blank" id="L31" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank target="_blank" id="L32" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank target="_blank" id="L33" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank target="_blank" id="L34" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank target="_blank" id="L35" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank target="_blank" id="L36" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank target="_blank" id="L37" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank target="_blank" id="L38" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank target="_blank" id="L39" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
<?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"	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 加载Properties文件 -->      <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="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.timeout">0</prop>            </props>          </property>      </bean>  </beans>



来自CODE的代码片
Spring_mail_applicationContext.xml


3、发送简单的邮件

<a target=_blank target="_blank" id="L1" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank target="_blank" id="L2" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank target="_blank" id="L3" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank target="_blank" id="L4" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank target="_blank" id="L5" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank target="_blank" id="L6" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank target="_blank" id="L7" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank target="_blank" id="L8" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank target="_blank" id="L9" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank target="_blank" id="L10" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank target="_blank" id="L11" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank target="_blank" id="L12" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank target="_blank" id="L13" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank target="_blank" id="L14" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank target="_blank" id="L15" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank target="_blank" id="L16" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank target="_blank" id="L17" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank target="_blank" id="L18" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank target="_blank" id="L19" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank target="_blank" id="L20" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank target="_blank" id="L21" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank target="_blank" id="L22" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank target="_blank" id="L23" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank target="_blank" id="L24" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank target="_blank" id="L25" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank target="_blank" id="L26" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank target="_blank" id="L27" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank target="_blank" id="L28" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank target="_blank" id="L29" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
package mail;
import javax.mail.MessagingException;
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.mail.MailSender;import org.springframework.mail.SimpleMailMessage;
/** * 本类测试邮件发送Html形式 *  * @author Eternity_._ *  */public class SingleMailSend {

static ApplicationContext actx = new ClassPathXmlApplicationContext(			"applicationContext.xml");	static MailSender sender = (MailSender) actx.getBean("mailSender");	static SimpleMailMessage mailMessage = (SimpleMailMessage) actx.getBean("mailMessage");	public static void main(String args[]) throws MessagingException {				mailMessage.setSubject("你好");		mailMessage.setText("这个是一个通过Spring框架来发送邮件的小程序");		mailMessage.setTo("9197****1@qq.com");		sender.send(mailMessage);	}}



来自CODE的代码片
Spring_mail_SingleMail.java


4、发送带有图片的邮件,以嵌入HTML的方式

<a target=_blank target="_blank" id="L1" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank target="_blank" id="L2" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank target="_blank" id="L3" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank target="_blank" id="L4" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank target="_blank" id="L5" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank target="_blank" id="L6" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank target="_blank" id="L7" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank target="_blank" id="L8" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank target="_blank" id="L9" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank target="_blank" id="L10" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank target="_blank" id="L11" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank target="_blank" id="L12" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank target="_blank" id="L13" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank target="_blank" id="L14" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank target="_blank" id="L15" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank target="_blank" id="L16" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank target="_blank" id="L17" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank target="_blank" id="L18" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank target="_blank" id="L19" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank target="_blank" id="L20" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank target="_blank" id="L21" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank target="_blank" id="L22" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank target="_blank" id="L23" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank target="_blank" id="L24" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank target="_blank" id="L25" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank target="_blank" id="L26" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank target="_blank" id="L27" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank target="_blank" id="L28" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank target="_blank" id="L29" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank target="_blank" id="L30" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank target="_blank" id="L31" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank target="_blank" id="L32" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank target="_blank" id="L33" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank target="_blank" id="L34" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank target="_blank" id="L35" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank target="_blank" id="L36" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank target="_blank" id="L37" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank target="_blank" id="L38" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank target="_blank" id="L39" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
<a target=_blank target="_blank" id="L40" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;"> 40</a>
<a target=_blank target="_blank" id="L41" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;"> 41</a>
package springmail;
import java.io.File;
import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;
public class SpringAttachedImageMail {
public static void main(String[] args) throws MessagingException {
ApplicationContext ctx = new ClassPathXmlApplicationContext(				"applicationContext.xml");		JavaMailSenderImpl sender = (JavaMailSenderImpl) ctx				.getBean("mailSender");		MimeMessage mailMessage = sender.createMimeMessage();		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true);		messageHelper.setFrom("9197****1@qq.com");		messageHelper.setTo("9197****1@qq.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=cid:image/></body></html>", true);
FileSystemResource img = new FileSystemResource(new File("单.png"));
messageHelper.addInline("image", img);//跟cid一致
sender.send(mailMessage);		System.out.println("邮件发送成功...");
}}



来自CODE的代码片
Spring_mail_AttachedFileMail.java


5、发送带有附件的邮件

<a target=_blank target="_blank" id="L1" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank target="_blank" id="L2" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank target="_blank" id="L3" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank target="_blank" id="L4" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank target="_blank" id="L5" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank target="_blank" id="L6" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank target="_blank" id="L7" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank target="_blank" id="L8" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank target="_blank" id="L9" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank target="_blank" id="L10" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank target="_blank" id="L11" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank target="_blank" id="L12" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank target="_blank" id="L13" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank target="_blank" id="L14" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank target="_blank" id="L15" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank target="_blank" id="L16" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank target="_blank" id="L17" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank target="_blank" id="L18" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank target="_blank" id="L19" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank target="_blank" id="L20" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank target="_blank" id="L21" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank target="_blank" id="L22" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank target="_blank" id="L23" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank target="_blank" id="L24" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank target="_blank" id="L25" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank target="_blank" id="L26" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank target="_blank" id="L27" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank target="_blank" id="L28" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank target="_blank" id="L29" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank target="_blank" id="L30" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank target="_blank" id="L31" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank target="_blank" id="L32" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank target="_blank" id="L33" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank target="_blank" id="L34" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank target="_blank" id="L35" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank target="_blank" id="L36" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank target="_blank" id="L37" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank target="_blank" id="L38" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank target="_blank" id="L39" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
<a target=_blank target="_blank" id="L40" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;"> 40</a>
<a target=_blank target="_blank" id="L41" href="http://blog.csdn.net/eternity0_0/article/details/23475019#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;"> 41</a>
package springmail;
import java.io.File;
import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;
public class SpringAttachedImageMail {
public static void main(String[] args) throws MessagingException {
ApplicationContext ctx = new ClassPathXmlApplicationContext(				"applicationContext.xml");		JavaMailSenderImpl sender = (JavaMailSenderImpl) ctx				.getBean("mailSender");		MimeMessage mailMessage = sender.createMimeMessage();		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true);		messageHelper.setFrom("9197****1@qq.com");		messageHelper.setTo("9197****1@qq.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=cid:image/></body></html>", true);
FileSystemResource img = new FileSystemResource(new File("单.png"));
messageHelper.addAttachment("单.png", file);//添加到附件
sender.send(mailMessage);		System.out.println("邮件发送成功...");
}}



来自CODE的代码片
Spring_mail_AttachedFileMail.java


6、如果使用的是QQ邮箱的话建议把这几项都选上,不然可能调试不能通过。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: