硬件学院 | 网络学院 | 游戏秘籍 | 求职技巧 | 企业管理 | 软件资讯 | IT导购 | 软件下载 | 源码下载
软件学院 | 安全资讯 | 图形图象 | 网络营销 | 电子商务 | 硬件资讯 | IT生活 | 教程下载 | 电影娱乐
网站首页    个人求职    单位招聘    高校联盟    猎头服务    培训服务    资讯中心    IT论坛
让每一个热爱IT的人都找到一份满意的工作!
文章搜索:
 您的位置首页->-> 软件学院-> JSP技术-> use javamail to send attachments(转)
use javamail to send attachments(转)
作者:中国资讯网 来源:zixuen.com 加入时间:2005-5-12 www.cnitrc.com
Attachments are resources associated with a mail message, usually kept outside of the message like a text file, spreadsheet, or image. As with common mail programs like Eudora and pine, you can attach resources to your mail message with the JavaMail API and get those attachments when you receive the message.

Sending attachments:
Sending attachments is quite like forwarding messages. You build up the parts to make the complete message. After the first part, your message text, you add other parts where the DataHandler for each is your attachment, instead of the shared handler in the case of a forwarded message. If you are reading the attachment from a file, your attachment data source is a FileDataSource. Reading from a URL, it is a URLDataSource. Once you have your DataSource, just pass it on to the DataHandler constructor, before finally attaching it to the BodyPart with setDataHandler(). Assuming you want to retain the original filename for the attachment, the last thing to do is to set the filename associated with the attachment with the setFileName() method of BodyPart. All this is shown here:


  // Define message
  Message message = new MimeMessage(session);
  message.setFrom(new InternetAddress(from));
  message.addRecipient(Message.RecipientType.TO,
    new InternetAddress(to));
  message.setSubject("Hello JavaMail Attachment");
  
  // Create the message part
  BodyPart messageBodyPart = new MimeBodyPart();
  
  // Fill the message
  messageBodyPart.setText("Pardon Ideas");
  
  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(messageBodyPart);
  
  // Part two is attachment
  messageBodyPart = new MimeBodyPart();
  DataSource source = new FileDataSource(filename);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(filename);
  multipart.addBodyPart(messageBodyPart);
  
  // Put parts in message
  message.setContent(multipart);
  
  // Send the message
  Transport.send(message);
  
When including attachments with your messages, if your program is a servlet, your users must upload the attachment besides telling you where to send the message. Uploading each file can be handled with a form encoding type of multipart/form-data.


<FORM ENCTYPE="multipart/form-data"
    method=post action="/myservlet">
  <INPUT TYPE="file" NAME="thefile">
  <INPUT TYPE="submit" VALUE="Upload">
</FORM>

Note: Message size is limited by your SMTP server, not the JavaMail API. If you run into problems, consider increasing the Java heap size by setting the ms and mx parameters.

Exercise:
Exercise 5. How to send attachments

Getting attachments:
Getting attachments out of your messages is a little more involved then sending them because MIME has no simple notion of attachments. The content of your message is a Multipart object when it has attachments. You then need to process each Part, to get the main content and the attachment(s). Parts marked with a disposition of Part.ATTACHMENT from part.getDisposition() are clearly attachments. However, attachments can also come across with no disposition (and a non-text MIME type) or a disposition of Part.INLINE. When the disposition is either Part.ATTACHMENT or Part.INLINE, you can save off the content for that message part. Just get the original filename with getFileName() and the input stream with getInputStream().


Multipart mp = (Multipart)message.getContent();

for (int i=0, n=multipart.getCount(); i<n; i++) {
  Part part = multipart.getBodyPart(i));
  
  String disposition = part.getDisposition();
    
  if ((disposition != null) &&
       ((disposition.equals(Part.ATTACHMENT) ||
          (disposition.equals(Part.INLINE))) {
    saveFile(part.getFileName(), part.getInputStream());
  }
}

The saveFile() method just creates a File from the filename, reads the bytes from the input stream, and writes them off to the file. In case the file already exists, a number is added to the end of the filename until one is found that doesn't exist.


// from saveFile()
File file = new File(filename);
for (int i=0; file.exists(); i++) {
  file = new File(filename+i);
}

The code above covers the simplest case where message parts are flagged appropriately. To cover all cases, handle when the disposition is null and get the MIME type of the part to handle accordingly.


if (disposition == null) {
  // Check if plain
  MimeBodyPart mbp = (MimeBodyPart)part;
  if (mbp.isMimeType("text/plain")) {
     // Handle plain
  } else {
     // Special non-attachment cases here of image/gif, text/html, ...
  }
  ...
}

  相关文章:
JSP技术
ASP技术
PHP技术
JSP技术
.NET技术
服务器技术
数据库技术
其它类
工具软件
办公软件
本类阅读TOP10
 
关于我们   |   服务声明   |   使用帮助   |   广告合作   |   网站地图   |   友情链接   |   加盟合作   |   联系我们
Copyright © 2006 cnitrc.com Inc. All Rights Reserved. 浙ICP备05074295号
中国IT人才网 版权所有 网络实名:中国IT人才
未经书面授权严禁转载和复制本站的任何招聘信息和文章