zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    CSU 1333 Funny Car Racing
    FZU 2195 检查站点
    FZU 2193 So Hard
    ZOJ 1655 FZU 1125 Transport Goods
    zoj 2750 Idiomatic Phrases Game
    hdu 1874 畅通工程续
    hdu 2489 Minimal Ratio Tree
    hdu 3398 String
    洛谷 P2158 [SDOI2008]仪仗队 解题报告
    POJ 1958 Strange Towers of Hanoi 解题报告
  • 原文地址:https://www.cnblogs.com/ZJ199012/p/4917815.html
Copyright © 2011-2022 走看看