MailSender2.java
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:

Java编程

开发平台:

Java

  1. //JavaMail:MailSender2.java
  2. //author:ZhangHongbin 
  3. //This program is protected by copyright laws.
  4. //Mail Sender program.
  5. import java.util.*;
  6. import javax.mail.internet.*;
  7. import javax.mail.*;
  8. import java.io.*;
  9. import javax.activation.*; 
  10. public class MailSender2
  11. {
  12. public static void main(String[] args) throws Exception
  13. {
  14. String smtp = "smtp.21cn.com";
  15. String from = "bestbooks@21cn.com";
  16. String to = "bestbooks@21cn.com";
  17. String cc = "bestbooks@netease.com";
  18. String bcc = "zhbking@netease.com";
  19. //String from = "abc@21cn.com";
  20. //String to = "abc@21cn.com";
  21. //String cc = "abc@netease.com";
  22. //String bcc = "abc@netease.com";
  23. Properties props = System.getProperties();
  24. Session session = Session.getInstance(props,null);
  25. MimeMessage msg = new MimeMessage(session);
  26. msg.setFrom(new InternetAddress(from));
  27. msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  28. msg.addRecipient(Message.RecipientType.CC,new InternetAddress(cc));
  29. msg.addRecipient(Message.RecipientType.BCC,new InternetAddress(bcc));
  30. msg.setSubject("Send mail 测试");
  31. msg.setText(" Test text 测试");
  32. msgInfo(msg);
  33. Transport transport =session.getTransport("smtp");
  34. transport.connect(smtp,"","");
  35. transport.sendMessage(msg,msg.getAllRecipients());
  36. transport.close();
  37.     }
  38. public static void msgInfo(MimeMessage msg) throws Exception
  39. {
  40. Address[] addresses = msg.getAllRecipients();
  41. System.out.println("---Addresses Info---");
  42. for (int i=0; i <addresses.length;++i)
  43. {
  44. System.out.println(addresses[i]);
  45. }
  46. Enumeration e = msg.getAllHeaderLines();
  47. System.out.println("---Header Info---");
  48. while (e.hasMoreElements()) 
  49. {
  50. System.out.println(e.nextElement());
  51. }
  52. DataHandler dataHandler=msg.getDataHandler();
  53. BufferedReader in =new BufferedReader(
  54.                     new InputStreamReader(dataHandler.getInputStream()));
  55. String s;
  56. System.out.println("---Mail Text---");
  57. while(( s = in.readLine())!= null)
  58. {
  59. System.out.println(s);
  60. }
  61. System.out.println("---Some Methods---");
  62. System.out.println("getContentType()= "+msg.getContentType());
  63. System.out.println("getDisposition()= "+msg.getDisposition());
  64. System.out.println("getReceivedDate()= "+msg.getReceivedDate());
  65. System.out.println("getSentDate()= "+msg.getSentDate());
  66. System.out.println("getSubject()= "+msg.getSubject());
  67. }
  68. }