views:

56

answers:

1

Hey Everyone,

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);
+1  A: 

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....

Dean Harding
can it use the SMTP settings from the web.config? Im not clear on when thoose vlaues get picked up and when they might get ignored.
chopps
No, `System.Web.Mail` does not support any settings from the web.config file (only `System.Net.Mail` does). The settings I've given need to be specified on *every* message.
Dean Harding
Thats the answer I was looking for. Shoot..ok...time to try and dig up the source code. Thanks for the info!
chopps
No probs. If you're going to have to modify the code, I'd suggest switching to `System.Net.Mail` anyway. The API is similar, but at least then you can configure the authentication in the web.config.
Dean Harding