<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-17  評論-6  文章-1  trackbacks-0

    有發(fā)送人名稱中文支持,支持bytes格式附件,附件中文支持

    ? public static boolean send(String fromName, String fromAddr, String to, String subject, String
    ???????????????????????????? body, String fileName, byte[] file) throws
    ????? Exception {
    ??????? //發(fā)送人名稱,用base64編碼,再加上特殊標(biāo)志
    ??????? fromName = "=?GB2312?B?" + new String(base64.encode((fromName).getBytes()))? + "?=";
    ??? Properties props = new Properties();
    ??? Session session = Session.getInstance(props, null);
    ??? props.put("mail.smtp.host", Constants.mailhost);
    ??? props.put("mail.smtp.auth", "false");?
    ??? Message msg = new MimeMessage(session);
    ????? msg.setFrom(new InternetAddress(fromAddr,fromName));
    //后面的BodyPart將加入到此處創(chuàng)建的Multipart中
    ??? Multipart mp = new MimeMultipart();
    // Create the message part
    ??? BodyPart messageBodyPart = new MimeBodyPart();

    ??? // Fill the message
    ??? messageBodyPart.setText(body);

    ??? mp.addBodyPart(messageBodyPart);

    ????? /*發(fā)送附件*/
    ???? if (file != null && file.length > 0) {
    ?????? //利用枚舉器方便的遍歷集合
    ???????? MimeBodyPart mbp = new MimeBodyPart();?
    //???????? File fileTmp = null;
    ???????? //得到數(shù)據(jù)源
    //???????? FileDataSource fds = new FileDataSource(fileTmp);
    ???????? //得到附件本身并至入BodyPart
    ???????? mbp.setDataHandler(new DataHandler(new ByteArrayDataSource(file,"application/octet-stream")));
    ???????? //得到文件名同樣至入BodyPart
    ???????? mbp.setFileName(MimeUtility.encodeWord(fileName,"GB2312",null));
    ???????? mp.addBodyPart(mbp);
    ???? }
    ???
    ??? //Multipart加入到信件
    ??? msg.setContent(mp);

    ??? msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    ??? msg.setSubject(subject);

    ??? msg.setHeader("X-Mailer", "personal Email Sender");
    ??? msg.setSentDate(new Date());

    ??? Transport transport = session.getTransport("smtp");

    ??? //添加認(rèn)證信息
    ??? transport.connect(Constants.mailhost, Constants.user, Constants.pwd);
    ??? transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
    ??? transport.close();
    ??? return true;
    ? }




    import java.io.*;
    import javax.activation.*;

    public class ByteArrayDataSource implements DataSource {
    ??? /** * Data to write. */
    ??? private byte[] _data;

    ??? /** * Content-Type. */
    ??? private String _type;

    ??? /* Create a datasource from an input stream */
    ??? public ByteArrayDataSource(InputStream is, String type) {
    ??????? _type = type;
    ??????? try {
    ??????????? ByteArrayOutputStream os = new ByteArrayOutputStream();
    ??????????? int ch;

    ??????????? // XXX : must be made more efficient by
    ??????????? // doing buffered reads, rather than one byte reads
    ??????????? while ((ch = is.read()) != -1)
    ??????????????? os.write(ch);
    ??????????? _data = os.toByteArray();
    ??????? } catch (IOException ioe) {
    ??????? }
    ??? }

    ??? /* Create a datasource from a byte array */
    ??? public ByteArrayDataSource(byte[] data, String type) {
    ??????? _data = data;
    ??????? _type = type;
    ??? }

    ??? /* Create a datasource from a String */
    ??? public ByteArrayDataSource(String data, String type) {
    ??????? try {
    ??????????? // Assumption that the string contains only ascii
    ??????????? // characters ! Else just pass in a charset into this
    ??????????? // constructor and use it in getBytes()
    ??????????? _data = data.getBytes("iso-8859-1");
    ??????? } catch (UnsupportedEncodingException uee) {
    ??????? }
    ??????? _type = type;
    ??? }

    ??? public InputStream getInputStream() throws IOException {
    ??????? if (_data == null)
    ??????????? throw new IOException("no data");
    ??????? return new ByteArrayInputStream(_data);
    ??? }

    ??? public OutputStream getOutputStream() throws IOException {
    ??????? throw new IOException("cannot do this");
    ??? }

    ??? public String getContentType() {
    ??????? return _type;
    ??? }

    ??? public String getName() {
    ??????? return "dummy";
    ??? }
    }

    posted on 2006-05-10 18:02 小鐵匠 閱讀(642) 評論(1)  編輯  收藏 所屬分類: java

    評論:
    # re: java 發(fā)mail 2008-04-01 22:16 | 草包書生
    我也來研究一下,支持中文的附件  回復(fù)  更多評論
      
    主站蜘蛛池模板: 日本一道本不卡免费| 精品久久久久久久久亚洲偷窥女厕| 一边摸一边爽一边叫床免费视频 | 亚洲人AV在线无码影院观看| 日韩免费高清大片在线| 久久综合图区亚洲综合图区| 羞羞视频免费网站在线看| 在线观看亚洲成人| 久久精品成人免费观看97| 伊人久久大香线蕉亚洲五月天| 久久www免费人成精品香蕉| 亚洲自偷自偷图片| 久久久久成人精品免费播放动漫| 亚洲bt加勒比一区二区| 51视频精品全部免费最新| 亚洲冬月枫中文字幕在线看| 女人毛片a级大学毛片免费| 国产成人高清亚洲一区91| 国产精品V亚洲精品V日韩精品| 免费看一区二区三区四区| 亚洲成AV人综合在线观看 | 免费无码一区二区三区| 亚洲国产午夜电影在线入口| 毛片免费在线视频| 一级特黄a免费大片| 亚洲精品免费观看| 很黄很色很刺激的视频免费| 色噜噜的亚洲男人的天堂| 亚洲人成在线播放网站| 精品女同一区二区三区免费站| 亚洲欧好州第一的日产suv| 亚洲午夜精品一级在线播放放 | 国产精品亚洲片在线| 麻豆国产精品免费视频| 黄网站在线播放视频免费观看| 国产亚洲人成网站在线观看| 100部毛片免费全部播放完整| 亚洲国产aⅴ成人精品无吗| 久久亚洲国产精品一区二区| 毛片在线看免费版| 中文字幕视频免费在线观看|