smtpclient不适用于Exchange服务器,但适用于smtp服务器

时间:2012-05-10 18:20:52

标签: c# .net smtp

我写了一个非常基本的课程来发送电子邮件。我用smtp服务器对它进行了测试,它工作正常,但是当我尝试使用我公司的交换服务器时,它给出了这个例外:

SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.7.1客户端未经过身份验证

我的代码如下:

MailMessage mailMessage = new MailMessage("From@company.com", "To@company.com", "Test Subject", "Void body");
SmtpClient smtpClient = new SmtpClient(smtpServerAddress, smtpServerPort);
NetworkCredential credentials = new NetworkCredential(AccountName,Password);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; // without this I get: The remote certificate is invalid according to the validation procedure.
smtpClient.Send(mailMessage);

我是否需要以不同于smtp服务器的方式处理Exchange服务器。

请指教 感谢

4 个答案:

答案 0 :(得分:2)

您使用的是正确的端口号吗?

  

协议:SMTP / SSL

     

•端口(TCP / UDP):465(TCP)

     

•说明:SMTP over SSL。 TCP端口465由common保留   使用SSL进行安全SMTP通信的行业惯例   协议。但是,与IMAP4,POP3,NNTP和HTTP,SMTP不同   Exchange 2000不使用单独的端口进行安全通信   (SSL),而是采用称为“带内安全子系统”   传输层安全性(TLS)。启用TLS以在Exchange上工作   2000年,您必须在Exchange 2000上安装计算机证书   服务器

从此处翻录:http://www.petri.co.il/ports_used_by_exchange.htm

答案 1 :(得分:1)

我刚遇到这个问题。在Exchange服务器上,输入Exchange系统管理器并打开对象树到服务器,然后打开协议,右键单击默认SMTP虚拟服务器并选择属性。单击访问选项卡,然后单击身份验证按钮。最后单击用户并确保您使用的用户可以访问SMTP中继。此外,如果您还没有这样做,请选择SMTP下的中继按钮,并确保运行该应用程序的计算机未被列入黑名单,或者已被授予权限。

答案 2 :(得分:0)

我发现这篇关于使用Exchange发送电子邮件的帖子。看来他正在使用Microsoft.Exchange.WebServices命名空间。我没有交换测试。只是提出想法。

http://waqarsherbhatti.posterous.com/sending-email-using-exchange-server-2010-ews

答案 3 :(得分:0)

您可能想尝试使用WebDAV发送。如果您想尝试一下,这是我的方法..

    public static void ViaWebDav(String sMailbox, String sExchange, String sTo, String sCc, String sSubject, String sBody, params String[] sAttachments)
    {
        HttpWebRequest hwrOut;
        HttpWebResponse hwrIn;

        //String strServer = "SXGM-202.xxx.com";
        //string strPassword = "123";
        //string strUserID = "u";
        //string strDomain = "fg";


        Byte[] b = null;
        Stream s = null;



        String sMailboxUrl = "http://" + sExchange + "/exchange/" + sMailbox;

        String sMailboxSend = sMailboxUrl + "/##DavMailSubmissionURI##/";

        String sMailboxTemp = sMailboxUrl + "/drafts/" + sSubject + ".eml";

        // Construct the RFC 822 formatted body of the PUT request.
        // Note: If the From: header is included here,
        // the MOVE method request will return a
        // 403 (Forbidden) status. The From address will
        // be generated by the Exchange server.
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("To: " + sTo);
        if (!sCc.IsEmpty()) sb.AppendLine("Cc: " + sCc);
        sb.AppendLine("Subject: " + sSubject);
        sb.AppendLine("Date: " + System.DateTime.Now);
        sb.AppendLine("X-Mailer: AML FIU;");
        sb.AppendLine("MIME-Version: 1.0;");
        sb.AppendLine("Content-Type: text/plain;");
        sb.AppendLine("Charset = \"iso-8859-1\"");
        sb.AppendLine("Content-Transfer-Encoding: 7bit;");
        sb.AppendLine();
        sb.AppendLine(sBody);

        // Create a new CredentialCache object and fill it with the network
        // credentials required to access the server.
        //MyCredentialCache = new CredentialCache();
        //MyCredentialCache.Add(new System.Uri(strMailboxURI),
        //    "NTLM",
        //    new NetworkCredential(strUserID, strPassword, strDomain)
        //   );

        // Create the HttpWebRequest object.
        hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp);
        hwrOut.Credentials = CredentialCache.DefaultCredentials;
        hwrOut.Method = "PUT";
        hwrOut.ContentType = "message/rfc822";

        // Encode the body using UTF-8.
        b = Encoding.UTF8.GetBytes(sb.ToString());

        hwrOut.ContentLength = b.Length;


        s = hwrOut.GetRequestStream();
        s.Write(b, 0, b.Length);
        s.Close();

        // PUT the message in the Drafts folder of the mailbox.
        hwrIn = (HttpWebResponse)hwrOut.GetResponse();


        #region //ATTACHMENTS
        //Do the PROPPATCH
        sb = new StringBuilder();
        sb.Append("<?xml version='1.0'?>");
        sb.Append("<d:propertyupdate xmlns:d='DAV:'>");
        sb.Append("<d:set>");
        sb.Append("<d:prop>");
        sb.Append("<isCollection xmlns='DAV:'>False</isCollection>");
        sb.Append("</d:prop>");
        sb.Append("</d:set>");
        sb.Append("</d:propertyupdate>");

        foreach (String sAttach in sAttachments)
        {
            hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp);
            hwrOut.Credentials = CredentialCache.DefaultCredentials;
            hwrOut.Method = "PROPPATCH";
            hwrOut.ContentType = "text/xml";
            hwrOut.Headers.Set("Translate", "f");

            b = Encoding.UTF8.GetBytes(sb.ToString());

            hwrOut.ContentLength = b.Length;

            s = hwrOut.GetRequestStream();
            s.Write(b, 0, b.Length);
            s.Close();

            hwrIn = (HttpWebResponse)hwrOut.GetResponse();


            hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp + "/" + Path.GetFileName(sAttach));
            hwrOut.Credentials = CredentialCache.DefaultCredentials;
            hwrOut.Method = "PUT";

            using (FileStream fs = new FileStream(sAttach, FileMode.Open, FileAccess.Read))
            {
                b = new Byte[fs.Length];

                fs.Read(b, 0, (Int32)fs.Length);
            }

            hwrOut.ContentLength = b.Length;

            s = hwrOut.GetRequestStream();
            s.Write(b, 0, b.Length);
            s.Close();

            hwrIn = (HttpWebResponse)hwrOut.GetResponse();
        }
        #endregion



        // Create the HttpWebRequest object.
        hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp);
        hwrOut.Credentials = CredentialCache.DefaultCredentials;
        hwrOut.Method = "MOVE";
        hwrOut.Headers.Add("Destination", sMailboxSend);

        hwrIn = (HttpWebResponse)hwrOut.GetResponse();

        // Clean up.
        hwrIn.Close();
    }