Spring2.0邮件的发送 附件 图片 HTML格式

2024-03-31 14:58

本文主要是介绍Spring2.0邮件的发送 附件 图片 HTML格式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring2.0邮件的发送,支持多附件  图片    HTML格式   小于10M

 

package com.tht.common.mail.spring;import org.apache.log4j.Logger;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;import java.util.Properties;/*** 简单的文体邮件发送* Created by IntelliJ IDEA.* User: liuwen* Date: 2010-11-6* Time: 10:01:58* To change this template use File | Settings | File Templates.*/
public class SimpleMailDemoAuth {static Logger log=Logger.getLogger(SimpleMailDemoAuth.class);public static void main(String[] args){JavaMailSenderImpl senderImple=new JavaMailSenderImpl();//设置Mail ServersenderImple.setHost("smtp.126.com");//设置连接端口senderImple.setPort(25);senderImple.setDefaultEncoding("UTF-8");senderImple.setUsername("thinktothings@126.com");senderImple.setPassword("Tht12345");Properties prop=new Properties();prop.put( "mail.smtp.auth" ,  "true" ) ;  //  将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确prop.put( "mail.smtp.timeout" ,  "25000" ) ;senderImple.setJavaMailProperties(prop);//建立邮件消息SimpleMailMessage mailMessage=new SimpleMailMessage();mailMessage.setTo("thinktothings@sina.cn");mailMessage.setFrom("thinktothings@126.com");//设置  主题与正文mailMessage.setSubject("Spring simple mail test");mailMessage.setText("测试邮件的文本");senderImple.send(mailMessage);log.info("邮件发送成功了。。。。。。");}
}

 

 

package com.tht.common.mail.spring;import com.tht.common.log.log4j.base.Log4jBase;
import com.tht.common.log.log4j.base.Log4jStaticBase;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;/*** Created by IntelliJ IDEA.* User: liuwen* Date: 2010-11-6* Time: 10:48:00* To change this template use File | Settings | File Templates.*/
public class HtmlMailDemo extends Log4jBase{public static void main(String[] args){JavaMailSenderImpl senderImpl=new JavaMailSenderImpl();//设置Mail ServersenderImpl.setHost("203.170.49.14");Properties prop=new Properties();prop.put( "mail.smtp.auth" ,  "true" ) ;  //  将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确prop.put( "mail.smtp.timeout" ,  "25000" ) ;senderImple.setJavaMailProperties(prop);       //建立邮件消息MimeMessage mailMessage=senderImpl.createMimeMessage();MimeMessageHelper messageHelper=new MimeMessageHelper(mailMessage);//设置收件人、寄件人、主题与正文try {String[] tos={"test@mail_server.com","test2@mail_server.com"};messageHelper.setTo(tos);messageHelper.setFrom("liuwen");messageHelper.setSubject("Html mail test");messageHelper.setText("<html><head><title>Title test</title></head><body><h1>Hello html test</h1></body></html>",true);//传送邮件senderImpl.send(mailMessage);Log4jStaticBase.log.info("Html send success");} catch (MessagingException e) {Log4jStaticBase.log.error(e.getMessage(), e.fillInStackTrace());}}}

 

 

package com.tht.common.mail.spring;import com.tht.common.log.log4j.base.Log4jBase;
import com.tht.common.log.log4j.base.Log4jStaticBase;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;/*** Created by IntelliJ IDEA.* User: liuwen* Date: 2010-11-6* Time: 10:48:00* To change this template use File | Settings | File Templates.* 邮件支持HTML格式,并且可以将图片直接在邮件正文中显示*/
public class AttachedImageDemo extends Log4jBase{public static void main(String[] args){JavaMailSenderImpl senderImpl=new JavaMailSenderImpl();//设置Mail ServersenderImpl.setHost("203.170.49.14");Properties prop=new Properties();prop.put( "mail.smtp.auth" ,  "true" ) ;  //  将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确prop.put( "mail.smtp.timeout" ,  "25000" ) ;senderImple.setJavaMailProperties(prop);//设置收件人、寄件人、主题与正文try {//建立邮件消息MimeMessage mailMessage=senderImpl.createMimeMessage();MimeMessageHelper messageHelper=new MimeMessageHelper(mailMessage,true);String[] tos={"test@mail_server.com","test@mail_server.com"};messageHelper.setTo(tos);messageHelper.setFrom("liuwen");messageHelper.setSubject("Html mail test");messageHelper.setText("<html><head><title>Title test</title></head><body><h1>Hello image html test</h1><img src=\"cid:testImageId\" /></body></html>",true);//ClassPathResource img=new ClassPathResource("testImageId.jpg");File img=new File("files/images/testImageId.jpg");messageHelper.addInline("testImageId",img);//传送邮件senderImpl.send(mailMessage);Log4jStaticBase.log.info("Html image send success");} catch (MessagingException e) {Log4jStaticBase.log.error(e.getMessage(), e.fillInStackTrace());}}}

 

package com.tht.common.mail.spring;import com.tht.common.log.log4j.base.Log4jStaticBase;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;/*** Created by IntelliJ IDEA.* User: liuwen* Date: 2010-11-6* Time: 15:28:03* To change this template use File | Settings | File Templates.* 邮件附件发送* 测试数据:  附件大小9.88M   文件类型  ZIP  ;  xls   1M*/
public class AttachedFileDemo {public static void main(String[] args){JavaMailSenderImpl senderImpl=new JavaMailSenderImpl();//设置Mail ServersenderImpl.setHost("203.170.49.14");Properties prop=new Properties();prop.put( "mail.smtp.auth" ,  "true" ) ;  //  将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确prop.put( "mail.smtp.timeout" ,  "25000" ) ;senderImple.setJavaMailProperties(prop);//设置收件人、寄件人、主题与正文try {//建立邮件消息MimeMessage mailMessage=senderImpl.createMimeMessage();MimeMessageHelper messageHelper=new MimeMessageHelper(mailMessage,true);String[] tos={"test@mail_server.com","test@mail_server.com"};messageHelper.setTo(tos);messageHelper.setFrom("liuwen");messageHelper.setSubject("Html mail test");messageHelper.setText("<html><head><title>Title test</title></head><body><h1>Hello attach html test</h1></body></html>",true);//ClassPathResource img=new ClassPathResource("testImageId.jpg");File img=new File("files/data/xwork-2.1.5-all.zip");messageHelper.addAttachment("xwork-2.1.5-all.zip",img);//传送邮件senderImpl.send(mailMessage);Log4jStaticBase.log.info("Html attach send success");} catch (MessagingException e) {Log4jStaticBase.log.error(e.getMessage(), e.fillInStackTrace());}}
}

 

package com.tht.common.mail.spring;import com.tht.common.log.log4j.base.Log4jStaticBase;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;/*** Created by IntelliJ IDEA.* User: liuwen* Date: 2010-11-6* Time: 15:28:03* To change this template use File | Settings | File Templates.* 邮件附件发送* 测试数据:  附件大小9.88M   文件类型  ZIP  ;  xls   1M*/
public class AttachedFileMultiDemo {public static void main(String[] args){JavaMailSenderImpl senderImpl=new JavaMailSenderImpl();//设置Mail ServersenderImpl.setHost("203.170.49.14");Properties prop=new Properties();prop.put( "mail.smtp.auth" ,  "true" ) ;  //  将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确prop.put( "mail.smtp.timeout" ,  "25000" ) ;senderImple.setJavaMailProperties(prop);//设置收件人、寄件人、主题与正文try {//建立邮件消息MimeMessage mailMessage=senderImpl.createMimeMessage();MimeMessageHelper messageHelper=new MimeMessageHelper(mailMessage,true);String[] tos={"test@mail_server.com","test@mail_server.com"};messageHelper.setTo(tos);messageHelper.setFrom("liuwen");messageHelper.setSubject("Html mail test");messageHelper.setText("<html><head><title>Title test</title></head><body><h1>Hello attach html test</h1></body></html>",true);//ClassPathResource img=new ClassPathResource("testImageId.jpg");//第一个附件File img=new File("files/data/M800_IN_VOIP_Daily_2010-10-19-18-29-23.XLS");if(img.exists()){messageHelper.addAttachment("M800_IN_VOIP_Daily_2010-10-19-18-29-23.XLS",img);}//第二个附件img=new File("files/images/testImageId.jpg");if(img.exists()){messageHelper.addAttachment("testImageId.jpg",img);}//第三个附件img=new File("files/data/ext-3.0.0.zip");if(img.exists()){messageHelper.addAttachment("ext-3.0.0.zip",img);}//传送邮件senderImpl.send(mailMessage);Log4jStaticBase.log.info("Html attach send success");} catch (MessagingException e) {Log4jStaticBase.log.error(e.getMessage(), e.fillInStackTrace());}}
}

这篇关于Spring2.0邮件的发送 附件 图片 HTML格式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/864603

相关文章

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

Vuex Actions多参数传递的解决方案

《VuexActions多参数传递的解决方案》在Vuex中,actions的设计默认只支持单个参数传递,这有时会限制我们的使用场景,下面我将详细介绍几种处理多参数传递的解决方案,从基础到高级,... 目录一、对象封装法(推荐)二、参数解构法三、柯里化函数法四、Payload 工厂函数五、TypeScript

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo