JavaMail java邮件开发_中文附件名_回信地址_友好名称等技巧

JavaMail03:

package com.yuming.test;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;


public class JavaMail03 {

	public static void main(String[] args) throws Exception {
		
		Session session = Session.getInstance(new Properties());
		MimeMessage msg = new MimeMessage(session);
		//发件人
		msg.setFrom(new InternetAddress("\""+MimeUtility.encodeText("中文名称")+"\" <itcast_test@sina.com>"));
		
		//回复
		//注:发件人与回复的人不是同一人
		//收件人只有一个
		msg.setReplyTo(new Address[]{new InternetAddress("lili@126.com")});
		//收件人有多个
		msg.setRecipients(RecipientType.TO, InternetAddress.parse(MimeUtility.encodeText("张三")+" <zhangsan@itcast.cn>,"+MimeUtility.encodeText("李四") +"<lisi@itcast.cn>"));
		
		
		msg.setSubject("这是发邮件的主题");
		
		//mixed---混合关系
		MimeMultipart  msgMultipart = new MimeMultipart("mixed");
		msg.setContent(msgMultipart);
		msg.getReplyTo();
		
		//html body与附件整合
		//body
		MimeBodyPart content = new MimeBodyPart();
		//附件1
		MimeBodyPart attch1 = new MimeBodyPart();
		//附件2
		MimeBodyPart attch2 = new MimeBodyPart();
		
		msgMultipart.addBodyPart(content);
		msgMultipart.addBodyPart(attch1);
		msgMultipart.addBodyPart(attch2);
		
		//附件1
	    DataSource ds = new FileDataSource("D:\\ycsql.txt");
		DataHandler dh1 = new DataHandler(ds );
		attch1.setDataHandler(dh1);
		//英文名称
		//attch1.setFileName("ycsql.txt");
		//中文名称
		attch1.setFileName(
				MimeUtility.encodeText("yxsql文件.txt")
				);
		
		//附件2
		 DataSource ds2 = new FileDataSource("D:\\log.jpg");
		 DataHandler dh2 = new DataHandler(ds2 );
		 attch1.setDataHandler(dh2);
		 attch2.setFileName("log.jpg");
		 
		 //内容 related--关联关系
		 MimeMultipart  bodyMultipart = new MimeMultipart("related");
		 content.setContent(bodyMultipart);
		 
		 MimeBodyPart htmlpart = new MimeBodyPart();
		 MimeBodyPart gifpart = new MimeBodyPart();
		 bodyMultipart.addBodyPart(htmlpart);
		 bodyMultipart.addBodyPart(gifpart);
	
		 DataSource gifds = new FileDataSource("D:\\log.jpg");
		 DataHandler gifdh = new DataHandler(gifds );
		 gifpart.setDataHandler(gifdh);
		 gifpart.setHeader("Content-Location", "D:\\log.jpg");
		 
		 //htmlpart
		 htmlpart.setContent("我自己用程序生成和发送的邮件哦!<img src='D:\\log.jpg'>", "text/html;charset=UTF-8");
		 
		 //根据这些对象生成邮件
		 msg.saveChanges();
		 
		 OutputStream os =  new FileOutputStream("E:\\JavaMail03.eml");
		 msg.writeTo(os);
		// msg.writeTo(os);
		 os.close();
		 

	}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值