Todd
Todd

Reputation: 1822

ASP.NET SMTP + web.config

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 settings 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);

Upvotes: 2

Views: 1225

Answers (1)

Dean Harding
Dean Harding

Reputation: 72638

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 = "[email protected]"; 
msg.From = "[email protected]";
msg.Subject = "Subject";
msg.Body = "Message";
SmtpMail.Send(msg);

Whether that works for your situation or not, I'm not sure....

Upvotes: 1

Related Questions