c#.net sendmail中继错误

时间:2014-08-21 09:00:21

标签: c# .net email exchange-server

我最近将一些代码从VB.net重写为C#.net。 VB代码仍然发送​​邮件,但是使用C#代码我得到一个"邮箱不可用。服务器响应是:5.7.1无法中继" -error。

配置相同,我在Windows 2008 R2计算机上使用IIS 7.5进行测试,我发送的Exchange服务器正在另一台我无法访问的服务器上运行。收件人是由1&1德国托管的服务器上的mailaccount。

"老但工作" VB.NET代码:

Private Sub sendemail(mailadresse As String, betreff As String, nachricht As String)

    Dim plainnachricht As AlternateView = AlternateView.CreateAlternateViewFromString(nachricht, Nothing, "text/plain")
    Try
        Dim mail As New MailMessage() With {.From = New MailAddress("from@exchange.de", "Sender")}
        mail.To.Add(New MailAddress(mailadresse, "Recipient"))
        mail.Subject = betreff
        mail.AlternateViews.Add(plainnachricht)
        mail.IsBodyHtml = False
        Dim smtpclient As New SmtpClient("xxx.xxx.xxx.xxx", 25) With {.UseDefaultCredentials = False, .Credentials = New NetworkCredential("user", "password"), .EnableSsl = False, .DeliveryMethod = SmtpDeliveryMethod.Network}
        smtpclient.Send(mail)
    Catch ex As Exception
        Infolbl.Text = "Mail not send"
    End Try

新的C#.NET代码:

    private void sendemail(string mailadresse, string betreff, string nachricht)
    {
        AlternateView plainnachricht = AlternateView.CreateAlternateViewFromString(nachricht, null, "text/plain");
        try
        {
            MailMessage mail = new MailMessage(new MailAddress("from@exchange.de", "Sender"), new MailAddress(mailadresse, "Recipient"));
            mail.Subject = betreff;
            mail.AlternateViews.Add(plainnachricht);
            mail.IsBodyHtml = false;
            using (SmtpClient smtp = new SmtpClient("xxx.xxx.xxx.xxx", 25))
            {
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential("user", "password");
                smtp.EnableSsl = false;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Send(mail);
            }
        }
        catch (Exception ex)
        {
            Infolbl.Text = "Mail not send";
        }
    }

在Exchange服务器上使用不同的邮件地址作为收件人,邮件将被发送并到达。我还尝试过使用C#代码时出现同样错误的Gmail地址。 VB总是有效。

关于如何解决这个问题的任何想法? 感谢

1 个答案:

答案 0 :(得分:0)

问题是密码错误。在我的情况下,Exchange服务器只需要用户名,没有域名。