利用Tomcat的JNDI容器管理JavaMail中的Session

本文详细介绍了使用JavaMail API发送包含文本、图片和附件的邮件过程,包括配置邮件服务器、创建邮件内容、发送邮件等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:把javamail的jar包(mail.jar/jaf.jar(JDK5-):Java Activation Framework JavaMail中用它读取磁盘文件)拷贝到Tomcat\lib目录下...


第二步:在引用的META-INF目录下,建立一个context.xml的配置文件,内容如下:


<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resource name="mail/Session" auth="Container"
            type="javax.mail.Session"
            mail.smtp.host="smtp.163.com"/>
</Context>

  第三步:在web环境下通过JNDI获取JavaMail的Session


--------------------------------------------------------------------------------------------------------

案例一:发送文本和图片的邮件:

package com.leaf.eg04.mail;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailDemo2 {

	public static void main(String[] args) throws Exception{
		Properties props = new Properties();//不设置任何配置,发送时需要
		props.setProperty("mail.host","smtp.163.com");//设置发送主机所使用的发送协议
		props.setProperty("mail.transport.protocol","smtp");//设置传输协议
		Session session = Session.getInstance(props);//通过属性获取到Session的实例
		session.setDebug(true);//Set the debug setting for this Session.
		MimeMessage msg = new MimeMessage(session);//将session加入到模拟信息MimeMessage中,因为最终发送信息Trasport需要它
		msg.setFrom(new InternetAddress("itheimacloud@163.com"));//AddressException,设置发件人邮箱
		msg.setRecipients(Message.RecipientType.TO, "itheima14@163.com");//设置收件类型,收件地址
		msg.setSubject("JavaMail发送的邮件");//设置收件的主题
		//邮件内容的组装
			//文本部分
			MimeBodyPart textPart = new MimeBodyPart();
			textPart.setContent("abc<img src='cid:abc'>abc","text/html");//设置文本内容和文本类型
			//图片部分
			MimeBodyPart imagePart = new MimeBodyPart();
			//需要用到jaf的API
			DataHandler dh = new DataHandler(new FileDataSource("src/1.jpg"));//将本地的图片内容加载到DataHandler中
			imagePart.setDataHandler(dh);//将图片数据处理加载到MineBodyPart中
			imagePart.setContentID("abc");//设置文本内容的id
			
			//描述关系
			MimeMultipart mm = new MimeMultipart();
			mm.addBodyPart(textPart);//将文本内容添加到MimeMultipart中
			mm.addBodyPart(imagePart);//将图片内容添加到MimeMultipart中
			mm.setSubType("related");//默认子类型为mixed
		
		msg.setContent(mm);//将组装好的邮件内容放到模拟信息中
		msg.saveChanges();//保存信息
		//msg.writeTo(new FileOutputStream("d:/"));//可以将组装好的邮件写到本地磁盘
		//发送邮件
		Transport ts = session.getTransport();
		ts.connect("itheimacloud","iamsorry");//登录本地发送邮件客户端
		ts.sendMessage(msg, msg.getAllRecipients());//向所有收件人发送信息(可以群发)
	}
}
案例二:带有文本、图片和附件的邮件发送:

package com.leaf.eg04.mail;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class MailDemo3 {

	public static void main(String[] args) throws Exception{
		Properties props = new Properties();
		props.setProperty("mail.host","smtp.163.com");
		props.setProperty("mail.transport.protocol","smtp");
		props.setProperty("mail.smtp.auth", "true");//请求认证与java的实现有关
		Session session = Session.getInstance(props);
		session.setDebug(true);
		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("itheimacloud@163.com"));
		msg.setRecipients(Message.RecipientType.TO, "itheima14@163.com");
		//msg.setContent("<img src='src/1.jpg' id='abc'/>","text/html");
		msg.setSubject("您好呀");
		//msg.saveChanges();
			//文本部分
			MimeBodyPart textPart = new MimeBodyPart();
			textPart.setContent("abc<img src='cid:abc'/>abc","text/html;charset=utf-8");
			//图片部分
			MimeBodyPart imagePart = new MimeBodyPart();
			DataHandler dh = new DataHandler(new FileDataSource("src/1.jpg"));
			imagePart.setDataHandler(dh);
			imagePart.setContentID("abc");
			//附件部分
			MimeBodyPart attachmentPart = new MimeBodyPart();
			//attachmentPart.attachFile("src/动态代理.txt");
			DataHandler dh1 = new DataHandler(new FileDataSource("src/动态代理.txt"));
			String name = dh1.getName();
			System.out.println(name);//加上charset就不会有乱码
			attachmentPart.setDataHandler(dh1);
			attachmentPart.setFileName(MimeUtility.encodeText(name));//手动设置附件的名称
			
			//描述关系
			MimeMultipart mmPart = new MimeMultipart();
			mmPart.addBodyPart(textPart);
			mmPart.addBodyPart(imagePart);
			mmPart.setSubType("related");
			
			//文本和图片的组合再和附件进行组合
			MimeBodyPart textImagePart = new MimeBodyPart();
			textImagePart.setContent(mmPart);
			
			MimeMultipart multipart = new MimeMultipart();//用户组合关系
			multipart.addBodyPart(textImagePart);
			multipart.addBodyPart(attachmentPart);
			multipart.setSubType("mixed");//u复杂关系
			
		msg.setContent(mmPart);
		msg.saveChanges();
		//Transport ts = new Transport(new FileDataSource("src/1.jpg"));
		Transport ts = session.getTransport();
		ts.connect("itheima@163.com","iamsorry");
		ts.sendMessage(msg,msg.getAllRecipients());
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值