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

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

使用Python的requests库来发送HTTP请求的操作指南

《使用Python的requests库来发送HTTP请求的操作指南》使用Python的requests库发送HTTP请求是非常简单和直观的,requests库提供了丰富的API,可以发送各种类型的HT... 目录前言1. 安装 requests 库2. 发送 GET 请求3. 发送 POST 请求4. 发送