下面是一个例子使用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. 链接 DB App.config 解析

    <?xml version="1.0" encoding="utf-8"?><configuration> <startup> ...

  2. 003iptables 命令介绍

    http://www.cnblogs.com/wangkangluo1/archive/2012/04/19/2457072.html iptables 防火墙可以用于创建过滤(filter)与NAT ...

  3. LSTM及其变种及其克服梯度消失

    本宝宝又转了一篇博文,但是真的很好懂啊: 写在前面:知乎上关于lstm能够解决梯度消失的问题的原因: 上面说到,LSTM 是为了解决 RNN 的 Gradient Vanish 的问题所提出的.关于 ...

  4. initialization 与 finalization 执行顺序 研究

    看GIF: 第二个GIF: DEMO下载:http://files.cnblogs.com/files/del88/InitOrderDemo.zip

  5. hdu 5912(迭代+gcd)

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  6. Mybatis的核心配置

    之前了解了Mybatis的基本用法,现在学习一下Mybatis框架中的核心对象以及映射文件和配置文件,来深入的了解这个框架. 1.Mybatis的核心对象 使用MyBatis框架时,主要涉及两个核心对 ...

  7. jquery datatable客户端分页保持

    请加入下面注释的参数,并强制刷新浏览器,即可解决,关键配置: "bStateSave":true, $("#tableID").DataTable({ &quo ...

  8. 谷歌翻译python接口

    项目地址:  https://github.com/ssut/py-googletrans 安装: sudo pip install googletrans 使用: #!/usr/bin/python ...

  9. MFC+WinPcap编写一个嗅探器之零(目录)

    零零散散写了三天,完成了编写嗅探器的文章,旨在让自己加深印象,是初学者少走一些弯路.因为先前未接触MFC,之后也不打算精通,完全是0基础,所以文章技术含量不高,但难点基本上都都包括了,凑合这看吧,接下 ...

  10. C# 找出实现某个接口的所有类

    该方法只能找实现某个接口的类,不能找继承某个抽象类的子类 var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => ...