Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 电子邮件不发送,没有错误?_C#_Asp.net_Email - Fatal编程技术网

C# 电子邮件不发送,没有错误?

C# 电子邮件不发送,没有错误?,c#,asp.net,email,C#,Asp.net,Email,我正在使用以下简单代码尝试发送电子邮件: try { MailMessage newMail = new MailMessage(); newMail.To.Add("me@mydomain.com"); newMail.Subject = "test"; newMail.Body = "test"; newMail.From = new MailAddress("from@mydomain.com"

我正在使用以下简单代码尝试发送电子邮件:

    try
    {
        MailMessage newMail = new MailMessage();
        newMail.To.Add("me@mydomain.com");
        newMail.Subject = "test";
        newMail.Body = "test";
        newMail.From = new MailAddress("from@mydomain.com", "from");
        newMail.IsBodyHtml = true;

        SmtpClient SmtpSender = new SmtpClient();
        SmtpSender.Send(newMail);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
以及我的web.config文件中的以下代码:

<system.net>
<mailSettings>
  <smtp>
    <network host="localhost" userName="me@mydomain.com" password="password" defaultCredentials="true"/>
  </smtp>
</mailSettings>

我运行页面,页面完成时没有错误,但没有发送电子邮件。我做错了什么


编辑:我已经根据我看过的许多示例更新了代码,现在正在使用一个名为hMailServer的本地SMTP客户端。我已经在ASP.Net之外测试了我的hMailServer,它确实发送电子邮件。但是,使用上面的新代码仍然不起作用,也不会引发任何异常。

如果您在其他地方托管电子邮件表单,您可能需要“医生”帮助您发送表单

例如,GoDoad会阻止某些被认为是高垃圾邮件来源的域名(如Gmail、Hotmail和雅虎邮件)。 若要解决此问题,请从源于您的域的电子邮件地址发送邮件,最好是假地址,这样您就不会收到大量垃圾邮件:

private const string mailer = "no-reply@yourDomain.com";
继续并在您的网站管理员地址中编码,以便更好地衡量:

private const string webmaster = "yourEmail@yourDomain.com";
在构建电子邮件时,应在回复到字段中指定联系人的电子邮件地址(从下面代码中的
txtEmail.Text
):

lblError.Text=null;
使用(var email=new System.Net.Mail.MailMessage(
新邮寄地址(邮寄者),新邮寄地址(网站管理员)){
email.ReplyTo=txtEmail.Text;//这是从您的联系人表单中获取的
串体=
"" +
txtSubject.Text+
"";
email.Subject=“网站联系人”;
email.Body=strHtmlBody;
email.IsBodyHtml=true;
var server=new-SmtpClient(“relay-hosting.secureserver.net”);
试一试{
发送(电子邮件);
}捕获(异常错误){
lblError.Text=错误消息;
}
}

您确定没有发送电子邮件,还是说电子邮件没有到达您的收件箱?电子邮件可能会出现很多问题,其中很少有问题与代码有关。SMTP服务器的响应是什么?如果将其指向您控制的调试服务器(如smtp4dev),会发生什么?特别是,看看你的Gmail发送框。我刚检查过,发送框中没有电子邮件。不幸的是,我没有控制服务器进行调试。你是否在防火墙后面,不允许该端口出去?我也尝试过端口25,我怀疑它被阻止,因为我们有一个smtp服务器。我尝试过使用这在我的代码中,但结果相同;没有错误,没有电子邮件。我在回来的路上写了一篇文章:谢谢,但我已经更新了代码。我使用内部smtp服务器尝试了它,但问题仍然存在。
lblError.Text = null;
using (var email = new System.Net.Mail.MailMessage(
  new MailAddress(mailer), new MailAddress(webmaster)) {
  email.ReplyTo = txtEmail.Text; // this comes off of your contact form
  string strHtmlBody =
    "<html><body style=\"background-color:#cccccc\">" +
    txtSubject.Text +
    "</body></html>";
  email.Subject = "Website Contact";
  email.Body = strHtmlBody;
  email.IsBodyHtml = true;
  var server = new SmtpClient("relay-hosting.secureserver.net");
  try {
    server.Send(email);
  } catch (Exception err) {
    lblError.Text = err.Message;
  }
}