简体   繁体   中英

smtp mailSettings from

Is it possible to use the "from" in the config file as the MailMessage.From when creating an email in .net. Having trouble accessing it. Whenever I try referring ConfigurationManager I get a does not exist error but I am referencing them, see below.

<mailSettings>
  <smtp from="test@test.org" deliveryMethod="Network">
    <network host="smtpsend" defaultCredentials="true"/>
  </smtp>
</mailSettings>

using System.Net.Mail;
using System.Net;
using System.Configuration;
using System.Net.Configuration;

            MailMessage msg = new MailMessage();
            msg.From = ***~~**
            msg.Subject = string.Format(subject);
            msg.IsBodyHtml = true;
            msg.Body = body;
            msg.Priority = MailPriority.Normal;
            msg.To.Add("me");

            SmtpClient client = new SmtpClient();
            client.Send(msg);

Good afternoon have you added an actual reference to the system.configuration dll library? You can check by looking in the references folder of your project in solution explorer. If it is not there, right click your project and choose the add references option. Then click on the .net tab an search for system.configuration. That should take care of your reference issue. Additionally, by specifying the from address in the config file I believe that you do not have to reset it again when actually making a request to send an email.

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