Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Web.config file contains that code

XML
<appSettings>
    <add key="FromMail" value="info@domain.ae"/>
    <add key ="Password" value="password"/>
    <add key ="Host" value="smtp.gmail.com"/>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>


and the remaining code is here

C#
protected void BtnSubmit_Click(object sender, EventArgs e)
      {

          try
          {
              if (email1.Text.Contains("@") || email1.Text.Contains(".com"))
              {

                  toEmail = email1.Text;
                  EmailSubj = Convert.ToString("Worktops.ae");
                  EmailMsg = Convert.ToString("Hello!");
                  CompanyMsg = Convert.ToString("Team WORKTOPS.ae<br> Customer: " + name1.Text + "");

                  //passing parameter to Email Method
                  SendEmail.Email_Without_Attachment(toEmail, EmailSubj, EmailMsg);
                  SendEmail.Email_Without_Attachment1("info@domain.ae", EmailSubj, CompanyMsg);
                  string message = "Thank you for contacting us. We will get back you soon...";
                  Page.ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);


              }
              else
              {
                  string message = "Please Put an Valid Email address";
                  Page.ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);

              }
          }

          catch (Exception x)
          {

          }



      }


/***************************************************************************/


C#
public static class SendEmail
{
    public static string Pass, FromEmailid, HostAdd;

    public static void Email_Without_Attachment(String ToEmail, String Subj, string Message)
    {
        //Reading sender Email credential from web.config file
        HostAdd = ConfigurationManager.AppSettings["Host"].ToString();
        FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();
        Pass = ConfigurationManager.AppSettings["Password"].ToString();

        //creating the object of MailMessage
        MailMessage mailMessage = new MailMessage();

        mailMessage.From = new MailAddress(FromEmailid); //From Email Id
        mailMessage.Subject = Subj; //Subject of Email
        mailMessage.Body = Message; //body or message of Email
        mailMessage.IsBodyHtml = true;
        mailMessage.To.Add(new MailAddress(ToEmail)); //reciver's Email Id

        SmtpClient smtp = new SmtpClient(); // creating object of smptpclient
        smtp.Host = HostAdd; //host of emailaddress for example smtp.gmail.com etc

        //network and security related credentials

        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential();
        NetworkCred.UserName = mailMessage.From.Address;
        NetworkCred.Password = Pass;
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mailMessage); //sending Email
    }
    public static void Email_Without_Attachment1(String ToEmail, String Subj, string Message)
    {
        //Reading sender Email credential from web.config file
        HostAdd = ConfigurationManager.AppSettings["Host"].ToString();
        FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();
        Pass = ConfigurationManager.AppSettings["Password"].ToString();

        //creating the object of MailMessage
        MailMessage mailMessage = new MailMessage();

        mailMessage.From = new MailAddress(FromEmailid); //From Email Id
        mailMessage.Subject = Subj; //Subject of Email
        mailMessage.Body = Message; //body or message of Email
        mailMessage.IsBodyHtml = true;
        mailMessage.To.Add(new MailAddress(ToEmail)); //reciver's Email Id

        SmtpClient smtp = new SmtpClient(); // creating object of smptpclient
        smtp.Host = HostAdd; //host of emailaddress for example smtp.gmail.com etc

        //network and security related credentials

        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential();
        NetworkCred.UserName = mailMessage.From.Address;
        NetworkCred.Password = Pass;
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mailMessage); //sending Email
    }

}


What I have tried:

C#
Asalam u alaikum...

i am trying to send email using form in C# email is working properly on local host but not working when host properly. if anyone have idea so please help me 
thanks 
Posted
Updated 28-Nov-16 5:22am
v2
Comments
Richard MacCutchan 28-Nov-16 4:20am    
What SMTP client are you trying to use? If it is Gmail you need to ensure your credentials are valid.

1 solution

I would have a read of this code project article Send Email from Yahoo!, GMail, Hotmail (C#)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900