mail.jar spring-core.jar
application-mail.xml文件
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.exmail.sina.com"></property> <property name="port" value="25"></property> <property name="username" value="*******@ssreader.cn"></property> <property name="password" value="******"></property> <property name="defaultEncoding" value="UTF-8"></property> <property name="javaMailProperties"> <props> <prop key="mail.debug">false</prop> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> </beans>
SendMailBySpringMail.java
package test.email; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.UrlResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.activation.URLDataSource; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; public class SendMailBySpringMail { public static JavaMailSender javaMailSender; static { javaMailSender = (JavaMailSender) new ClassPathXmlApplicationContext("application-mail.xml").getBean("javaMailSender"); } /** * 发送简单文本邮件 * SimpleMailMessage */ public static void sendSimpleMailMessage() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setFrom("******@ssreader.cn");//可选的 mailMessage.setTo("lamamo@163.com"); mailMessage.setSubject("你好啊 ******"); mailMessage.setText("那就随便说说吧" + new Date()); javaMailSender.send(mailMessage); } /** * 发送html类型邮件 * MimeMessage */ public static void sendMimeMessage() throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8"); // 可选的,可以用来修改显示给接收者的名字 helper.setFrom("******@ssreader.cn"); helper.setTo("lamamo@163.com"); helper.setSubject("这就是个实验"); helper.setText("<h1>你好</h1>", true); javaMailSender.send(mimeMessage); } /** * 发送含附件的邮件 */ public static void sendMimeMessageAttachment() throws MessagingException, UnsupportedEncodingException, MalformedURLException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); //第二个参数设置为true,表示允许添加附件 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8"); helper.setFrom("******@ssreader.cn"); helper.setTo("lamamo@163.com"); helper.setSubject("发送含图片附件的邮件"); //第二个参数为true表示需要内容为HTML格式 helper.setText("<h1>你好</h1>", true); //需要对文件名进行转码 helper.addAttachment(MimeUtility.encodeText("王.png"), new File("D:\\我的文档\\My Pictures\\logo.png")); helper.addAttachment(MimeUtility.encodeText("Apache网站图标.gif"), new URLDataSource(new URL("http://www.apache.org/images/asf_logo_wide.gif"))); javaMailSender.send(mimeMessage); } /** * 发送HTML格式含图片的邮件 */ public static void sendMimeMessageInline() throws MessagingException, MalformedURLException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); //第二个参数设置为true,表示允许添加附件 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8"); helper.setFrom("******@ssreader.cn"); helper.setTo("lamamo@163.com"); helper.setSubject("发送HTML格式含图片的邮件"); //第二个参数为true表示需要内容为HTML格式 helper.setText("<h1>你好</h1><br><img src='cid:fileId'><img src='cid:abc'>", true); helper.addInline("fileId", new File("D:\\我的文档\\My Pictures\\logo.png")); helper.addInline("abc", new UrlResource("http://www.apache.org/images/asf_logo_wide.gif")); javaMailSender.send(mimeMessage); } public static void main(String[] args) throws UnsupportedEncodingException, MessagingException, MalformedURLException { // sendSimpleMailMessage(); sendMimeMessage(); // sendMimeMessageAttachment(); // sendMimeMessageInline(); } }
相关推荐
3、Spring Boot 发送邮件(文本格式的邮件、发送HTML格式的邮件、发送带附件 的邮件、发送带静态资源的邮件) 个人觉得Springboot的开发简单的归纳为三步jar包引入,配置,应用。 (一)简单使用 1)JSP的使用...
Spring实现各种邮件的发送 简单配置就可发送邮件
java 发送邮件 spring发送邮件Mail
基于spring mvc 框架,用freemarker创建邮件模板,包括详细邮件发送功能的spring配置和实现代码
Spring mail发送邮件实例 Spring mail发送邮件实例
Spring整合Quartz定时发送邮件
利用spring下的一个类进行邮件发送,内附具体用法
Spring 封装的JavaMail,使邮件发送变的相当简单,我通过Spring的邮件发送实现了发纯文本形的内容,HTMl,带附件,带图片等示例代码,并自行测试过.希望对初学者有帮助
使用Spring mail发送邮件的完整实例,包含代码与Spring mail的jar,可直接运行与使用。相关学习文档参考博客《Spring Mail邮件发送 http://blog.csdn.net/chenxiang0207/article/details/8256528》。
用spring实现了定时发送邮件,无需复杂的代码
通过spring类,发送邮件信息!包含相应的jar包,有实例
spring+velocity发送邮件,spring+velocity发送邮件
Spring编写发送邮件程序,通过spring框架编写的邮件发送,代码简单实用。
SpringMail.rar SpringMail发邮件
它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。 包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层...
Spring2.5发送邮件程序Demo 希望大家喜欢
Spring邮件发送服务(java邮件发送),支持多附件、多人发送/抄送/暗送
本资源提供了一套基于Spring Boot的邮件发送服务的设计源码,包含1629个文件,其中包括760个SVG矢量文件,642个PNG图片文件,以及56个CSS样式文件。此外,还包括25个JavaScript脚本文件,23个Java源代码文件,以及18...
用spring发送邮件,该功能在一般系统监控中经常用到。
pdf介绍spring-boot-邮件发送。 Spring Boot通过集成spring-boot-starter-mail依赖和相关配置,可以方便地实现邮件发送功能。具体的步骤: 添加依赖:在Spring Boot项目的pom.xml文件中加入spring-boot-starter-mail...