Im working on a legacy app that has this logic in its code that I cant modify unfortunately. I have the proper settings in the web.config and was wondering if i list the proper SMTP server would the web.config s开发者_如何学运维ettings take care of the credentials?
If not what options do i have for sending email out with this code?
string str13 = "";
str13 = StringType.FromObject(HttpContext.Current.Application["MailServer"]);
if (str13.Length > 2)
{
SmtpMail.SmtpServer = str13;
}
else
{
SmtpMail.SmtpServer = "localhost";
}
SmtpMail.Send(message);
System.Web.Mail
does not expose any settings for specifying credentials, unfortunately. It is possible to send authenticated emails, though, because System.Web.Mail
is built on top of CDOSYS. Here's a KB article which describes how to do it, but you basically have to modify some properties on the message itself:
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 = "someone@domain.com";
msg.From = "me@domain.com";
msg.Subject = "Subject";
msg.Body = "Message";
SmtpMail.Send(msg);
Whether that works for your situation or not, I'm not sure....
精彩评论