发送邮件完成实例(张孝祥视频教程内容)

package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
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 Demo3 {

 public static void main(String[] args) throws IOException, MessagingException{
  Properties props = new Properties();
  props.setProperty("mail.smtp.auth", "true");
  props.setProperty("mail.transport.protocol", "smtp");
  props.setProperty("mail.host", "smtp.163.com");
  Session session=Session.getInstance(props,
   new Authenticator(){
   protected PasswordAuthentication getPasswordAuthentication(){
    return new PasswordAuthentication("398198920","02040105");
   }
  }  
  );
  session.setDebug(true);
  MimeMessage msg=new MimeMessage(session);
  msg.setSubject("中文主题");
  msg.setFrom(new InternetAddress("\""+MimeUtility.encodeText("宋运兵")+"\"<398198920@163.com>"));
  msg.setReplyTo(new InternetAddress[]{new InternetAddress("398198920@qq.com")});//设置回复人
  msg.setRecipients(RecipientType.TO, InternetAddress.parse(""+MimeUtility.encodeText("宋运兵")+" <398198920@qq.com>,"+MimeUtility.encodeText("宋运兵")+" <398198920@163.com>"));
  
  MimeMultipart msgMultipart=new MimeMultipart("mixed");
  msg.setContent(msgMultipart);
  MimeBodyPart content=new MimeBodyPart();
  MimeBodyPart attch1=new MimeBodyPart();
  MimeBodyPart attch2=new MimeBodyPart();
  msgMultipart.addBodyPart(content);
  msgMultipart.addBodyPart(attch1);
  msgMultipart.addBodyPart(attch2);
  
  DataSource ds1=new FileDataSource("D:\\qq.txt");
  DataHandler dh1=new DataHandler(ds1);
  attch1.setDataHandler(dh1);
  attch1.setFileName(MimeUtility.encodeText("java培训.txt"));
  
  DataSource ds2=new FileDataSource("C:\\Documents and Settings\\song\\My Documents\\images\\bdcg.jpg");
  DataHandler dh2=new DataHandler(ds2);
  attch2.setDataHandler(dh2);
  attch2.setFileName("java2.jpg");
  
  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("C:\\Documents and Settings\\song\\My Documents\\images\\bdcg.jpg");
  DataHandler gifdh=new DataHandler(gifds);
  gifPart.setDataHandler(gifdh);
  gifPart.setHeader("Content-Location", "http://www.itcast.cn/bdcg.gif");
  
  htmlPart.setContent("abcdefghijklmnopqrst<img src='http://www.itcast.cn/bdcg.gif'/>", "text/html;charset=gbk");
  
  msg.saveChanges();
  
  OutputStream ous=new FileOutputStream("C:\\Documents and Settings\\song\\My Documents\\demo3.eml");
  msg.writeTo(ous);
  ous.close();
  
  Transport.send(msg);
   //msg=new MimeMessage(session,new FileInputStream("C:\\Documents and Settings\\song\\My Documents\\demo3.eml"));
  //Transport.send(msg,new InternetAddress[]{new InternetAddress("398198920@qq.com")});
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值