java 邮件服务

  1. package com.dtb.comment;   
  2.   
  3.   
  4.   
  5. import java.util.Properties;    
  6.   
  7. import javax.mail.*;    
  8.   
  9. import javax.mail.internet.*;   
  10.   
  11. import javax.activation.*;   
  12.   
  13. import java.util.Date;   
  14.   
  15. import com.dtb.utils.*;   
  16.   
  17. import java.io.*;   
  18.   
  19.   
  20.   
  21. public class JavaMail {   
  22.   
  23.   
  24.   
  25.     public String sendhost;   
  26.   
  27.     public String receivehost;   
  28.   
  29.     public String from;   
  30.   
  31.     public String passwd;   
  32.   
  33.     public String to;   
  34.   
  35.        
  36.   
  37.     public JavaMail(){   
  38.   
  39.         this.sendhost="发送服务器";   
  40.   
  41.         this.receivehost="接收服务器";   
  42.   
  43.         this.from="帐户";   
  44.   
  45.         this.passwd="密码";   
  46.   
  47.     }   
  48.   
  49.     /**  
  50.  
  51.      * 设置发邮件信息  
  52.  
  53.      *  
  54.  
  55.      */  
  56.   
  57.     public void setSendMail(String to){   
  58.   
  59.         if(StringUtil.isFine(to))   
  60.   
  61.             this.to=to;   
  62.   
  63.     }   
  64.   
  65.   
  66.   
  67.     /**  
  68.  
  69.      * 发送email  
  70.  
  71.      * @param from 发件人  
  72.  
  73.      * @param to 收件人  
  74.  
  75.      * @param title 标题  
  76.  
  77.      * @param mailcontent 内容  
  78.  
  79.      * @param host 发送邮件服务器(SMTP)  
  80.  
  81.      */  
  82.   
  83.     public void sendMail(String title,String mailcontent){   
  84.   
  85.   
  86.   
  87.         Properties props = System.getProperties();//得到系统属性   
  88.   
  89.            
  90.   
  91.         props.put("mail.smtp.host",sendhost);//压入发送邮件服务器(SMTP)   
  92.   
  93.            
  94.   
  95.         props.put("mail.smtp.auth""true"); //这样才能通过验证   
  96.   
  97.            
  98.   
  99.         MyAuthenticator myauth = new MyAuthenticator(from,passwd);   
  100.   
  101.            
  102.   
  103.         Session session = Session.getDefaultInstance(props, myauth); // Get session   
  104.   
  105.            
  106.   
  107.         try{//Define message   
  108.   
  109.   
  110.   
  111.             MimeMessage message = new MimeMessage(session);   
  112.   
  113.                
  114.   
  115.             message.setFrom(new InternetAddress(from));   
  116.   
  117.                
  118.   
  119.             String mailList[] = to.split(";");   
  120.   
  121.                
  122.   
  123.             for(int i=0;i<mailList.length;i++){   
  124.   
  125.                 message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailList[i]));   
  126.   
  127.             }   
  128.   
  129.                
  130.   
  131.             message.setSubject(title);//加入邮件标题   
  132.   
  133.                
  134.   
  135.             message.setText(mailcontent);//加入邮件内容   
  136.   
  137.                
  138.   
  139.             Transport.send(message);// 发送邮件   
  140.   
  141.         }   
  142.   
  143.         catch (Exception e){e.printStackTrace();}   
  144.   
  145.            
  146.   
  147.     }   
  148.   
  149.     /**  
  150.  
  151.      * 发送附件  
  152.  
  153.      * @param title 标题  
  154.  
  155.      * @param mailconent 文体内容  
  156.  
  157.      * @param filenames 本地文件  
  158.  
  159.      */  
  160.   
  161.     public void sendAppendix(String title,String mailconent,String filenames){   
  162.   
  163.            
  164.   
  165.         Properties props = System.getProperties();//得到系统属性   
  166.   
  167.            
  168.   
  169.         props.put("mail.smtp.host",sendhost);//压入发送邮件服务器(SMTP)   
  170.   
  171.            
  172.   
  173.         props.put("mail.smtp.auth""true"); //这样才能通过验证   
  174.   
  175.            
  176.   
  177.         MyAuthenticator myauth = new MyAuthenticator(from,passwd);   
  178.   
  179.            
  180.   
  181.         Session session = Session.getDefaultInstance(props, myauth);//获取默认会话   
  182.   
  183.            
  184.   
  185.         try{   
  186.   
  187.             MimeMessage msg = new MimeMessage(session);//建立邮件   
  188.   
  189.             msg.setFrom(new InternetAddress(from));   
  190.   
  191.             InternetAddress[] address = {new InternetAddress(to)};   
  192.   
  193.             msg.setRecipients(Message.RecipientType.TO, address);   
  194.   
  195.             msg.setSubject(title);   
  196.   
  197.   
  198.   
  199.             MimeBodyPart mbp1 = new MimeBodyPart();//用于存放文本内容   
  200.   
  201.             mbp1.setText(mailconent);   
  202.   
  203.                
  204.   
  205.             MimeMultipart   mimemultipart   =   new   MimeMultipart();   
  206.   
  207.             mimemultipart.addBodyPart(mbp1);//加入文字内容      
  208.   
  209.   
  210.   
  211.             if(!StringUtil.isFine(filenames))   
  212.   
  213.                 return;   
  214.   
  215.             String[] filelist=filenames.split(",");   
  216.   
  217.                
  218.   
  219.             MimeBodyPart mbp2;//用于存放附件内容   
  220.   
  221.             FileDataSource   filedatasource;      
  222.   
  223.   
  224.   
  225.             for(int i=0;i<filelist.length;i++){   
  226.   
  227.                 //逐个加入附件内容   
  228.   
  229.                 mbp2=new MimeBodyPart();      
  230.   
  231.                 filedatasource=new FileDataSource(filelist[i]);      
  232.   
  233.                 mbp2.setDataHandler(new DataHandler(filedatasource));      
  234.   
  235.                 mbp2.setFileName(filedatasource.getName());      
  236.   
  237.                 mimemultipart.addBodyPart(mbp2);      
  238.   
  239.             }   
  240.   
  241.   
  242.   
  243.             //压入附件内容   
  244.   
  245.             msg.setContent(mimemultipart);   
  246.   
  247.   
  248.   
  249.             //压入当前时间   
  250.   
  251.             msg.setSentDate(new Date());   
  252.   
  253.   
  254.   
  255.             //发送邮件   
  256.   
  257.             Transport.send(msg);   
  258.   
  259.   
  260.   
  261.         }    
  262.   
  263.         catch (MessagingException mex) {   
  264.   
  265.             mex.printStackTrace();   
  266.   
  267.         }   
  268.   
  269.     }   
  270.   
  271.   
  272.   
  273.     /**  
  274.  
  275.      * 接收邮件  
  276.  
  277.      * @param popServer  
  278.  
  279.      * @param popUser  
  280.  
  281.      * @param popPwd  
  282.  
  283.      */  
  284.   
  285.     public void receiveMail(){   
  286.   
  287.         Store store=null;   
  288.   
  289.         Folder folder=null;   
  290.   
  291.         try{   
  292.   
  293.                
  294.   
  295.             Properties props = System.getProperties();//得到系统属性   
  296.   
  297.                
  298.   
  299.             Session session = Session.getDefaultInstance(props, null);//获取默认会话   
  300.   
  301.                
  302.   
  303.             store = session.getStore("pop3");//使用POP3会话机制,连接服务器   
  304.   
  305.                
  306.   
  307.             store.connect(receivehost, from, passwd);//压入接收邮件服务器,用户,密码   
  308.   
  309.                
  310.   
  311.             folder = store.getDefaultFolder();//获取默认文件夹   
  312.   
  313.                
  314.   
  315.             if (folder == nullthrow new Exception("No default folder");   
  316.   
  317.                
  318.   
  319.             folder = folder.getFolder("INBOX");//链接收件箱   
  320.   
  321.                
  322.   
  323.             if (folder == nullthrow new Exception("No POP3 INBOX");   
  324.   
  325.                
  326.   
  327.             folder.open(Folder.READ_ONLY);//使用只读方式打开收件箱   
  328.   
  329.                
  330.   
  331.             Message[] msgs = folder.getMessages();//得到文件夹信息,获取邮件列表   
  332.   
  333.                
  334.   
  335.             for (int msgNum = 0; msgNum < msgs.length; msgNum++){   
  336.   
  337.                 printMessage(msgs[msgNum]);   
  338.   
  339.             }   
  340.   
  341.         }   
  342.   
  343.         catch (Exception ex){   
  344.   
  345.             ex.printStackTrace();   
  346.   
  347.         }   
  348.   
  349.         finally{//释放资源   
  350.   
  351.             try{   
  352.   
  353.                 if (folder!=null) folder.close(false);   
  354.   
  355.                 if (store!=null) store.close();   
  356.   
  357.             }   
  358.   
  359.             catch (Exception ex2){   
  360.   
  361.                 ex2.printStackTrace();   
  362.   
  363.             }   
  364.   
  365.         }   
  366.   
  367.     }   
  368.   
  369.     /**  
  370.  
  371.      * 打印邮件内容  
  372.  
  373.      * @param message  
  374.  
  375.      */  
  376.   
  377.     public void printMessage(Message message){   
  378.   
  379.         try{   
  380.   
  381.             String from=((InternetAddress)message.getFrom()[0]).getPersonal();//获得发送邮件地址   
  382.   
  383.                
  384.   
  385.             if (from==null) from=((InternetAddress)message.getFrom()[0]).getAddress();   
  386.   
  387.                
  388.   
  389.             System.out.println("FROM: "+from);   
  390.   
  391.                
  392.   
  393.             String subject=message.getSubject();//获取标题   
  394.   
  395.                
  396.   
  397.             System.out.println("SUBJECT: "+subject);   
  398.   
  399.                
  400.   
  401.             Part messagePart=message;//获取信息对象   
  402.   
  403.                
  404.   
  405.             Object content=messagePart.getContent();//附件   
  406.   
  407.                
  408.   
  409.             if (content instanceof Multipart){   
  410.   
  411.                 messagePart=((Multipart)content).getBodyPart(0);   
  412.   
  413.                 System.out.println("[ Multipart Message ]");   
  414.   
  415.             }   
  416.   
  417.                
  418.   
  419.             String contentType=messagePart.getContentType();//获取content类型   
  420.   
  421.                
  422.   
  423.             System.out.println("CONTENT:"+contentType);   
  424.   
  425.                
  426.   
  427.             //如果邮件内容是纯文本或者是HTML,那么打印出信息   
  428.   
  429.             if (contentType.startsWith("text/plain") || contentType.startsWith("text/html")){   
  430.   
  431.                 InputStream is = messagePart.getInputStream();   
  432.   
  433.                 BufferedReader br=new BufferedReader(new InputStreamReader(is));   
  434.   
  435.                 String thisLine="";   
  436.   
  437.                 while ((thisLine=br.readLine())!=null){   
  438.   
  439.                     System.out.println(thisLine);   
  440.   
  441.                     thisLine=br.readLine();   
  442.   
  443.                 }   
  444.   
  445.             }   
  446.   
  447.             System.out.println("-------------- END ---------------");   
  448.   
  449.         }   
  450.   
  451.         catch (Exception ex){   
  452.   
  453.             ex.printStackTrace();   
  454.   
  455.         }   
  456.   
  457.     }   
  458.   
  459.   
  460.   
  461.     public static void main(String[] args){        
  462.   
  463.         JavaMail mymail=new JavaMail();   
  464.   
  465.     }   
  466.   
  467. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值