2016-10-28 103 views
0

如果我有内部的web.configSMTP客户端和配置代码访问用户名和密码

<system.net> 
    <mailSettings> 
    <smtp deliveryMethod="Network" from="&quot;testo&quot; &lt;[email protected]&gt;" > 
     <network host="mail.test.com" userName="[email protected]" password="waiff75E-" port="25"/> 
    </smtp> 
    </mailSettings> 
</system.net> 

,如果我用下面的代码从C#代码发送邮件,邮件设置

smtpclient.EnableSsl = false; 
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network; 
smtpclient.Credentials = new System.Net.NetworkCredential(username, password); 
smtpclient.Send(mail); 

我怎样才能在这里配置用户名和密码从上面的配置代码?

回答

0

请参考下面的代码片段这和证书会照顾由webconfig添加的DefaultCredentials = “假”,在webconfig

public bool SendSupportEmail(string fromMailID, string toMailID, string subject, string body) 
     { 

      bool brv = true; 

      try 
      { 
       SmtpClient smtpClient = new SmtpClient(); 
       //smtpClient.EnableSsl = true; 
       MailMessage message = new MailMessage(); 
       message.From = new MailAddress(fromMailID.ToString()); 
       message.To.Add(toMailID); 
       message.Subject = subject; 
       message.IsBodyHtml = true; 
       message.Body = body; 
       log.Info("From Addres-> " + fromMailID.ToString()); 
       log.Info("To Addres-> " + toMailID); 
       smtpClient.Send(message); 
      } 
      catch (Exception ex) 
      { 
       log.Info("From Addres-> " + fromMailID.ToString()); 
       log.Info("To Addres-> " + toMailID); 
       //log.Info("CC Addres-> " + EmailId); 
       log.Error("Error: " + ex.Message + "\nStrace: " + ex.StackTrace); 
       brv = false; 

      } 
      return brv; 
     }