Recently, I need to use exchange operations in the project, so I refer to msdn to create a simple operation class. Currently, the functions of sending emails and pulling inboxes have been implemented first, and the others will be added slowly in the future.
using ; using System; using ; using ; using ; using ; using ; namespace { /// <summary> /// Exchange mail client class /// </summary> public class ExChangeMailClient { /// <summary> /// Exchange service object /// </summary> private static ExchangeService _exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1); /// <summary> /// Get Inbox /// </summary> /// <param name="userId">Current username</param> /// <param name="pwd">Password</param> /// <param name="domain">Domain</param> /// <param name="pageSize">number of loads at one time</param> /// <param name="offset">offset</param> /// <returns></returns> public static List<Email> GetInbox(string userId, string pwd, string domain, int pageSize, int offset) { try { if ((userId) || (pwd) || (domain)) { throw new ArgumentNullException("The current user information is empty, the exchange server cannot be accessed"); } List<Email> lstEmails = new List<Email>(); _exchangeService.Credentials = new NetworkCredential(userId, pwd, domain); _exchangeService.Url = new Uri(); ItemView view = new ItemView(pageSize, offset); FindItemsResults<Item> findResults = _exchangeService.FindItems(, SetFilter(), view); foreach (Item item in ) { (); //Convert to EmailMessage to get email details var currentEmail = ()(item); List<string> ccRecipientsEmailLists = new List<string>(); List<string> bccRecipientsEmailLists = new List<string>(); foreach (var cc in ) { (); } foreach (var bcc in ) { (); } (new Email() { ExchangeItemId = , body = , Mail_cc = (";", ()), Mail_bcc = (";", ()), Mail_from = , IsRead = , Subject = , CreateOn = }); } return lstEmails; } catch (Exception ex) { throw ex; } } /// <summary> /// Return the number of unread emails of the user based on the user's email address /// </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(); _exchangeService.ImpersonatedUserId = new (, email); unRead = (_exchangeService, ).UnreadCount; } catch (Exception ex) { throw ex; } return unRead; } /// <summary> /// Filter /// </summary> /// <returns></returns> private static SearchFilter SetFilter() { List<SearchFilter> searchFilterCollection = new List<SearchFilter>(); //(new (, false)); //(new (, true)); //Filter today's email SearchFilter start = new (, (("yyyy-MM-dd 00:00:00"))); SearchFilter end = new (, (("yyyy-MM-dd 23:59:59"))); (start); (end); SearchFilter filter = new (, ()); return filter; } /// <summary> /// Send email /// </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(); //Send by Mailbox mail = new Mailbox(email.Mail_from); //Email content EmailMessage message = new EmailMessage(_exchangeService); string[] strTos = email.Mail_to.Split(';'); //Recipient foreach (string item in strTos) { if (!(item)) { (item); } } //Check the person foreach (string item in email.Mail_cc.Split(';')) { if (!(item)) { (item); } } //Email Title = ; //Email content = new MessageBody(); //Send and save (); } catch (Exception ex) { throw new Exception("There was an error sending the email," + + "\r\n" + ); } } } }
Summarize
1. Use the Exchange protocol to send emails without setting up the host. If you need to set up the IP6 address you can pass in (_exchangeService.Url = new Uri(););
2. Exchange Server doesn't support the requested version error;
Message calls save and other operations or sending email methods, and reports an error. Exchange Server doesn't support the requested version. You need to check the specific Exchange Server version provided (ExchangeService _exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
The above is the detailed content of C# using Exchange to send emails. For more information about sending emails in C#, please pay attention to my other related articles!