JAVA送受信

8214 ワード


package org.sparon.email.send;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class MailSender {
   public final static boolean sendMail(String smtphost,String mailfrom,
     boolean mailauth,String mailuser,String mailpassword,
     String mailto,String mailcc,String mailsubject,
     String mailmessage,String mailattach)
    {
    
    /*
     * smtphost: smtp.163.com   smtp.126.com       
     * mailfron:          
     * mailto:          
     * mailcc:        
     * mailauth: true     smtp    :mail.smtp.auth = true
     * mailuser:           
     * mailpassword:          
     * mailsubject:      
     * mailmessage:      
     * mailattach:        ,           
     */
     //MIME    
    MimeMessage mimeMsg; 
    //Multipart  ,    ,  ,                //MimeMessage  
    MimeMultipart mp; 
    //      
    Session session;     
//    String sep[]={";"};
    //    
    Properties props=new java.util.Properties();    
    int i;
    
    //  SMTP  
    props.put("mail.smtp.host",smtphost);
    try{
     //        
         session = Session.getDefaultInstance(props,null); 
         
         mimeMsg = new MimeMessage(session);
         mp = new MimeMultipart();
         if(mailauth)
          props.put("mail.smtp.auth","true");
         else
          props.put("mail.smtp.auth","false");
         System.out.println("Mail Host Address: "+smtphost);
    }
    catch(Exception e)
    {
          System.out.println(e.getMessage());
          return false;
     }
    try
    {
     //     
         mimeMsg.setFrom(new InternetAddress(mailfrom)); 
         System.out.println("Mail From Address: "+mailfrom);
    }
    catch(Exception e)
    {
     System.out.println(e.getMessage());
         return false; 
    }
    try{
//        java.util.Vector temp = WordsConvert.getWords(mailto,sep);
     java.util.Vector temp=new java.util.Vector(1);
     temp.add("[email protected]");
        if (temp.size()==0)
        {
         System.out.println("Mail Target Address Requried.");
         return false;
        }
        Address toaddress[] = new Address[temp.size()];
        for(i=0;i<temp.size();i++)
         toaddress[i]=InternetAddress.parse(temp.elementAt(i).toString())[0];
         //     
         mimeMsg.setRecipients(Message.RecipientType.TO,toaddress);
         System.out.println("Mail To   Address: "+mailto);
    }
    catch(Exception e)
    {
     System.out.println("Error Mail To,"+e);
        return false;
    }
    if(mailcc != null && mailcc.length()>0)
    {
     try{
//         java.util.Vector temp = WordsConvert.getWords(mailcc,sep);
      java.util.Vector temp=new java.util.Vector(1);
      temp.add("[email protected]");
      if (temp.size()>0)
         {
       Address ccaddress[] = new Address[temp.size()];
       for(i=0;i<temp.size();i++)
             ccaddress[i]=InternetAddress.parse(temp.elementAt(i).toString())[0];
       //          
       mimeMsg.setRecipients(Message.RecipientType.CC,ccaddress);
       System.out.println("Mail Cc   Address: "+mailcc);
         }
     }
     catch(Exception e)
     {
      System.out.println(e.getMessage()+"hello........... mailcc");
         return false;
      }
    }
    try
    {
      mimeMsg.setSubject(mailsubject,"GB2312");
     //        
      BodyPart bp = new MimeBodyPart();
      bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+
      mailmessage,"text/html;charset=GB2312");
      mp.addBodyPart(bp);
    }
    catch(Exception e) 
    {
     System.out.println(e.getMessage()+"dddddddddddd");
        return false;
    }
    if(mailattach != null && mailattach.length()>0)
    {
     try{
//          java.util.Vector temp = WordsConvert.getWords(mailattach,sep);
     java.util.Vector temp=new java.util.Vector(1);
     temp.add("c:\\b.txt");
     //     
          for(i=0;i<temp.size();i++)
          {
           MimeBodyPart bp = new MimeBodyPart();
           FileDataSource fileds = new FileDataSource(temp.elementAt(i).toString());
           DataHandler dh = new DataHandler(fileds);
           bp.setDisposition(Part.ATTACHMENT);
           bp.setFileName(fileds.getName());
           bp.setDataHandler(dh);
           mp.addBodyPart(bp);
          }
     }
     catch(Exception e)
     {
      System.out.println(e.getMessage());
           return false;
     }
    }
    //       
    try{
         mimeMsg.setContent(mp);
         mimeMsg.saveChanges();
         Session mailSession = Session.getInstance(props,null);
         Transport transport = mailSession.getTransport("smtp");
         transport.connect((String)props.get("mail.smtp.host"),mailuser,mailpassword);
         transport.sendMessage(mimeMsg,mimeMsg.getAllRecipients());
         System.out.println("Mail Successfully Sended!");
         transport.close();
    }
    catch(Exception e)
    {
      System.out.println(e.getMessage());;
         return false ;
    }
    return true;
 }
}

package org.sparon.email;

import javax.mail.Store;
import javax.mail.FetchProfile;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;

public class EmailReceiver {
    public static void main(String[] args) {
        try {

            Properties props = new Properties();
            Session s = Session.getInstance(props);
            Store store = s.getStore("pop3");
            store.connect("pop.163.com", "bluebit_cn", "xiaohao");

            Folder folder = store.getFolder("Inbox");
            folder.open(Folder.READ_WRITE);

            FetchProfile profile = new FetchProfile();
            profile.add(FetchProfile.Item.ENVELOPE);
            Message arraymessage[] = folder.getMessages();
            folder.fetch(arraymessage, profile);

            System.out.println("       :" + arraymessage.length);
            for (int i = 0; i < arraymessage.length; i++) {
                //     
                String from = arraymessage[i].getFrom()[0].toString();
                InternetAddress ia = new InternetAddress(from);
                System.out.println("FROM:" + ia.getPersonal() + '(' +
                                   ia.getAddress() + ')');
                //    
                System.out.println("TITLE:" + arraymessage[i].getSubject());
                //    
                System.out.println("SIZE:" + arraymessage[i].getSize());
                //      
                System.out.println("DATE:" + arraymessage[i].getSentDate());
            }

            folder.close(false);
            store.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }
}