Asp.net SmtpClient赢得';从web.config充气时无法进行身份验证

Asp.net SmtpClient赢得';从web.config充气时无法进行身份验证,asp.net,smtpclient,Asp.net,Smtpclient,使用system.net/mail web.config设置配置我的SmtpClient时,它无法发送电子邮件,Base64编码和身份验证问题最能说明“协议错误”: 示例: 使用以下配置 <system.net> <mailSettings> <smtp from="email@server.com"> <network host="servermail.outsourced.com"

使用system.net/mail web.config设置配置我的SmtpClient时,它无法发送电子邮件,Base64编码和身份验证问题最能说明“协议错误”:

示例:
使用以下配置

<system.net>
    <mailSettings>
        <smtp from="email@server.com">
            <network host="servermail.outsourced.com"
                     port="2525" 
                     defaultCredentials="false" 
                     userName="username" 
                     password="password"/>
        </smtp>
    </mailSettings>
</system.net>
生成错误消息:

System.Net.Mail.SmtpException: The server committed a protocol violation The server response was: UGFzc3dvcmQ6
   at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException
& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
但是,在下面的代码中,我手动设置了所有属性,代码毫无例外地运行,并且电子邮件已发送

var tmp2 = new SmtpClient("servermail.outsourced.com", 2525);
tmp2.Credentials = new NetworkCredential("username", "password");
tmp2.UseDefaultCredentials = false;

MailMessage msg = new MailMessage();
msg.Subject = "test";
msg.From = new MailAddress("me@server.com");
msg.To.Add(new MailAddress("me@server.com"));
msg.Body = "test";
tmp2.Send(msg);

尝试设置deliveryMethod属性以强制配置使用网络配置

<system.net>
    <mailSettings>
        <smtp deliveryMethod="network" from="email@server.com">
            <network host="servermail.outsourced.com"
                     port="2525" 
                     defaultCredentials="false" 
                     userName="username" 
                     password="password"/>
        </smtp>
    </mailSettings>
</system.net>

我在LINQPad中针对我的hMailServer邮件服务器尝试了您的配置设置,效果非常好。因此,我猜想您正在与之通信的邮件服务器正在以一种意想不到的方式与客户端握手。当我进行测试时,我从服务器捕获了SMTP日志,下面是它的样子(当然是经过消毒的):

LINQPad查询如下所示:

SmtpClient mailer = new SmtpClient();

Guid g = Guid.NewGuid();
g.Dump("Message GUID");

MailMessage msg = new MailMessage();
msg.Subject = "Test:" + g;
msg.To.Add(new MailAddress("happyuser@mailserv.er"));
msg.Body = "I HAS A BODY";
mailer.Send(msg);
将此添加到日志中

请说明使用了哪些AuthenticationModule(您将在network.log中找到它们)。 在我的盒子上,SmtpLoginAuthenticationModule#是常量,但也有其他可能

  <system.diagnostics>
    <sources>
      <source name="System.Net" tracemode="includehex">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Sockets">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Cache">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>

    </sources>
    <switches>
      <add name="System.Net" value="Verbose"/>
      <add name="System.Net.Sockets" value="Verbose"/>
      <add name="System.Net.Cache" value="Verbose"/>
    </switches>
    <sharedListeners>
      <add name="System.Net"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="network.log"
      />
    </sharedListeners>
    <trace autoflush="true"/>

  </system.diagnostics>

伙计们,这段代码还可以,我尝试在本地机器上使用devsmtp,效果很好

受保护的子按钮1\u单击(ByVal sender作为对象,ByVal e作为事件参数)处理按钮1。单击

    Using tmp = New SmtpClient()
        Dim msg As New MailMessage() With {.Subject = "test", .From = New MailAddress("youremail@domain.com")}
        msg.[To].Add(New MailAddress("youremail@domain.com"))
        msg.Body = "test"
        tmp.Send(msg)
    End Using

End Sub
请从下载本地计算机的devsmtp


谢谢你的推荐。没有效果。在使用web.config存储凭据时,是否已尝试在正确填充SMTP服务器的属性后调试和检查这些属性?可能里面有一个不正确的值。如果你解码UGFzc3dvcmQ6,你会得到“密码:”所以你的身份验证显然有问题。我自己测试过,没有任何问题。可能重复:这是我最初的怀疑,除了相同的凭据在通过web配置进行设置时会产生错误,并且在使用代码进行设置时会起作用。正在使用的邮件服务器是什么?您是否能够诊断您的问题?
SmtpClient mailer = new SmtpClient();

Guid g = Guid.NewGuid();
g.Dump("Message GUID");

MailMessage msg = new MailMessage();
msg.Subject = "Test:" + g;
msg.To.Add(new MailAddress("happyuser@mailserv.er"));
msg.Body = "I HAS A BODY";
mailer.Send(msg);
  <system.diagnostics>
    <sources>
      <source name="System.Net" tracemode="includehex">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Sockets">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Cache">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>

    </sources>
    <switches>
      <add name="System.Net" value="Verbose"/>
      <add name="System.Net.Sockets" value="Verbose"/>
      <add name="System.Net.Cache" value="Verbose"/>
    </switches>
    <sharedListeners>
      <add name="System.Net"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="network.log"
      />
    </sharedListeners>
    <trace autoflush="true"/>

  </system.diagnostics>
    Using tmp = New SmtpClient()
        Dim msg As New MailMessage() With {.Subject = "test", .From = New MailAddress("youremail@domain.com")}
        msg.[To].Add(New MailAddress("youremail@domain.com"))
        msg.Body = "test"
        tmp.Send(msg)
    End Using

End Sub