sending mail in asp.net-also using web config

28,267

Solution 1

You need to set SMTP setting inside the mailSettings configuration in web.config like this

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network host="smtp.gmail.com" port="587" userName="[email protected]" password="mypassword" />
      </smtp>
    </mailSettings>
  </system.net>

Your server name is smtp.gmail.com (remove the 587 from there). 587 is the port that smtp is using. So put this value in host property.

C# Code:

SmtpClient smtpClient = new SmtpClient();
MailMessage mailMessage = new MailMessage();

mailMessage.To.Add(new MailAddress("[email protected]"));
mailMessage.Subject = "mailSubject";
mailMessage.Body = "mailBody";

smtpClient.Send(mailMessage);

Solution 2

This is what I currently use in my Web Config with some obvious edits

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network defaultCredentials="false" host="smtp.gmail.com" port="587" userName=username" password="xxxxxxxxxxxx" />
      </smtp>
    </mailSettings>
  </system.net>

in the CS file

using System.Net.Mail;

and

    MailMessage myMessage = new MailMessage();
    //subject line
    myMessage.Subject = Your Subject;
    //whats going to be in the body
    myMessage.Body = Your Body Info;
    //who the message is from
    myMessage.From = (new MailAddress("[email protected]"));
    //who the message is to

    myMessage.To.Add(new MailAddress("[email protected]"));

    //sends the message
    SmtpClient mySmtpClient = new SmtpClient();
    mySmtpClient.Send(myMessage);

for sending.

Share:
28,267
user3141831
Author by

user3141831

Updated on November 07, 2020

Comments

  • user3141831
    user3141831 over 3 years

    i am tryiung to create a contact us page ,where the user clicks submit and sends an email to me, i looked at some examples, but they seem to be hard coding their email credentials into the code, i found out that for security m you can store the username and password in the webconfig file, below is my web config code and my default aspx.cs code, could anybody please help me solve the problem, this is the error i get

    The remote name could not be resolved: 'smtp.gmail.com,587' Line 45: mailClient.Send(message);

    Here is my appsettings and code:

            <appSettings>
            <add key="PFUserName" value="[email protected]"/>
        <add key="PFPassWord" value="mypassword"/>
       <add key="MailServerName" value="smtp.gmail.com,587"/>
        </appSettings>
    
    
          using System;
       using System.Data;
       using System.Configuration;
        using System.Web;
        using System.Web.Security;
        using System.Web.UI;
        using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Web.UI.HtmlControls;
        using System.Net.Mail;
        using System.Web.Configuration;
      using System.Net;
    
     namespace WebApplication2 
            {
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            SendMail(txtEmail.Text, txtComments.Text);
        }
    
        private void SendMail(string from, string body)
        {
            string Username = WebConfigurationManager.AppSettings["PFUserName"].ToString();
            string Password = WebConfigurationManager.AppSettings["PFPassWord"].ToString();
            string MailServer = WebConfigurationManager.AppSettings["MailServerName"].ToString();
            NetworkCredential cred = new NetworkCredential(Username, Password);
            string mailServerName = ("smtp.gmail.com,587");
    
    
    
            MailMessage message = new MailMessage(from, Username, "feedback", body);
            SmtpClient mailClient = new SmtpClient("smtp.gmail.com,587");
            mailClient.EnableSsl = true;
    
            mailClient.Host = mailServerName;
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = cred;
            mailClient.Send(message);
            message.Dispose();
    
        }
    }
    

    }

  • user3141831
    user3141831 over 10 years
    what do i change the code in the aspx.cs page to reference the webconfig
  • Sachin
    Sachin over 10 years
    @user3141831 you don't need to send these value on code. As you have already given them in SMTP section in web.config. Just try to run above code.
  • Sachin
    Sachin over 10 years
    @user3141831 you don't need to change anything in code to reference the web.config. Just see my c# code to send the mail.
  • user3141831
    user3141831 over 10 years
    i get this error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. j9sm10355414wjz.13 - gsmtp
  • user3141831
    user3141831 over 10 years
    ran it again and got this The remote name could not be resolved: 'smtp.gmail.com'