有一陣子沒用 java mail了,都有點忘了呵呵;java mail 經過幾年也有了一些容易感覺出來的變化

相同的:
一樣要用到 jaf 這個 framework (activation.jar) 跟 mail.jar (也可用單獨的 imap.jar, pop3.jar, smtp.jar.. 等);只是版本變新了

不同的:
選擇變多了,多了 jakarta 的 common-email-xxx.jar (分類在 common 專案中),感覺上好像有讓 java mail 變好用了 :-)

以下是直接用 java mail 的 code, 最原始的來源好像是用 java 附的 sample 中的一支 mail class 去改的
    public String Send() {
 	  String errMsg = null;

	  // create some properties and get the default Session
	  Properties props = System.getProperties();
	  props.put("mail.smtp.smtpServer", smtpServer);

	  Session session = Session.getDefaultInstance(props, null);
	  session.setDebug(debug);

	  try {
	      // create a message
	      MimeMessage msg = new MimeMessage(session);
  	      msg.setFrom(new InternetAddress(from));
	      InternetAddress[] address = {new InternetAddress(to)};
	      msg.setRecipients(Message.RecipientType.TO, address);

          if (cc != null) {
  	        InternetAddress[] addressCC = {new InternetAddress(cc)};
	        msg.setRecipients(Message.RecipientType.CC, addressCC);
          }
	      msg.setSubject(subject);

	      // create the Multipart and its parts to it
	      Multipart mp = new MimeMultipart();

	      // create and fill the first message part
	      MimeBodyPart mbp1 = new MimeBodyPart();
	      mbp1.setContent(msgContext,"text/html");

	      mp.addBodyPart(mbp1);

          if (attach.isEmpty()==false) {
            for (Enumeration e=attach.elements(); e.hasMoreElements();) {
              String file = (String)e.nextElement();

              file = attFilePath + file;
   	          FileDataSource fds = new FileDataSource(file);

		      // create the second message part
		      MimeBodyPart mbp2 = new MimeBodyPart();
	          mbp2.setDataHandler(new DataHandler(fds));
	          mbp2.setFileName(fds.getName());
        	  mp.addBodyPart(mbp2);
        	}
    	  }

	      // add the Multipart to the message
	      msg.setContent(mp);

	      // set the Date: header
	      msg.setSentDate(new Date());

	      // send the message
	      Transport.send(msg);

	      errMsg = "true";

	  }
      catch (MessagingException mex) {
	    mex.printStackTrace();
	    Exception ex = null;
	    if ((ex = mex.getNextException()) != null) {
		  errMsg = "message : " + ex.toString();
	    }
	  }

      return errMsg;
    }


以下是用 common-email 的 code
	public static void send(Hashtable ht, Staff me, ArticleForm form,

			String host, String serv, String contextPath) {

		HtmlEmail email = new HtmlEmail();
		email.setHostName(host);

		try {
			String subject = ((ArticleForm) form).getCreator() + " 發佈訊息";
			email.setSubject(subject);

			StringBuffer msg = new StringBuffer("");
msg.append("");
msg.append(((ArticleForm) form).getTitle());
msg.append("... (按我)");
msg.append("
");
msg.append("
"); log.debug(msg.toString()); email.setHtmlMsg(zhtw2en(msg.toString())); String sender = me.getEmail() + Tokens.EMAIL_SEP + serv; log.debug(sender); email.setFrom(sender, "Me"); email.addCc(sender); List list = new Vector(); Set set = ht.keySet(); Iterator it = set.iterator(); while (it.hasNext()) { String s = (String) it.next(); Staff u = (Staff) ht.get(s); if (u.getName().equals(me.getName())) continue; // log.debug(u.getEmail() + "," + u.getIp()); if ((u.getEmail() != null) && (u.getEmail().length() > 0)) { String em = u.getEmail() + Tokens.EMAIL_SEP + serv; log.debug(em); try { list.add(new InternetAddress(em)); } catch (AddressException e) { log.error(e); } } } email.setTo(list); email.send(); } catch (EmailException e) { log.error("mail:" + e); } }
二段 code 都是用 mail 寄送 html 格式的訊息,另
1。要下載 jaf & mai.jar 參考資料:JavaWorld (sun 網站也有)
2。要執行 mail 的 class 要加入 jaf & mail 的 jar file 在 CLASSPATH
3。在 code 中直接用中文實在不是好習慣,因為在 cross platform 時會有問題
創作者介紹
創作者 心得筆記 的頭像
identical

心得筆記

identical 發表在 痞客邦 留言(0) 人氣( 330 )