<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

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

    ? public static boolean send(String fromName, String fromAddr, String to, String subject, String
    ???????????????????????????? body, String fileName, byte[] file) throws
    ????? Exception {
    ??????? //發送人名稱,用base64編碼,再加上特殊標志
    ??????? 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將加入到此處創建的Multipart中
    ??? Multipart mp = new MimeMultipart();
    // Create the message part
    ??? BodyPart messageBodyPart = new MimeBodyPart();

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

    ??? mp.addBodyPart(messageBodyPart);

    ????? /*發送附件*/
    ???? if (file != null && file.length > 0) {
    ?????? //利用枚舉器方便的遍歷集合
    ???????? MimeBodyPart mbp = new MimeBodyPart();?
    //???????? File fileTmp = null;
    ???????? //得到數據源
    //???????? 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");

    ??? //添加認證信息
    ??? 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 小鐵匠 閱讀(645) 評論(1)  編輯  收藏 所屬分類: java

    評論:
    # re: java 發mail 2008-04-01 22:16 | 草包書生
    我也來研究一下,支持中文的附件  回復  更多評論
      
    主站蜘蛛池模板: 无码囯产精品一区二区免费| 天堂亚洲国产中文在线| 亚洲va中文字幕| 91手机看片国产永久免费| 亚洲色大18成人网站WWW在线播放| 免费国产成人午夜电影| 国产精品免费福利久久| 亚洲码欧美码一区二区三区| 亚洲中文字幕无码中文字在线| 日本免费人成在线网站| 特色特黄a毛片高清免费观看| 国产成人精品免费视频大全五级| 黄床大片免费30分钟国产精品| 亚洲人成高清在线播放| 亚洲一区精品伊人久久伊人| 成人在线免费看片| 国产精品内射视频免费| 中文字幕精品三区无码亚洲| 亚洲日韩精品一区二区三区无码| 成人人观看的免费毛片| 久久美女网站免费| 日韩国产欧美亚洲v片| 亚洲精品**中文毛片| 老司机亚洲精品影视www| 91视频国产免费| 国产99视频精品免费专区| 精品特级一级毛片免费观看| 亚洲综合色一区二区三区小说| 亚洲国产中文v高清在线观看| 免费人成网站在线观看10分钟| 中国人免费观看高清在线观看二区 | 亚洲中文无码亚洲人成影院| 好看的电影网站亚洲一区| 国产成人在线观看免费网站 | 含羞草国产亚洲精品岁国产精品| 四虎影视永久免费观看| 国产激情久久久久影院老熟女免费| 亚洲香蕉久久一区二区| 亚洲美女精品视频| 亚洲无线码在线一区观看| 亚洲精品乱码久久久久久不卡|