下面是一个例子使用Spring通过Gmail SMTP服务器来发送电子邮件附件。为了包含附件的电子邮件,你必须使用 Spring的JavaMailSender及MimeMessage 来代替 MailSender&SimpleMailMessage。
2.Spring的邮件发件人

必须使用 JavaMailSender 代替 MailSender 发送附件,并用 MimeMessageHelper 附加的资源。在这个例子中,它会得到 “c:\\log.txt” 从文件系统(FileSystemResource)作为电子邮件附件的文本文件。

除了文件系统,您还可以从URL路径(UrlResource对象),类路径(使用ClassPathResource),InputStream(InputStreamResource)的任何资源......请参考 Spring 的 AbstractResource 类的实现。

File : MailMail.java

package com.yiibai.common;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; public class MailMail
{
private JavaMailSender mailSender;
private SimpleMailMessage simpleMailMessage; public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
} public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
} public void sendMail(String dear, String content) { MimeMessage message = mailSender.createMimeMessage(); try{
MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(simpleMailMessage.getFrom());
helper.setTo(simpleMailMessage.getTo());
helper.setSubject(simpleMailMessage.getSubject());
helper.setText(String.format(
simpleMailMessage.getText(), dear, content)); FileSystemResource file = new FileSystemResource("C:\\log.txt");
helper.addAttachment(file.getFilename(), file); }catch (MessagingException e) {
throw new MailParseException(e);
}
mailSender.send(message);
}
}

3. Bean配置文件

配置 mailSender bean,电子邮件模板,并指定Gmail的SMTP服务器电子邮件的详细信息。

File : Spring-Mail.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="yiibai.com@gmail.com" />
<property name="password" value="password" /> <property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean> <bean id="mailMail" class="com.yiibai.common.MailMail">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean> <bean id="customeMailMessage"
class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="from@no-spam.com" />
<property name="to" value="to@no-spam.com" />
<property name="subject" value="Testing Subject" />
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean> </beans>

4. 运行它

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml"); MailMail mm = (MailMail) context.getBean("mailMail");
mm.sendMail("Yiibai", "This is text content"); }
}

输出结果

Dear Yiibai,
Mail Content : This is text content
Attachment : log.txt
 

Spring发送带附件邮件的更多相关文章

  1. ORACLE发送带附件邮件的二三事之一

    在oracle使用过程中,我们可以通过pl/sql生成数据文件,也可以通过spool on spool off生成,但某些环境下,我们需要通过存储过程处理数据,数据处理完,需要自动生成数据文件,手工导 ...

  2. 使用Spring发送带附件的电子邮件(站内和站外传送)

    JavaMail的介绍 JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口.它是Sun发布的用来处理email的API.它可以方便地执行一些常用的邮件传输.   虽然JavaMail是 ...

  3. 利用spring-mail模块发送带附件邮件dome

    本例为maven项目,直接撸代码吧. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  4. [SpringBoot] - 发送带附件的邮件

    <!--发送email依赖--> <dependency> <groupId>org.springframework.boot</groupId> &l ...

  5. java发送带附件的邮件

    /** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...

  6. C#发送带附件的邮件的代码

    如下的代码是关于C#发送带附件的邮件的代码. MailMessage m = new MailMessage();m.Subject = "File attachment!";m. ...

  7. 利用Python+163邮箱授权码发送带附件的邮件

    背景 前段时间写了个自动爬虫的脚本,定时在阿里云服务器上执行,会从某个网站上爬取链接保存到txt文本中,但是脚本不够完善,我需要爬虫完毕之后通过邮件把附件给我发送过来,之前写过一个<利用Pyth ...

  8. python 发送带附件的邮件

    特别注意的地方:filespart.add_header("Content-Disposition","attachment",filename=file_na ...

  9. 接口测试基础——第2篇smtplib发送带附件的邮件

    我先给大家补充一个用QQ发送纯文本电子邮件的代码,用QQ的朋友可以参考一下: # coding=utf-8 import smtplib from email.mime.text import MIM ...

随机推荐

  1. WPF之DataGrid--列的前台及后台实现

    一.前台实现 在xaml里可以很轻松地实现一个如下图所示的DataGrid <StackPanel> <ComboBox Width="50" Horizonta ...

  2. 进度条算法 progressBar

    ; ;var maxNum=int.MaxValue; progressBar.Maximum =maxNum; progressBar.Minimum = ; progressBar.Value = ...

  3. SP 页面缓存以及清除缓存

    JSP 页面缓存以及清除缓存 一.概述 缓存的思想可以应用在软件分层的各个层面.它是一种内部机制,对外界而言,是不可感知的. 数据库本身有缓存,持久层也可以缓存.(比如:hibernate,还分1级和 ...

  4. css如何将div画成三角形

    首先了解一下盒模型: 盒模型 先看一段代码: #div1{ height: 100px; border-style: solid; border-width: 100px 100px 100px 10 ...

  5. intellij idea 远程tomcat 调试

    由于在服务器上远程调试风险较高,所以万不得已的情况下 不要这样做.可以本地调试好  再上传到服务器上. 1.关闭防火墙 启动Firewalld,及开机自启. # systemctl start fir ...

  6. C++大数据处理

    转:http://blog.csdn.net/v_july_v/article/details/7382693 作者:July出处:结构之法算法之道blog 前言 一般而言,标题含有“秒杀”,“99% ...

  7. Eolinker——前置用例返回的reponse值进行传递

    如下补充均是Eolinker的文档中未说明的部分 示例:将login接口reponse中的mobile的值作为参数,传递给”重置密码”的请求体“code" 1.打开”前置用例“,先点击左上角 ...

  8. Oracle 入门学习笔记

    linux命令 查看linux系统版本号 uname -r 或 uname -a 查看linux发行版本号 cat /etc/redhat-release 查看linux具体版本号 cat /proc ...

  9. winEDT 9.0 完美破解

    options-->options interface... --> advance configuration... -->event handlers --> Exit,并 ...

  10. day1作业一:编写登陆接口

    作业一:编写登陆接口 1.输入用户名和密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 Readme: (1)提示用户输入用户名: (2)用户名验证,验证是否已经锁定: (3)是否锁定:已锁定告诉 ...