Chinaunix首页 | 论坛 | 博客
  • 博客访问: 377253
  • 博文数量: 85
  • 博客积分: 1504
  • 博客等级: 上尉
  • 技术积分: 928
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-04 12:20
文章分类

全部博文(85)

文章存档

2011年(66)

2010年(19)

分类: Java

2011-01-21 15:51:51

文件:SendMailFile.zip
大小:0KB
下载:下载
  1. public class SendFile {  
  2.     public static void main(String args[]) throws Exception {  
  3.         String host = "smtp.sohu.com";  
  4.         String from = "*******";  
  5.         String to = "*******";  
  6.         String fileAttachment = "d:\\FileName";  
  7.   
  8.         Properties properties = System.getProperties();  
  9.   
  10.         Session session = Session.getInstance(properties, null);  
  11.   
  12.         MimeMessage message = new MimeMessage(session);  
  13.         message.setFrom(new InternetAddress(from));  
  14.         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
  15.         message.setSubject("SendFile");  
  16.   
  17.         MimeBodyPart messageBodyPart = new MimeBodyPart();  
  18.   
  19.         messageBodyPart.setText("Hi");  
  20.   
  21.         Multipart multipart = new MimeMultipart();  
  22.         multipart.addBodyPart(messageBodyPart);  
  23.   
  24.         messageBodyPart = new MimeBodyPart();  
  25.         DataSource source = new FileDataSource(fileAttachment);  
  26.         messageBodyPart.setDataHandler(new DataHandler(source));  
  27.         messageBodyPart.setFileName(fileAttachment);  
  28.         multipart.addBodyPart(messageBodyPart);  
  29.   
  30.         message.setContent(multipart);  
  31.   
  32.         Transport transport = session.getTransport("smtp");  
  33.         transport.connect(host, """");  
  34.   
  35.         transport.sendMessage(message, message.getAllRecipients());  
  36.   
  37.         transport.close();  
  38.     }  

from  http://shen198623.javaeye.com/blog/239967

key word: java mail html  邮件 附件
阅读(1083) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-08 14:07:01

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com