JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » EJB / 其它Java EE 討論區 » JavaMail  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
己加入精華區
by koji at 2008-07-08 09:51
本主題所含的標籤
無標籤
作者 [分享]JavaMail利用Exchange Server發送Html格式並夾帶附件 [精華]
oldmemory

海無界、心有帆



發文: 95
積分: 2
於 2008-07-08 09:35 user profilesend a private message to usersend email to oldmemoryreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這是小弟這幾天研究了一下JavaMail利用Exchange Server來做發信動作的原始碼!
分享給大家,希望對大家有所幫助!
程式碼中都有註解說明,應該會很容易明白和了解!
也希望有疑問或有問題的地方,還望各位能指點和提出討論!^^

改善空間,可將MailServer類別中的sendMail改成帶參數,
並利用JavaBean的setter/getter方式,做成發送信件模組!
後續改善方式很多,若大家有不錯想法的可以提出討論!

MailMain.java
1
2
3
4
5
6
7
8
9
public class MailMain {
   public static void main(String [] args){
      MailServer ms = new MailServer();
      if(ms.sendMail())
         System.out.println("信件發送成功!");
      else
         System.out.println("信件發送失敗!");
   }
}


MailServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;
 
class MailServer {
   public MailServer(){}
   
   public boolean sendMail(){
      boolean debug = false;
      //收件者、主旨、內文的設定
      String[] To = {"xxxxxx@gmail.com","xxxxxx@pchome.com.tw","xxxxxx@yahoo.com.tw"};
      String[] Cc = {"xxxxxx@hotmail.com"};
      String[] Bcc = {"xxxxxx@xxxxxx.com.tw"};
      String Subject = "測試信件,html內容含附檔"; //主旨
      String sendMsgHtml = "<html><body>測試內容,我的姓名叫<h1>AJ</h1></body></html>"; //HTML內文
      
      //郵件資訊設定    
      String From = "xxxxxx@xxxxxx.com.tw"; //發信人信箱
      String Personal = "xxxxxx"; //發信人(顯示的文字)
      String smtpServer = "xxxxxx.xxxxxx.xxxxxx.xxxxxx"; //Mail Server
      String account = "xxxxxx"; //登入帳號
      String passwd = "xxxxxx"; //登入密碼
 
      //設定SMTP伺服器
      Properties props = new Properties();
      props.put("mail.smtp.host", smtpServer);
      props.put("mail.smtp.port", "25");
      props.put("mail.smtp.auth", "true"); //採用認證,因為使用外部smtp主機
      props.put("mail.smtp.connectiontimeout","1000");
      props.put("mail.smtp.timeout","2000");
      Authenticator smtpAuthenticator = new SMTPAuthenticator(account, passwd);
 
      //建立session
      Session session = null;
      MimeMessage msg = null;
      try{
         session = Session.getDefaultInstance(props, smtpAuthenticator);
         session.setDebug(debug); //停止偵錯
         
         //設定寄件者
         msg = new MimeMessage(session); //建立新的MimeMessage物件   
         msg.setFrom(new InternetAddress(From,Personal,"Big5"));
 
         //設定收件者TO、Cc、Bcc資料
         InternetAddress[] Toaddress = new InternetAddress[To.length];
          for (int i = 0; i < To.length; i++)
               Toaddress[i] = new InternetAddress(To[i]);
               
         msg.setRecipients(Message.RecipientType.TO,Toaddress);
 
         if(Cc != null){ //驗證是否有多位副本收件者
            InternetAddress[] Ccaddress = new InternetAddress[Cc.length];
            for (int i = 0; i < Cc.length; i++)
               Ccaddress[i] = new InternetAddress(Cc[i]);
            msg.setRecipients(Message.RecipientType.CC,Ccaddress);
         }
            
         if(Bcc != null){ //驗證是否有多位密件副本收件者
            InternetAddress[] Bccaddress = new InternetAddress[Bcc.length];
            for (int i = 0; i < Bcc.length; i++)
               Bccaddress[i] = new InternetAddress(Bcc[i]);
            msg.setRecipients(Message.RecipientType.BCC, Bccaddress); 
         }                   
         
         //其他設定
         msg.setSubject(Subject,"Big5");//設定郵件主旨
         msg.setSentDate(new Date()); //設定發送時間        
         
         //建立多內容 Multpart 郵件物件
         Multipart multipart = new MimeMultipart();
         
         MimeBodyPart mbp = new MimeBodyPart();
         
         //建立html格式內容
         mbp.setContent(sendMsgHtml,"text/html;charset=big5");
         
         //將內容加入 Multpart 郵件物件
         multipart.addBodyPart(mbp);
         
         //附加檔案
         mbp = new MimeBodyPart();
         DataSource fds = new FileDataSource("report\\附件.txt");
         mbp.setDataHandler(new DataHandler(fds));
         mbp.setFileName(MimeUtility.encodeText(fds.getName(), "Big5", "B"));         
 
         //將附加檔案內容加入 Multpart 郵件物件
         multipart.addBodyPart(mbp);
         
         //對 Multpart 郵件物件設定訊息
         msg.setContent(multipart);
 
         Transport.send(msg);
         return true; //發送信件動作正常
      }catch(Exception e){
         e.printStackTrace();
      }finally{
         if(msg!=null) msg = null;
         if(session!=null) session = null;
      }
      return false; //發送信件動作異常
   }
}


SMTPAuthenticator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 *驗證發信人權限
 **/
import javax.mail.*;
 
class SMTPAuthenticator extends javax.mail.Authenticator {
  private String fUser;
  private String fPassword;
 
  public SMTPAuthenticator(String user, String password) {
    fUser = user;
    fPassword = password;
  }
 
  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(fUser, fPassword);
  }
}


oldmemory edited on 2008-07-08 09:42
reply to postreply to post
» JWorld@TW »  EJB / 其它Java EE 討論區 » JavaMail

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8