最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用Exchange實現(xiàn)發(fā)送郵件

 更新時間:2023年10月31日 11:05:30   作者:wolfy  
最近項目中需要用到exchange的操作,所以本文就參照msdn弄了一個簡單的C#操作類,實現(xiàn)了發(fā)送郵件和拉取收件箱的功能,感興趣的小伙伴可以了解下

最近項目中需要用到exchange的操作,就參照msdn弄了一個簡單的操作類。目前先實現(xiàn)了,發(fā)送郵件和拉取收件箱的功能,其他的以后在慢慢的添加。

using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WebSite.Utilities.Mail
{
    /// <summary>
    /// exchange郵件客戶端類
    /// </summary>
    public class ExChangeMailClient
    {
        /// <summary>
        /// exchange服務(wù)對象
        /// </summary>
        private static ExchangeService _exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        /// <summary>
        /// 獲取收件箱
        /// </summary>
        /// <param name="userId">當(dāng)前用戶名</param>
        /// <param name="pwd">密碼</param>
        /// <param name="domain">域</param>
        /// <param name="pageSize">一次加載的數(shù)量</param>
        /// <param name="offset">偏移量</param>
        /// <returns></returns>
        public static List<Email> GetInbox(string userId, string pwd, string domain, int pageSize, int offset)
        {
            try
            {
                if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(domain))
                {
                    throw new ArgumentNullException("當(dāng)前用戶信息為空,無法訪問exchange服務(wù)器");
                }
                List<Email> lstEmails = new List<Email>();
                _exchangeService.Credentials = new NetworkCredential(userId, pwd, domain);
                _exchangeService.Url = new Uri(WebConfig.ExchangeServerUrl);
                ItemView view = new ItemView(pageSize, offset);
                FindItemsResults<Item> findResults = _exchangeService.FindItems(WellKnownFolderName.Inbox, SetFilter(), view);
                foreach (Item item in findResults.Items)
                {
                    item.Load(PropertySet.FirstClassProperties);
 //轉(zhuǎn)化為EmailMessage獲取 獲取郵件詳情                    var currentEmail = (Microsoft.Exchange.WebServices.Data.EmailMessage)(item);                    List<string> ccRecipientsEmailLists = new List<string>();                    List<string> bccRecipientsEmailLists = new List<string>();                    foreach (var cc in currentEmail.CcRecipients)                    {                        ccRecipientsEmailLists.Add(cc.Address);                    }                    foreach (var bcc in currentEmail.BccRecipients)                    {                        bccRecipientsEmailLists.Add(bcc.Address);                    }                    lstEmails.Add(new Email()                    {                        ExchangeItemId = item.Id.ChangeKey,                        body = item.Body.Text,                        Mail_cc = string.Join(";", ccRecipientsEmailLists.ToArray()),                        Mail_bcc = string.Join(";", bccRecipientsEmailLists.ToArray()),                        Mail_from = currentEmail.From.Address,                        IsRead = item.IsNew,                        Subject = item.Subject,                        CreateOn = item.DateTimeCreated                    });

                }
                return lstEmails;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        /// <summary>
        /// 根據(jù)用戶郵件地址返回用戶的未讀郵件數(shù)
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static int GetUnReadMailCountByUserMailAddress(string userId, string pwd, string domain, string email)
        {
            int unRead = 0;
            try
            {
                _exchangeService.Credentials = new NetworkCredential(userId, pwd, domain);
                _exchangeService.Url = new Uri(WebConfig.ExchangeServerUrl);
                _exchangeService.ImpersonatedUserId = new Microsoft.Exchange.WebServices.Data.ImpersonatedUserId(Microsoft.Exchange.WebServices.Data.ConnectingIdType.SmtpAddress, email);

                unRead = Microsoft.Exchange.WebServices.Data.Folder.Bind(_exchangeService, Microsoft.Exchange.WebServices.Data.WellKnownFolderName.Inbox).UnreadCount;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return unRead;
        }
        /// <summary>
        /// 過濾器
        /// </summary>
        /// <returns></returns>
        private static SearchFilter SetFilter()
        {
            List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
            //searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
            //searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true));
            //篩選今天的郵件
            SearchFilter start = new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeCreated, Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")));
            SearchFilter end = new SearchFilter.IsLessThanOrEqualTo(EmailMessageSchema.DateTimeCreated, Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59")));
            searchFilterCollection.Add(start);
            searchFilterCollection.Add(end);
            SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray());
            return filter;
        }
        /// <summary>
        /// 發(fā)送郵件
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static void SendMail(Email email, string userId, string pwd, string domain)
        {
            try
            {
                _exchangeService.Credentials = new NetworkCredential(userId, pwd, domain);
                _exchangeService.Url = new Uri(WebConfig.ExchangeServerUrl);
                //發(fā)送人
                Mailbox mail = new Mailbox(email.Mail_from);
                //郵件內(nèi)容
                EmailMessage message = new EmailMessage(_exchangeService);
                string[] strTos = email.Mail_to.Split(';');
                //接收人
                foreach (string item in strTos)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        message.ToRecipients.Add(item);
                    }
                }
                //抄送人
                foreach (string item in email.Mail_cc.Split(';'))
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        message.CcRecipients.Add(item);
                    }

                }
                //郵件標(biāo)題
                message.Subject = email.Subject;
                //郵件內(nèi)容
                message.Body = new MessageBody(email.body);
                //發(fā)送并且保存
                message.SendAndSaveCopy();

            }
            catch (Exception ex)
            {
                throw new Exception("發(fā)送郵件出錯," + ex.Message + "\r\n" + ex.StackTrace);
            }
        }
    }
}

總結(jié)

1. 使用Exchange協(xié)議發(fā)送郵件可以無需設(shè)置主機,如需設(shè)置可通過service.url傳入IP6地址(_exchangeService.Url = new Uri(WebConfig.ExchangeServerUrl););

2. Exchange Server doesn't support the requested version 錯誤;

message調(diào)用save等操作或發(fā)送郵件方法,報錯 Exchange Server doesn't support the requested version , 需查看提供具體Exchange Server版本(ExchangeService _exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);

以上就是C#使用Exchange實現(xiàn)發(fā)送郵件的詳細(xì)內(nèi)容,更多關(guān)于C#發(fā)送郵件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#中Params的用法

    C#中Params的用法

    這篇文章主要介紹了C#中Params的用法,以實例的形式展示了采用Params在不知道參數(shù)的數(shù)量時的應(yīng)用技巧,非常具有實用價值,需要的朋友可以參考下
    2014-11-11
  • C#定時器和隨機數(shù)

    C#定時器和隨機數(shù)

    在前一篇中我們介紹了鍵盤和鼠標(biāo)事件,其實還有一個非常常用的事件,就是定時器事件,如果要對程序?qū)崿F(xiàn)時間上的控制,那么就要使用到定時器。而隨機數(shù)也是很常用的一個功能,在我們要想產(chǎn)生一個隨機的結(jié)果時就要使用到隨機數(shù)。本文我們就來簡單介紹一下定時器和隨機數(shù)。
    2015-06-06
  • c#基于Win32Api實現(xiàn)返回Windows桌面功能

    c#基于Win32Api實現(xiàn)返回Windows桌面功能

    本文分享下回到桌面功能的實現(xiàn)方法,效果與快捷鍵(Win+D)相同。有此需求的朋友可以參考下
    2021-05-05
  • C#實現(xiàn)訪問Web API Url提交數(shù)據(jù)并獲取處理結(jié)果

    C#實現(xiàn)訪問Web API Url提交數(shù)據(jù)并獲取處理結(jié)果

    Web API  是 Web 服務(wù)器和 Web 瀏覽器之間的應(yīng)用程序處理接口,我們常見的模式是訪問 Web API Url 地址,并獲取 Json 、XML或其它指定格式的處理結(jié)果, 本文我們介紹了使用C#實現(xiàn)訪問Web API Url提交數(shù)據(jù)并獲取處理結(jié)果,需要的朋友可以參考下
    2024-05-05
  • 詳解C#之委托

    詳解C#之委托

    這篇文章主要介紹了C#委托的含義以及用法,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí)
    2020-06-06
  • C#實現(xiàn)Winform序在系統(tǒng)托盤顯示圖標(biāo)和開機自啟動

    C#實現(xiàn)Winform序在系統(tǒng)托盤顯示圖標(biāo)和開機自啟動

    這篇文章主要為大家詳細(xì)介紹了C#如何實現(xiàn)Winform序在系統(tǒng)托盤顯示圖標(biāo)和開機自啟動功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
    2025-01-01
  • C#使用OleDb操作Excel和數(shù)據(jù)庫的策略

    C#使用OleDb操作Excel和數(shù)據(jù)庫的策略

    在C#編程中,使用OleDb可以方便地實現(xiàn)對Excel文件和數(shù)據(jù)庫的操作,本文探討了在C#中使用OleDb技術(shù)操作Excel和數(shù)據(jù)庫的策略,文章詳述了OleDb的定義、配置環(huán)境的步驟,并通過實際代碼示例演示了如何高效讀寫Excel文件和交互數(shù)據(jù)庫,需要的朋友可以參考下
    2024-05-05
  • C# 正則表達式常用的符號和模式解析(最新推薦)

    C# 正則表達式常用的符號和模式解析(最新推薦)

    這篇文章主要介紹了C# 正則表達式常用的符號和模式解析,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • WPF拖動DataGrid滾動條時內(nèi)容混亂的解決方法

    WPF拖動DataGrid滾動條時內(nèi)容混亂的解決方法

    這篇文章主要介紹了WPF拖動DataGrid滾動條時內(nèi)容混亂的解決方法
    2016-10-10
  • C#訪問命令行的兩種方法

    C#訪問命令行的兩種方法

    這篇文章主要介紹了C#訪問命令行的兩種方法,實例分析了C#操作命令行的兩種常用技巧,需要的朋友可以參考下
    2015-06-06

最新評論

芜湖县| 囊谦县| 田林县| 萝北县| 历史| 安陆市| 广南县| 邓州市| 景谷| 房产| 巩留县| 丹巴县| 浦县| 奉节县| 四川省| 湘潭市| 汶上县| 泌阳县| 乌拉特中旗| 鹤峰县| 灵璧县| 类乌齐县| 佳木斯市| 盱眙县| 台南市| 望城县| 句容市| 澜沧| 屯门区| 新巴尔虎左旗| 得荣县| 池州市| 南江县| 济阳县| 麟游县| 察隅县| 卢氏县| 安宁市| 凌云县| 合阳县| 津市市|