spring3框架提供了非常有用的邮件功能,org.springframework.mail 提供了对邮件的发送的jar包,最好还是自己去看一下里面的函数。spring framework提供了简单和带附件的邮件发送功能。

假设这是个业务逻辑层代码,假设我们需要给客户发送关于订单信息的邮件

publicinterface OrderManager {void placeOrder(Order order);
}

简单邮件发送功能

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;publicclass SimpleOrderManager implements OrderManager {

private MailSender mailSender;//spring 提供的一个已经封装好的库,具体的实现可以参考API

private SimpleMailMessage templateMessage; publicvoid setMailSender(MailSender mailSender) { this.mailSender = mailSender; } publicvoid setTemplateMessage(SimpleMailMessage templateMessage) { this.templateMessage = templateMessage; } public void placeOrder(Order order) { // Do the business calculations...// Call the collaborators to persist the order...// Create a thread safe "copy" of the template message and customize it SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage); msg.setTo(order.getCustomer().getEmailAddress()); msg.setText( "Dear " + order.getCustomer().getFirstName() + order.getCustomer().getLastName() + ", thank you for placing order. Your order number is " + order.getOrderNumber()); try{ this.mailSender.send(msg); } catch(MailException ex) { // simply log it and go on... System.err.println(ex.getMessage()); } }

}

xml的配置文件,当然你也可以使用注解

<beanid="mailSender"class="org.springframework.mail.javamail.JavaMailSenderImpl">

<propertyname="host"value="mail.mycompany.com"/>//邮箱的服务主机

<property name="port" value="25"/>

<property name="port" value="25" />

<property name="javaMailProperties">

<props>

<prop key="mail.smtp.auth">true</prop>

<prop key="mail.smtp.timeout">25000</prop>

</props>

</property>

<property name="username" value="**********" />//用户名

<property name="password" value="*******"></property>//密码

</bean> <!-- this is a template message that we can pre-load with default state --> <beanid="templateMessage"class="org.springframework.mail.SimpleMailMessage"> <property name="from"v alue="customerservice@mycompany.com"/>//发送者的邮箱地址 <property name="subject"value="Your order"/> </bean> <beanid="orderManager"class="com.mycompany.businessapp.support.SimpleOrderManager"> <propertyname="mailSender"ref="mailSender"/> <propertyname="templateMessage"ref="templateMessage"/> </bean>

发送附件和内嵌的htm啊

JavaMail and MimeMessageHelper

1 附件的发送

JavaMailSenderImpl sender = new JavaMailSenderImpl();MimeMessage message = sender.createMimeMessage();// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);//true 表示你发送的是带有附件的邮件
helper.setTo("test@host.com");helper.setText("Check out this p_w_picpath!");// let's attach the infamous windows Sample file (this time copied to c:/)

FileSystemResource file = new FileSystemResource(new File("c:/Sample.jpg"));

//spring 2.5的版本需要

//FileSystemResource file = new FileSystemResource(new File("file:c:/Sample.jpg"));

helper.addAttachment("CoolImage.jpg", file);

// helper.addAttachment(MimeUtility.encodeWord("个人申请表.pdf"),new FileSystemResource(new File(pdfP//ath)));

sender.send(message);

2 发送带有html的邮件

JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");MimeMessage message = sender.createMimeMessage();// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo("test@host.com");// use the true flag to indicate the text included is HTML
helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true);// let's include the infamous windows Sample file (this time copied to c:/)
FileSystemResource res = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addInline("identifier1234", res);sender.send(message);

转载于:https://blog.51cto.com/4747857/1372261

spring3 发送邮件和附件相关推荐

  1. python发送邮件和附件

    发送邮件的时候,需要发送人,收件人,和一台邮件服务器,这里使用python发送一个邮件,主要需要引入smtplib和email库. 下面是源码,粘贴即可用: #!/usr/bin/env python ...

  2. (转载)SpringBoot 发送邮件和附件

    什么是SMTP? SMTP全称为Simple Mail Transfer Protocol(简单邮件传输协议),它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式.SMTP认证要 ...

  3. Python 使用 smtp ssl 模式 发送邮件与附件

    参考 :         发送邮件简单入门 ( 以qq邮箱,163邮箱为例 ) :https://blog.csdn.net/qq_38661599/article/details/81013834 ...

  4. python3发送qq邮件_python3通过qq邮箱发送邮件以及附件

    本文实例为大家分享了python3通过qq邮箱发送邮件以及附件的具体代码,供大家参考,具体内容如下ZLP免费资源网 开启qq邮箱的smtp服务ZLP免费资源网 ZLP免费资源网 代码:ZLP免费资源网 ...

  5. python3通过qq邮箱发送邮件以及附件

    本文实例为大家分享了python3通过qq邮箱发送邮件以及附件的具体代码,供大家参考,具体内容如下 开启qq邮箱的smtp服务 代码: 在学习过程中有什么不懂得可以加我的 python学习qun,85 ...

  6. python发送邮件带附件_Python发送邮件(带附件)

    import smtplib                           #发送邮件模块 from email.mime.text import MIMEText    #定义邮件内容 fro ...

  7. Spring 4 使用Freemarker模板发送邮件添加附件

    前言 Spring对Java的邮件发送提供了很好的支持,提供了超级简单的API,大大简化了Java邮件发送功能的开发. Spring对Email的支持是基于JavaMail API开发的,所以,我们在 ...

  8. python发送邮件及附件

    今天给大伙说说python发送邮件,官方的多余的话自己去百度好了,还有一大堆文档说实话不到万不得已的时候一般人都不会去看,回归主题: 本人是mac如果没有按照依赖模块的请按照下面的截图安装 导入模块如 ...

  9. sql 发送邮件网络附件_利用VBA发送附件电子邮件

    大家好,我们今日讲解"利用VBA发送附件电子邮件",这节内容是"VBA信息获取与处理"教程中第五个专题"利用VBA发送电子邮件"的第一节. ...

最新文章

  1. Android实用应用程序源码
  2. 任意用户密码重置(四):重置凭证未校验
  3. ICLR 2020将采用远程会议,首次在非洲办会可能就这样泡汤了
  4. oracle数据库集群日志,Oracle集群数据库中恢复归档日志
  5. java 根据类名示例化类_Java LocalDateTime类| ofInstant()方法与示例
  6. NYOJ47过河问题
  7. php+loaction+框架,【集锦】nginx【php,location,alias,504】
  8. OneNote中到底能放多少种东西?
  9. 【项目篇】Android团队项目开发之统一代码规范
  10. springboot集成fastDFS文件上传下载
  11. 字符串算法 金策_OI-Public-Library/国家集训队论文1999-2017 at master · BlackWaters/OI-Public-Library · GitHub...
  12. 尔雅 科学通史(吴国盛) 个人笔记及课后习题 2018 第五章 欧洲科技文明的起源
  13. matlab cholesky分解函数,matlab中矩阵LDLT分解与Cholesky分解
  14. 阿肯色大学计算机,阿肯色大学怎么样?
  15. Shave Beaver! CodeForces - 331B2 (线段树)
  16. 若依专题 线程池配置
  17. RGB转换成CMYK
  18. python函数内嵌,嵌套函数
  19. ABB系统备份与恢复(重做系统)S4C系统
  20. flash builder 编译配置

热门文章

  1. java和python哪个好就业2020-JAVA和Python哪个好就业?
  2. 在windows上的git bash中安装tree 和 linux tree命令使用
  3. java set集合转数组_Java数组【array】与集合【list】的相互转换
  4. LeetCode Decode String(栈和递归)
  5. 前端技术学习路线及技术汇总
  6. msql查询指定日期
  7. Servlet学习笔记(三)之HttpServletRequest
  8. JavaScript对象this指向(普通键this指向 非指向函数的键)
  9. 获取当前应用程序的文件名
  10. javaMail发邮件