2

我写了一个非常基本的类来发送电子邮件。我用 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 服务器的方式处理交换服务器吗?

请指教 谢谢

4

4 回答 4

3

您是否使用了正确的端口号?

协议:SMTP/SSL

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

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

从这里撕下来:http ://www.petri.co.il/ports_used_by_exchange.htm

于 2012-05-10T18:26:31.820 回答
1

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

于 2013-04-01T20:47:49.663 回答
0

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

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

于 2012-05-10T18:32:17.357 回答
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();
    }
于 2012-05-10T19:02:23.677 回答