• C# 连接 Exchange 发送邮件


    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;
                }
            }
    

    如果exchange服务器只做了证书,在发送邮件的机器上也要安装证书邮件才能发送出去。

    参考地址: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

  • 相关阅读:
    CentOS 静态IP设置&修改网卡名
    Centos 6.5 升级python到版本2.7.12
    VMware 安装Windows sever 2008 R2服务器
    RF安装
    Python的包管理工具pip
    Appium学习路—Android定位元素与操作
    MYSQL ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.210' (111) 解决方法
    MYSQL ERROR 1045 (28000): Access denied for user (using password: YES)解决方案详细说明
    CentOS下Apache默认安装路径
    Apache JMeter配置、安装
  • 原文地址:https://www.cnblogs.com/ZJ199012/p/4917815.html
Copyright © 2020-2023  润新知