發表於 程式分享

Java開發mail寄送含附件及數位簽章

1.需先引用此3個jar檔: bcmail-jdk16-146.jar、bcprov-jdk16-146.jar、mail.jar

2.先申請mail測試憑證
同事介紹一免費憑證申請Free Email Certificate
請用IE申請(我用Chrome無法申請),申請後收到email點選確認信後,
憑證是下載到IE / 工具 / 網際網路選項 / 內容 / 憑證 / “個人"頁
找到新申請的憑證
發給欄: 為申請的email帳號,
簽發者: 為COMODO RSA Client Authentication and Secure Email CA,
請將檔案匯出成pfx檔。

3.程式碼: 為方便測試我以jsp撰寫

<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<%@ page import="java.io.*" %>
<%@ page import="java.security.*" %>
<%@ page import="java.security.cert.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="org.bouncycastle.mail.smime.*" %>
<%@ page import="javax.activation.*" %>
<%
    //1.讀取pfx檔
    KeyStore keyStore = KeyStore.getInstance("PKCS12");   
    //請將匯出的pfx檔放至與jsp檔案同一路徑
    String pfxFilePath = application.getRealPath("/test.pfx");    
    FileInputStream fis = new FileInputStream(pfxFilePath); 
    //xxxxxx請用pfx檔匯出時儲存的密碼
    char[] key = "xxxxxx".toCharArray();
    keyStore.load( (InputStream) fis, key);
    Enumeration e = keyStore.aliases();
    String alias = (String) e.nextElement();    
    PrivateKey pk = (PrivateKey) keyStore.getKey(alias, key);   
    X509Certificate pkcs12Cert = (X509Certificate) keyStore.getCertificate(alias);  
    SMIMESignedGenerator gen = new SMIMESignedGenerator();  
    gen.addSigner(pk, pkcs12Cert, SMIMESignedGenerator.DIGEST_SHA1);
    
    //2.email 內容
    MimeBodyPart msg = new MimeBodyPart();
    
    MimeBodyPart mbp1 = new MimeBodyPart();
    String message = "這是測試資料.";
    mbp1.setContent(message.toString(), "text/html; charset=big5");
    
    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp1);
    
    //3.email 附檔
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    String attachFile = "下載.png";
    String attachPath = application.getRealPath("/" + attachFile);
    DataSource source = new FileDataSource(attachPath);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(MimeUtility.encodeText(attachFile, "BIG5", "B"));
    mp.addBodyPart(messageBodyPart);

    msg.setContent(mp); 

    //4.以pfx簽 email 內容 及 email 附檔
    MimeMultipart mm = gen.generate(msg, new org.bouncycastle.jce.provider.BouncyCastleProvider ());
    
    Properties props = System.getProperties();
    props.put("mail.smtp.host", "webmail.xxx.com.tw");
    Session mailSession = Session.getDefaultInstance(props, null);
    
    //5.設定寄件者及收件者
    MimeMessage body = new MimeMessage(mailSession);
    InternetAddress fromUser = new InternetAddress("service@xxx.com.tw");
    fromUser.setPersonal("測試人員", "big5");
    body.setFrom(fromUser);
    
    String contact = "user1@xxx.com.tw, user2@xxx.com.tw, user3@xxx.com.tw";
    body.setRecipients(Message.RecipientType.TO, InternetAddress.parse(contact.trim()));
    
    //6.設定標題、內文
    body.setSubject("Signed message 範例" ,"big5");
    body.setHeader("X-Mailer", "Java mailer"); 
    body.setContent(mm, mm.getContentType());
    
    body.saveChanges();
    
    //7.傳送
    Transport.send(body);
%>

4.傳送結果
1

發表留言