随笔-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 小铁匠 阅读(620) 评论(1)  编辑  收藏 所属分类: java

评论:
# re: java 发mail 2008-04-01 22:16 | 草包书生
我也来研究一下,支持中文的附件  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: