码迷,mamicode.com
首页 > Windows程序 > 详细

C# 连接 Exchange 发送邮件

时间:2015-10-28 17:17:42      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

C#连接Exchange 发送邮件代码如下

/// <summary>
        /// exchange群发邮件
        /// </summary>
        /// <param name="credentialUserName">exchange用户名 [example: test]</param>
        /// <param name="credentialUserPwd">exchange用户密码</param>
        /// <param name="fromEmail">发送人地址 [example: test@allbring.com]</param>
        /// <param name="recipientEmail">收件人地址</param>
        /// <param name="domainName">邮件域名 [example: mail.allbring.com]</param>
        /// <param name="subjectName">邮件主题</param>
        /// <param name="bodyVal">邮件内容</param>
        /// <param name="isHtml">是否为html</param>
        /// <param name="fileArray">发送上传的路径</param>
        /// <returns></returns>
        /// 开发人员:ZJ
        public static bool SendExchangeEmails(string credentialUserName, string credentialUserPwd, string fromEmail, List<string> recipientEmail, string domainName, string subjectName, string bodyVal, bool isHtml, string[] fileArray)
        {
            try
            {
                MailMessage message = new MailMessage();
                MailAddress fromAddress = new MailAddress(fromEmail);
                foreach (string item in recipientEmail)
                {
                    message.To.Add(item);
                }
                message.From = fromAddress;
                message.Subject = subjectName;
                message.Body = bodyVal;
                message.IsBodyHtml = isHtml;
                if (fileArray != null)
                {
                    for (int i = 0; i < fileArray.Length; i++)
                    {
                        //为邮件创建文件附件
                        Attachment attr = new Attachment(fileArray[i], MediaTypeNames.Application.Octet);
                        //添加邮件时间戳信息
                        ContentDisposition conDispositon = attr.ContentDisposition;
                        conDispositon.CreationDate = System.IO.File.GetCreationTime(fileArray[i]);//文件的创建日期
                        conDispositon.ModificationDate = System.IO.File.GetLastWriteTime(fileArray[i]);//文件的修改日期
                        conDispositon.ReadDate = System.IO.File.GetLastAccessTime(fileArray[i]);//文件的读取日期
                        //给邮件添加附件
                        message.Attachments.Add(attr);
                    }
                }

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Timeout = 50000;
                smtpClient.Host = domainName;
                smtpClient.Port = 25;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new System.Net.NetworkCredential(credentialUserName, credentialUserPwd);
                smtpClient.EnableSsl = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.Send(message);
                smtpClient.Dispose();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

 参考地址:https://social.technet.microsoft.com/Forums/en-US/d37c7e8a-6d42-498a-bad4-5eb0ab7e9d40/connecting-to-exchange-server-using-c-to-send-an-email?forum=exchangesvrdevelopment

http://www.systemnetmail.com/faq/2.4.aspx

C# 连接 Exchange 发送邮件

标签:

原文地址:http://www.cnblogs.com/ZJ199012/p/4917815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!