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

来源:互联网 发布:京东美工王破解版 编辑:程序博客网 时间:2024/05/14 19:57

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
原创粉丝点击