<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女电影网| 成人性生交大片免费看好| 国产亚洲人成无码网在线观看| 成在人线av无码免费高潮水| 色噜噜AV亚洲色一区二区| 久青草视频97国内免费影视| 久久久久亚洲av无码尤物| 无码人妻精品中文字幕免费| 亚洲精品456在线播放| 歪歪漫画在线观看官网免费阅读| 国产亚洲精aa在线看| 处破痛哭A√18成年片免费| 亚洲av片在线观看| 红杏亚洲影院一区二区三区| 精品国产一区二区三区免费| 日产亚洲一区二区三区| www.999精品视频观看免费| 亚洲精品色播一区二区| 亚洲熟伦熟女新五十路熟妇 | 日韩电影免费在线观看中文字幕| 337p日本欧洲亚洲大胆色噜噜 | 99久久国产免费-99久久国产免费| 亚洲一区动漫卡通在线播放| 日本免费人成黄页网观看视频| 永久免费无码网站在线观看个| 五月天网站亚洲小说| 成人免费视频软件网站| 一级毛片免费不卡| 亚洲码一区二区三区| 国产乱子伦精品免费无码专区| a级男女仿爱免费视频| 亚洲av片不卡无码久久| 黑人大战亚洲人精品一区| 91久久成人免费| 一级中文字幕乱码免费| 亚洲精品永久www忘忧草| 一级毛片直播亚洲|