java email 发送带附件的邮件

mixed、related 请看 http://360193550.iteye.com/blog/1939073
有一个问题是 163邮箱,设置
MimeMultipart content_body=new MimeMultipart("related");

不管用,直接把内容当作附件显示了!qq 行浪都没有问题,怎么设置下呢,知道的网友给留下言谢谢!分享学习进步!!!
代码:
package com.my.mail;

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
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;

import org.apache.log4j.Logger;

/**
 * 发送带附件和本地图片信息的邮件
 * @author
 */
public class Test3 {
	static Logger logger=Logger.getLogger(Test3.class);
	
	public static void main(String[] args) {
		try{
			Properties ps=new Properties();
			ps.put("mail.smtp.auth", "true");
			ps.put("mail.transport.protocol", "smtp");
			ps.put("mail.host", "smtp.qq.com");
			
			Session ss=Session.getInstance(ps, new Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("[email protected]","密码");
				}
			});
			
			Message msg=new MimeMessage(ss);
			msg.setFrom(new InternetAddress("\""+MimeUtility.encodeText("神一般的哥")+"\"<[email protected]>"));
			msg.setSubject("测试自己写客户端发送Email");
			//msg.setRecipients(RecipientType.TO, InternetAddress.parse(MimeUtility.encodeText("#######[email protected]")+"<[email protected]>"));
			//用逗号分割开来实现群发
			msg.setRecipients(RecipientType.TO, InternetAddress.parse(MimeUtility.encodeText("白白")+"<[email protected]>,"+MimeUtility.encodeText("丫头")+"<[email protected]>,"+MimeUtility.encodeText("嗨皮")+"<[email protected]>"));
			
			MimeMultipart content=new MimeMultipart("mixed");
			msg.setContent(content);
			
			MimeBodyPart part_attch01=new MimeBodyPart();
			MimeBodyPart part_attch02=new MimeBodyPart();
			MimeBodyPart part_body=new MimeBodyPart();
			
			content.addBodyPart(part_attch01);
			content.addBodyPart(part_attch02);
			content.addBodyPart(part_body);
			
			DataSource ds01=new FileDataSource("res\\123.jpg");
			DataHandler dh01=new DataHandler(ds01);
			part_attch01.setDataHandler(dh01);
			part_attch01.setFileName(MimeUtility.encodeText("宇智波鼬.jpg"));
			DataSource ds02=new FileDataSource("res\\123.txt");
			DataHandler dh02=new DataHandler(ds02);
			part_attch02.setDataHandler(dh02);
			part_attch02.setFileName(MimeUtility.encodeText("测试文本.txt"));
			MimeMultipart content_body=new MimeMultipart("related");
			part_body.setContent(content_body);
			
			MimeBodyPart content_pic=new MimeBodyPart();
			MimeBodyPart content_html=new MimeBodyPart();
			content_body.addBodyPart(content_pic);
			content_body.addBodyPart(content_html);
			
			DataSource ds_pic=new FileDataSource("res\\456.jpg");
			DataHandler dh_pic=new DataHandler(ds_pic);
			content_pic.setDataHandler(dh_pic);
			content_pic.setHeader("Content-Location", "http://www.baidu.com/img/bdlogo.gif");
			content_pic.setFileName(MimeUtility.encodeText("大蛇丸.jpg"));
			
			content_html.setContent("试试本地图片资源<img src='http://www.baidu.com/img/bdlogo.gif'>", "text/html;charset=gbk");
			
			Transport.send(msg);
			
		}catch(Exception ex){
			logger.debug("===  Exception : Test3 Main ===",ex);
		}
	}
}



所需jar包http://pan.baidu.com/share/link?shareid=2078740269&uk=2248831455

你可能感兴趣的:(javaEmail)