最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
use javamail to send attachments(转)
时间:2008-01-12 编辑:简简单单 来源:一聚教程网
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));
-
上一个: JavaMail操作的总结(3)
相关文章
- PS制作旋转八卦图教程 07-27
- java中如何使用BufferedImage判断图像通道顺序并转RGB/BGR 07-25
- PPT如何制作一段月球围绕地球转的动画 07-06
- 微软裁员为了什么 补充新鲜血液向云计算服务公司转型 07-05
- 天龙八部手游门派转职详解 转职天龙注意事项 07-03
- 龙之谷手游刺客转职加点如何选 06-27