可带附件的复杂邮件发送实例

Properties props = new Properties();

       props.setProperty("mail.transport.protocol", "smtp");

       props.setProperty("mail.smtp.auth", "true");

       props.setProperty("mail.host", "smtp.163.com");

       Session session = Session.getInstance(props,new Authenticator(){

 

           @Override

           protected PasswordAuthentication getPasswordAuthentication() {

              // TODO Auto-generated method stub

              return new PasswordAuthentication("aaaa","******");

           }

          

       });

       session.setDebug(true);

      

       Message msg = new MimeMessage(session);

       msg.setFrom(new InternetAddress("\""+MimeUtility.encodeText("小明")+"\" <[email protected]>"));  //在邮件的发件人中显示中文

       msg.setSubject("复杂的邮件");

       msg.setRecipients(RecipientType.TO, InternetAddress.parse([email protected],[email protected]));

msg.setReplyTo(new Address[]{new InternetAddress("[email protected]")});   //回复时默认回复此地址

 

       Multipart msgMultipart = new MimeMultipart("mixed");    //邮件主体,是并列组合关系,因为附件和内容之间是并列的,这里不要用multipart/mixed

       msg.setContent(msgMultipart);

      

       MimeBodyPart attch1 = new MimeBodyPart();     //附件1

       MimeBodyPart attch2 = new MimeBodyPart();     //附件2

       MimeBodyPart htmlBody = new MimeBodyPart();      //主体内容

       msgMultipart.addBodyPart(attch1);

       msgMultipart.addBodyPart(attch2);

       msgMultipart.addBodyPart(htmlBody);

      

       DataSource ds1 = new FileDataSource("E:\\学习文件\\Java\\javamail\\JavaMail API简介().doc");       //附件1的文件

       DataHandler dh1 = new DataHandler(ds1);

       DataSource ds2 = new FileDataSource("E:\\学习文件\\Java\\javamail\\Java邮件发送实例.doc");    //附件2的文件

       DataHandler dh2 = new DataHandler(ds2);

      

       attch1.setDataHandler(dh1);

       attch2.setDataHandler(dh2);

       attch1.setFileName(MimeUtility.encodeText("JavaMail API简介().doc"));        //设置附件的文件名,不可少(有中文的要编码)

       attch2.setFileName(MimeUtility.encodeText("Java邮件发送实例.doc"));

      

       Multipart htmlPart = new MimeMultipart("related");      //主体内容里又包含多部分,是html和图片,它们的关系是依赖,这里不要用multipart/related

       htmlBody.setContent(htmlPart);

      

       MimeBodyPart contBody = new MimeBodyPart();          //主体内容里的html内容

       MimeBodyPart gifBody = new MimeBodyPart();           //主体内容里的图片内容

       htmlPart.addBodyPart(contBody);

       htmlPart.addBodyPart(gifBody);

      

       contBody.setContent("<span style='color:green;'>你好吗,朋友!</span><img src='http://www.wwc.com/邮件主体的组合关系.jpg'>", "text/html;charset=gbk");

       DataSource gifDS = new FileDataSource("E:\\学习文件\\Java\\javamail\\邮件主体的组合关系.jpg");

       DataHandler gifDH = new DataHandler(gifDS);

       gifBody.setDataHandler(gifDH);

       gifBody.setHeader("Content-Location", MimeUtility.encodeText("http://www.wwc.com/邮件主体的组合关系.jpg"));       //设置html部分引用的图片地址

      

       msg.saveChanges();          //把所有的内容保存在message

//     OutputStream msgFile = new FileOutputeam("E:\\学习文件\\Java\\javamail\\测试复杂邮件.eml");

//     msg.writeTo(msgFile);

//     msgFile.close();

      

       Transport.send(msg);

你可能感兴趣的:(java,html,qq)