Rahul Sharma

Rahul Sharma

  • NA
  • 194
  • 12.8k

GettingError while sending mail using C# code on Server

Jan 28 2018 1:39 AM
i am getting an error Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
 
on shared hosting server but it worked fine on localhost
here is my code
 
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = false;
message.Text = "Loading...";
SmtpClient smtp = new SmtpClient( "smtp.gmail.com", 587);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
smtp.EnableSsl = true;
//smtp.UseDefaultCredentials = false;
smtp.Send(mail);
message.Text = "Message sent successfully....";
txtEmail.Text = "";
txtmsg.Text = "";
txtName.Text = "";
success.Visible = true;
 

Answers (8)