2016-11-21 54 views
0

我有发送电子邮件代码:为什么电子邮件不在这里发送?

public static bool SendEmail(
    string fromEmail, string toEmail, 
    string subject, string body, 
    bool isBodyHtml = false, bool isThrowException = false) 
{ 
    var message = new MailMessage(fromEmail, toEmail) 
    { 
     IsBodyHtml = isBodyHtml, 
     Subject = subject, 
     Body = body, 
    }; 

    using (var client = new SmtpClient()) 
    { 
     try 
     { 
      client.Send(message); 
     } 
     catch (Exception ex) 
     { 
      if (isThrowException) 
      { 
       throw new Exception(ex.ToString()); 
      } 
     } 
    } 

    return true; 
} 

web.config我:

<system.net> 
    <mailSettings> 
     <smtp> 
     <network host="smtp.bizmail.yahoo.com" port="25" enableSsl="true" [email protected]" password="@cr123" defaultCredentials="true"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 
</configuration> 

现在我没有任何异常,尚邮在另一端没有收到。 我可以通过我的凭据登录到yahoo.com。 仍然

client->credentils->domain="" 
client->ServicePoint = 'client.ServicePoint' threw an exception of type 'System.TypeInitializationException' 

它出了什么问题?

该代码运行良好...在1个月前,我没有更改一行,出了什么问题?

+3

这是你真正的用户名和密码! –

+1

没有更多 - 我只是删除它的情况下:P – BugFinder

+0

其中''实际上位于配置? –

回答

0

首先登场的所有感谢所有谁试图帮助你。

正如在我提到的问题中,创建一个新的smtp对象时出现错误。

smtp将读取web.config并创建一个关于web.config的对象。 所以我才知道问题出在web.config上。我也确信C#代码没有问题。所以我分析了web.config并找到了一些过滤器。

<filter level="TraceEventType.Error" /> 

基本上我用这个来删除Azure版本的错误信息。

这是造成Serverpoint的问题,同时创建一个smtp对象。 所以我刚刚删除了该行。它运行良好。

我不仅知道smtp标签,web.config文件中的其他标签也会影响当前的问题。

0

添加连接性和凭据,以您的代码:

NetworkCredentials Credencials = new NetworkCredential(SmtpUsername, SmtpPassword); 

client.Host = SmtpHost; 
client.Port = SmtpPort; 
client.Credentials = Credencials; 
+0

没有这个主机和端口的需要没有这个...证书也没有,但没有域名cominng,不知道是否需要。但仍然没有收到邮件。 –

相关问题