收到邮件且自动发送到对方提供的邮箱

终于出结果啦,下面是程序

 

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using System.Net.Mail;


namespace dgx
{
    class program
    {
        static string strHost = string.Empty;
        static string strAccount = string.Empty;
        static string strPwd = string.Empty;
        static string strFrom = string.Empty;

        static void Main()
        {
           
            ApplicationClass OutLookApp = new ApplicationClass();
            OutLookApp.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(OutLookApp_NewMailEx);
            Console.ReadLine();
        }

        static void OutLookApp_NewMailEx(string EntryIDCollection)
        {
            string y="";

            ApplicationClass d = new ApplicationClass();
            NameSpace outlookns = d.GetNamespace("MAPI");
            MAPIFolder mFolder = d.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            MailItem newmail = (MailItem)outlookns.GetItemFromID(EntryIDCollection, mFolder.StoreID);                      
          
            string[] split = newmail.Body.Split(new char[] { ',' });           
            char[] dg = split [0].ToCharArray();
            Array.Reverse(dg);
          
            foreach (char dx in dg)
            {               
              y+=dx;         
            }
           
            strHost = "mail.zhuangxiang.com";   //STMP服务器地址
            strAccount = "dengguoxing@zhuangxiang.com";       //SMTP服务帐号
            strPwd = "dengguoxing";       //SMTP服务密码
            strFrom = "dengguoxing@zhuangxiang.com";  //发送方邮件地址

            sendMail(newmail.Subject, "装箱大师授权文件",y );
           
        }


        static bool sendMail(string to, string title, string content)
        {
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            _smtpClient.Host = strHost; ;//指定SMTP服务器
            _smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//用户名和密码

            MailMessage _mailMessage = new MailMessage(strFrom, to);
            _mailMessage.Subject = title;//主题
            _mailMessage.Body = content;//内容
            _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
            _mailMessage.IsBodyHtml = true;//设置为HTML格式
            _mailMessage.Priority = MailPriority.High;//优先级

            try
            {
                _smtpClient.Send(_mailMessage);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

 


 

posted @ 2008-09-03 08:57  kin2321  阅读(306)  评论(0编辑  收藏  举报