javamail send mail with 附件

慕璞
2023-12-01

package com.mail;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.PrintWriter;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.activation.*;
import java.io.IOException;

public class sendtest{
 
 public static class SMTPAuthenticator extends Authenticator
 {
  private String username;
  private String password;

  public SMTPAuthenticator(String username, String password)
  {
   super();
   this.username = username;
   this.password = password;
  }

  protected  PasswordAuthentication getPasswordAuthentication()
  {
   return new PasswordAuthentication(this.username, this.password);
  }
 }

 /*
   *   发送邮件
  *     * @param smtpHost
  *     * @param email
  *     * @throws MessagingException
  */
  protected static void sendMail(String email) throws MessagingException 
  { 
   try{
    Properties props = System.getProperties();
    props.put("mail.smtp.host", "mail.xxxx.com.cn");//SMTP服务器
    props.put("mail.smtp.auth", "true");//发送邮件时是否需要验证身份
    props.put("mail.mime.charset", "GB2312");//邮件正文的字符集
    String username = "xxxx";   //邮箱用户名
    String password = "xxxx";//邮箱密码
    SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password);//用户身份验证
    //create a new Session object
    Session session = Session.getInstance(props, authenticator);
    session.setDebug(false); //是否打印调试信息的标记
    String from = "xxxx@xxxx.com.cn"; //发送方邮箱
    String to = email;    //接收方邮箱
    String subject = "xxxxxx";//邮件标题
    //create a new MimeMessage object (using the Session created above)
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from, "xxxx", "gb2312"));
    InternetAddress[] address = new InternetAddress[] { new InternetAddress(to, "亲爱的用户", "gb2312") };
    message.setRecipients(Message.RecipientType.TO, address);
   
    //String file = "/home/qianglee/mail/user/datasmall/2005-01-11/xxxx/1105454895791.png";
    // Create your new message part
    BodyPart messageBodyPart = new MimeBodyPart();
   
    String mailTitle = "<HTML><HEAD><META HTTP-EQUIV=/"Content-Type/" CONTENT=/"text/html; charset=GB2312/"></HEAD><BODY>";
   
    String mailFoot = "</BODY></HTML>";

    String pBg = "<P>";
    String pEd = "</P>";

   
    String fonttmp = "中文测试";
    String fonttmp2 = "图文混排测试1 图文混排测试1 图文混排测试1 图文混排测试1 图文混排测试1 图文混排测试1";
    String fonttmp3 = "图文混排测试2 图文混排测试2 图文混排测试2 图文混排测试2 图文混排测试2 图文混排测试2";

    fonttmp = new String(fonttmp.getBytes("GB2312"),"ISO8859_1");
    fonttmp2 = new String(fonttmp2.getBytes("GB2312"),"ISO8859_1");
    fonttmp3 = new String(fonttmp3.getBytes("GB2312"),"ISO8859_1");
   
    String body = mailTitle + pBg+fonttmp+pEd;
   
    //http://220.194.61.227/user/data/2005-01-21/13311010093/1106278749591.png
   
    String file1 = "/home/qianglee/mail/user/data/2005-01-11/xxx/1105454895791.png";
   
    String file2 = "/home/qianglee/mail/user/data/2005-01-21/xxxx/1106278749591.png";

    String file3 = "/home/qianglee/mail/user/data/2005-01-21/xxxx/1106278745321.png";
   
    body += pBg + "<IMG SRC=/"cid:mememe1/">" + pEd;
   
   
    body += pBg+fonttmp2+pEd;

    body += pBg + "<IMG SRC=/"cid:mememe2/">" + pEd;
   
    body += pBg+fonttmp3+pEd;

    body += pBg + "<IMG SRC=/"cid:mememe3/">" + pEd;
   
    body += mailFoot;
   
    messageBodyPart.setContent(body,"text/html");
    // Create a related multi-part to combine the parts
    MimeMultipart multipart = new MimeMultipart("related");
    multipart.addBodyPart(messageBodyPart);
    // Create part for the image
    for(int i=1; i<4; i++){
    messageBodyPart = new MimeBodyPart();
    String t = "";
    if((i-1)==0)
     t = file1; 
    if((i-2)==0)
     t = file2;
    if((i-3)==0)
     t = file3; 
    // Fetch the image and associate to part
    DataSource fds = new FileDataSource(t);
    //DataSource fds2 = new FileDataSource(file2);
    //DataSource fds3 = new FileDataSource(file3);
    messageBodyPart.setDataHandler(new DataHandler(fds));
    //messageBodyPart.setDataHandler(new DataHandler(fds2));
    //messageBodyPart.setDataHandler(new DataHandler(fds3));
    String tm = "mememe"+i;
    messageBodyPart.setHeader("Content-ID",tm);
    //messageBodyPart.setHeader("Content-ID","mememe2");
    //messageBodyPart.setHeader("Content-ID","mememe3");
    // Add part to multi-part
    multipart.addBodyPart(messageBodyPart);
   
    // Associate multi-part with message
    message.setContent(multipart);
    }
    //如果不需要带附件,直接使用以下两行代码即可
    //message.setContent(body, "text/html");
    //message.setText(body, "GB2312");
    Transport.send(message);
    System.out.println("------邮件发送到["+to+"]成功-------/n");


   }catch(Throwable t){
   System.out.println("-------邮件没有发送成功-------/n");
   t.printStackTrace(System.out);
  }
  }

 public static void main(String args[])throws Exception{
  Thread.sleep(500);
  sendMail("xxxxxxxxxxx@xxxx.com.cn");
  Thread.sleep(500);
 }
  
}

 类似资料: