简体   繁体   English

C#SMTP虚拟服务器不发送邮件

[英]C# SMTP virtual server doesn't send mail

C# SMTP virtual server doesn't send mail. C#SMTP虚拟服务器不发送邮件。 Here's the code: 这是代码:

public static void SendEmail(string _FromEmail, string _ToEmail, string _Subject, string _EmailBody)
{

    // setup email header . 
    SmtpMail.SmtpServer = "127.0.0.1";
    MailMessage _MailMessage = new MailMessage();

    _MailMessage.From = _FromEmail;
    _MailMessage.To = _ToEmail;
    _MailMessage.Subject = _Subject;
    _MailMessage.Body = _EmailBody;

    try
    {
        SmtpMail.Send(_MailMessage);
    }
    catch (Exception ex)
    {
        if (ex.InnerException != null)
        {
            String str = ex.InnerException.ToString();

        }
    }

It is an Interop exception because that .NET code is relying on Interop(erability) services, non-managed stuff. 这是一个Interop例外,因为.NET代码依赖于Interop(erability)服务和非托管内容。 The message however is pretty clear, the server is unable to relay because it has been configured to reject relaying of emails. 但是该消息非常清楚,服务器无法中继,因为已将其配置为拒绝中继电子邮件。

Many years ago that was not a problem and you could virtually use any public SMTP server to send emails. 许多年前,这不是问题,您几乎可以使用任何公共SMTP服务器发送电子邮件。 With the advent of SPAM the whole game changed, now in most servers relaying is disabled and your mail send request is rejected. 随着SPAM的到来,整个游戏发生了变化,现在在大多数服务器中,中继功能已被禁用,您的邮件发送请求也被拒绝。

What you will need here so that it does not reject your mail, is to authenticate your request (account/user and password on that server). 您需要的是对请求进行身份验证(该服务器上的帐户/用户和密码),以使其不会拒绝您的邮件。 It has to be a valid username & password combination known to that SMTP server. 它必须是该SMTP服务器已知的有效用户名和密码组合。 You do that by setting those (out of my head sorry) in the .Credentials property. 您可以通过在.Credentials属性中设置这些设置(抱歉)。 See also the UseDefaultCredentials property, if you set Credentials you need to make sure UseDefaultCredentials is false. 另请参阅UseDefaultCredentials属性,如果您设置凭据,则需要确保UseDefaultCredentials为false。

If the error is this: 如果错误是这样的:

550 5.7.1 Unable to relay for ragaei.mahmoud@invensys.com 550 5.7.1无法中继至ragaei.mahmoud@invensys.com

Then your SMTP server cannot send to that email address. 然后,您的SMTP服务器无法发送到该电子邮件地址。 Use a different SMTP server or configure it to relay out to the internet. 使用其他SMTP服务器或将其配置为中继到Internet。 The local one will probably only send to addresses on your own domain. 本地地址可能只会发送到您自己域中的地址。 If this is an SMTP server configuration question, you may have more luck asking on Superuser. 如果这是SMTP服务器配置问题,那么您可能对超级用户有更多的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM