MailSender2.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- //JavaMail:MailSender2.java
- //author:ZhangHongbin
- //This program is protected by copyright laws.
- //Mail Sender program.
- import java.util.*;
- import javax.mail.internet.*;
- import javax.mail.*;
- import java.io.*;
- import javax.activation.*;
- public class MailSender2
- {
- public static void main(String[] args) throws Exception
- {
- String smtp = "smtp.21cn.com";
- String from = "bestbooks@21cn.com";
- String to = "bestbooks@21cn.com";
- String cc = "bestbooks@netease.com";
- String bcc = "zhbking@netease.com";
- //String from = "abc@21cn.com";
- //String to = "abc@21cn.com";
- //String cc = "abc@netease.com";
- //String bcc = "abc@netease.com";
- Properties props = System.getProperties();
- Session session = Session.getInstance(props,null);
- MimeMessage msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(from));
- msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
- msg.addRecipient(Message.RecipientType.CC,new InternetAddress(cc));
- msg.addRecipient(Message.RecipientType.BCC,new InternetAddress(bcc));
- msg.setSubject("Send mail 测试");
- msg.setText(" Test text 测试");
- msgInfo(msg);
- Transport transport =session.getTransport("smtp");
- transport.connect(smtp,"","");
- transport.sendMessage(msg,msg.getAllRecipients());
- transport.close();
- }
- public static void msgInfo(MimeMessage msg) throws Exception
- {
- Address[] addresses = msg.getAllRecipients();
- System.out.println("---Addresses Info---");
- for (int i=0; i <addresses.length;++i)
- {
- System.out.println(addresses[i]);
- }
- Enumeration e = msg.getAllHeaderLines();
- System.out.println("---Header Info---");
- while (e.hasMoreElements())
- {
- System.out.println(e.nextElement());
- }
- DataHandler dataHandler=msg.getDataHandler();
- BufferedReader in =new BufferedReader(
- new InputStreamReader(dataHandler.getInputStream()));
- String s;
- System.out.println("---Mail Text---");
- while(( s = in.readLine())!= null)
- {
- System.out.println(s);
- }
- System.out.println("---Some Methods---");
- System.out.println("getContentType()= "+msg.getContentType());
- System.out.println("getDisposition()= "+msg.getDisposition());
- System.out.println("getReceivedDate()= "+msg.getReceivedDate());
- System.out.println("getSentDate()= "+msg.getSentDate());
- System.out.println("getSubject()= "+msg.getSubject());
- }
- }