本文主要是介绍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格式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!