如果我在web.config中有邮件设置
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from=""testo" <admin@test.com>" >
<network host="mail.test.com" userName="admin@test.com" 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 :(得分:0)
尝试将属性defaultCredentials="false"
添加到network
元素,使其看起来像:
<network host="mail.test.com" port="25"
defaultCredentials="false"
userName="admin@test.com" password="waiff75E-" />
SmtpClient
对象将使用您在配置文件中指定的任何参数自动初始化(请参阅Remarks section in the MSDN article for the SmtpClient
constructor)。
答案 1 :(得分:0)
请参考下面的代码片段,凭据将由webconfig处理。添加defaultCredentials =&#34; false&#34;在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;
}