简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM