Member 11504333 Ответов: 1

Локальный хостинг и Windows хостинг SMTP электронной почты.....


Файл Web. config содержит этот код

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


а оставшийся код здесь

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

          }



      }


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


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
    }

}


Что я уже пробовал:

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 

Richard MacCutchan

Какой SMTP-клиент вы пытаетесь использовать? Если это Gmail, вам нужно убедиться, что ваши учетные данные действительны.

1 Ответов

Рейтинг:
6

Simon_Whale

Я бы прочитал эту статью проекта кода Отправка электронной почты из Yahoo!, GMail, Hotmail (C#)[^]