1

I want to get the SMTP from address from my Web.config, I am not sure how to do this below is my code from Web.config file.

system.net>
    <mailSettings>
      <smtp from="yourmail@gmail.com"> 

In my controller, I want to read the Web.config data, how do I do this?

I used the code below but it didn't work.

var fromAddress = new MailAddress("from@gmail.com", "From Name"); //Add from email from web config file

2 Answers2

2

Add Email address to your web.config

  <appSettings>
    <add key="FromEmail" value="yourmail@gmail.com" />
  </appSettings> 

And get it in a controller:

 string Email = ConfigurationManager.AppSettings["FromEmail"];

Edit: OP has mailSettings in config file:

    <system.net> 
      <mailSettings> 
        <smtp from="yourmail@gmail.com"> <!--EMAIL FROM MAYANK--> 
          <network host="smtp.gmail.com" port="587" userName="yourmail@gmail.com" password="yourpassword" enableSsl="true"/>   </smtp> 
  </mailSettings> 
 </system.net>

In that case you can use like this:

var msgSettings = new MailMessage();

var Email = msgSettings.From.Address;
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
0

Config your smtp in code

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);
smtp.Credentials = new NetworkCredential("xxxx@gmail.com", "***");
smtp.EnableSsl = true;
smtp.Send(mail);
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28