2010-08-07 28 views
2

我正在使用遺留應用程序,該應用程序的代碼中包含此邏輯,我不能修改它。我在web.config中有適當的設置,並且想知道如果我列出正確的SMTP服務器,web.config設置是否會處理憑據?ASP.NET SMTP + web.config

如果不是我用這段代碼發送電子郵件有什麼選擇?

string str13 = ""; 
    str13 = StringType.FromObject(HttpContext.Current.Application["MailServer"]); 
    if (str13.Length > 2) 
    { 
     SmtpMail.SmtpServer = str13; 
    } 
    else 
    { 
     SmtpMail.SmtpServer = "localhost"; 
    } 
    SmtpMail.Send(message); 
+0

'str13' ......哎喲。 – wsanville 2010-08-07 01:52:56

+0

我使用了Reflector來查看源代碼。 – Todd 2010-08-07 01:59:00

+1

我想看看System.Net.Mail。 – Nate 2010-08-07 14:15:44

回答

1

System.Web.Mail不幸的是,不公開指定憑證的任何設置。它儘管可能發送經過身份驗證的電子郵件,因爲System.Web.Mail構建於CDOSYS之上。 Here's a KB article which describes how to do it,但你基本上要修改消息本身的一些特性:對於你的環境條件或不

var msg = new MailMessage(); 
if (userName.Length > 0) 
{ 
    string ns = "http://schemas.microsoft.com/cdo/configuration/"; 
    msg.Fields.Add(ns + "smtpserver", smtpServer); 
    msg.Fields.Add(ns + "smtpserverport", 25) ; 
    msg.Fields.Add(ns + "sendusing", cdoSendUsingPort) ; 
    msg.Fields.Add(ns + "smtpauthenticate", cdoBasic); 
    msg.Fields.Add(ns + "sendusername", userName); 
    msg.Fields.Add(ns + "sendpassword", password); 
} 
msg.To = "[email protected]"; 
msg.From = "[email protected]"; 
msg.Subject = "Subject"; 
msg.Body = "Message"; 
SmtpMail.Send(msg); 

不管,我不知道....

+0

它可以使用web.config中的SMTP設置嗎?我不清楚什麼時候拿到了一些錢,以及什麼時候可能會被忽視。 – Todd 2010-08-07 03:56:57

+0

不,System.Web.Mail不支持web.config文件中的任何設置(只有'System.Net.Mail')。我給出的設置需要在* every *消息中指定。 – 2010-08-07 06:20:58

+0

那就是我正在尋找的答案。拍攝...時間來嘗試挖掘源代碼。謝謝(你的)信息! – Todd 2010-08-08 23:29:26