« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9691613 建立时间:2004年12月20日 |

| |
[j2ee]使用spring的mail发送带有图片的html的邮件 原创空间, 软件技术
邢红瑞 发表于 2005/10/1 15:36:20 |
有的朋友问我如何发送带有图片的html的邮件,其实这很简单,只要加入contentID就行下面是实现类package com.educast.mail;
import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.core.io.FileSystemResource;
import javax.mail.internet.MimeMessage;import javax.mail.MessagingException;import java.io.File;
public class ImageMailSender extends BaseMailSender { public void sendMessage() throws MessagingException { MimeMessage msg = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true, "GB2312");
helper.setTo(to); helper.setFrom(from); helper.setSubject(subject);
helper.setText("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='#'>郁闷!" + "<img src=\"cid:img\"></body></html>", true);
// add the image FileSystemResource image = new FileSystemResource(new File( "c:\\0.gif")); helper.addInline("img", image);
sender.send(msg); }
public static void main(String[] args) throws Exception { ApplicationContext ctx = new FileSystemXmlApplicationContext( new String[] { "D:\\WORK\\JDBC\\mail\\src\\javaMailSender.xml" }); ImageMailSender sender = (ImageMailSender) ctx.getBean("messageSender"); sender.sendMessage(); }}这里是配置文件<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host"> <value>localhost</value> </property> <property name="username"> <value>webmaster</value> </property> <property name="password"> <value>password</value> </property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property>
</bean>
<bean id="messageSender" class="com.educast.mail.ImageMailSender"> <property name="javaMailSender"> <ref bean="sender"/> </property> <property name="to"> <value>mfc42d@163.com</value> </property> <property name="from"> <value>webmaster@mymail.cn</value> </property> <property name="subject"> <value>HTML Mail</value> </property> </bean></beans>
|
|
|