JavaMail















import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

//基于JavaMail发送[简单]邮件
public class Demo1 {
	public static void main(String[] args) throws Exception {
		//设置发送邮件的环境信息,例如协议和主机
		Properties props = new Properties();
		props.put("mail.transport.protocol","smtp");
		props.put("mail.host","127.0.0.1");
		Session session = Session.getDefaultInstance(props);
		//取得传输对象
		Transport transport = session.getTransport();
		//创建邮件
		Message message = createMessage(session);
		//连接邮件服务器
		transport.connect();
		//transport.connect("aaa@zhaojun.com","123456");
		//发送邮件
		transport.send(message);
		//关闭传输对象
		transport.close();
	}
	//创建简单邮件
	public static Message createMessage(Session session) throws Exception{
		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress("aaa@zhaojun.com"));
		message.setRecipient(RecipientType.TO,new InternetAddress("bbb@qq.com"));
		message.setSubject("BBS论坛");
		message.setContent("<font color='red'>恭喜你,注册成功</font>","text/html;charset=UTF-8");
		return message;
	} 
}

import java.io.FileOutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
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 Demo5 {
	
	public static void main(String[] args) throws Exception {
		
		//创建一封邮件
		MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
		
		//设置From属性
		message.setFrom(new InternetAddress("aaa@zhaojun.com"));
		
		//设置To属性
		message.setRecipient(RecipientType.TO,new InternetAddress("bbb@qq.com"));
		
		//设置Subject属性
		message.setSubject("复杂邮件");
		
		//文本
		MimeBodyPart text = new MimeBodyPart();
		text.setContent("这是一副<br/><img src='cid:imageID'/><br/>图片<br/>","text/html;charset=UTF-8");
		
		//图片
		MimeBodyPart image = new MimeBodyPart();
		DataHandler dh = new DataHandler(new FileDataSource("src/cn/itcast/web/mail/config/神奇的图片.JPG"));
		image.setDataHandler(dh);
		image.setContentID("imageID");
		
		//附件
		MimeBodyPart append = new MimeBodyPart();
		dh = new DataHandler(new FileDataSource("src/cn/itcast/web/mail/config/学员提问.txt"));
		append.setDataHandler(dh);
		append.setFileName(MimeUtility.encodeText(dh.getName()));
		
		//文本+图片
		MimeMultipart mm1 = new MimeMultipart();
		mm1.addBodyPart(text);
		mm1.addBodyPart(image);
		mm1.setSubType("related");

		//将文本+图片封装成邮件的组成部分,即将关系看作成一个部分
		MimeBodyPart temp = new MimeBodyPart();
		temp.setContent(mm1);
		
		//设置和附件的关系
		MimeMultipart mm = new MimeMultipart();
		mm.addBodyPart(temp);
		mm.addBodyPart(append);
		mm.setSubType("mixed");
		
		//设置整个邮件的关系
		message.setContent(mm);
		
		//将该邮件保存的硬盘
		message.writeTo(new FileOutputStream("d:\\demo5.eml"));
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值