隨筆 - 23  文章 - 11  trackbacks - 0
          <2006年9月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          common mail是一個小而方便的mail包,他實現了對Java Mail的封裝,使用起來十分的方便,但是我在使用他的時候發現,使用純文本的內容發送,結果是亂碼,代碼如下:
          public class TestCommonMail {
          public static void main(String[] args) throws EmailException, MessagingException {
          SimpleEmail email = new SimpleEmail();
          email.setCharset("GB2312");
          email.setHostName("smtp.163.com");
          email.setSubject("test");
          email.addTo("test@163.com");
          email.setFrom("test@163.com");
          email.setMsg("我的測試");
          email.setAuthentication("test", "test");
          email.send();
          }
          }

          分析了一下commons mail的源碼找到了原因。源碼如下:
          public class SimpleEmail extends Email
          {
          public Email setMsg(String msg) throws EmailException, MessagingException
          {
          if (EmailUtils.isEmpty(msg))
          {
          throw new EmailException("Invalid message supplied");
          }

          setContent(msg, TEXT_PLAIN);
          return this;
          }
          }

          Email代碼片段
          public void setContent(Object aObject, String aContentType)
          {
          this.content = aObject;
          if (EmailUtils.isEmpty(aContentType))
          {
          this.contentType = null;
          }
          else
          {
          // set the content type
          this.contentType = aContentType;

          // set the charset if the input was properly formed
          String strMarker = "; charset=";
          int charsetPos = aContentType.toLowerCase().indexOf(strMarker);
          if (charsetPos != -1)
          {
          // find the next space (after the marker)
          charsetPos += strMarker.length();
          int intCharsetEnd =
          aContentType.toLowerCase().indexOf(" ", charsetPos);

          if (intCharsetEnd != -1)
          {
          this.charset =
          aContentType.substring(charsetPos, intCharsetEnd);
          }
          else
          {
          this.charset = aContentType.substring(charsetPos);
          }
          }
          }
          }

          email.send();的send方法將調用
          public void buildMimeMessage() throws EmailException
          {
          try
          {
          this.getMailSession();
          this.message = new MimeMessage(this.session);

          if (EmailUtils.isNotEmpty(this.subject))
          {
          if (EmailUtils.isNotEmpty(this.charset))
          {
          this.message.setSubject(this.subject, this.charset);
          }
          else
          {
          this.message.setSubject(this.subject);
          }
          }

          // ========================================================
          // Start of replacement code
          if (this.content != null)
          {
          this.message.setContent(this.content, this.contentType);
          }
          // end of replacement code
          // ========================================================
          else if (this.emailBody != null)
          {
          this.message.setContent(this.emailBody);
          }
          else
          {
          this.message.setContent("", Email.TEXT_PLAIN);
          }

          if (this.fromAddress != null)
          {
          this.message.setFrom(this.fromAddress);
          }
          else
          {
          throw new EmailException("Sender address required");
          }

          if (this.toList.size() + this.ccList.size() + this.bccList.size() == 0)
          {
          throw new EmailException(
          "At least one receiver address required");
          }

          if (this.toList.size() > 0)
          {
          this.message.setRecipients(
          Message.RecipientType.TO,
          this.toInternetAddressArray(this.toList));
          }

          if (this.ccList.size() > 0)
          {
          this.message.setRecipients(
          Message.RecipientType.CC,
          this.toInternetAddressArray(this.ccList));
          }

          if (this.bccList.size() > 0)
          {
          this.message.setRecipients(
          Message.RecipientType.BCC,
          this.toInternetAddressArray(this.bccList));
          }

          if (this.replyList.size() > 0)
          {
          this.message.setReplyTo(
          this.toInternetAddressArray(this.replyList));
          }

          if (this.headers.size() > 0)
          {
          Iterator iterHeaderKeys = this.headers.keySet().iterator();
          while (iterHeaderKeys.hasNext())
          {
          String name = (String) iterHeaderKeys.next();
          String value = (String) headers.get(name);
          this.message.addHeader(name, value);
          }
          }

          if (this.message.getSentDate() == null)
          {
          this.message.setSentDate(getSentDate());
          }

          if (this.popBeforeSmtp)
          {
          Store store = session.getStore("pop3");
          store.connect(this.popHost, this.popUsername, this.popPassword);
          }
          }
          catch (MessagingException me)
          {
          throw new EmailException(me);
          }
          }
          由代碼可以知道純文本方式最終調用了Java Mail的
          message.setContent(this.content, this.contentType);
          content是內容
          contentType是類型,如text/plain,
          (我們可以試試直接用Java mail發郵件,設置文本內容不使用setText方法,也使用setContent("測試", "text/plain")方式,你可以看到內容也是亂碼)
          關鍵就在于text/plain,我們改成text/plain;charset=gb2312,ok亂碼解決了。在commons mail我們看SimpleEmail 類中setMsg方法調用的就是 setContent(msg, TEXT_PLAIN);我們只需要將Email類中的常量TEXT_PLAIN修改一下加入 charset=你的字符集 ,重新打包jar,這樣就可以了

          posted on 2006-09-18 09:54 小小~咖啡豆 閱讀(1372) 評論(0)  編輯  收藏 所屬分類: 其他
          主站蜘蛛池模板: 明光市| 沂源县| 高尔夫| 临潭县| 辽阳县| 正镶白旗| 昭苏县| 光泽县| 保山市| 陆良县| 安福县| 华坪县| 天峻县| 收藏| 买车| 区。| 攀枝花市| 祁门县| 密山市| 武川县| 平谷区| 平利县| 广河县| 巴东县| 孝义市| 化隆| 威远县| 建瓯市| 广饶县| 凤翔县| 从化市| 五莲县| 灵台县| 邵阳县| 陕西省| 梁河县| 二连浩特市| 彭水| 新密市| 荔浦县| 三都|