Muhammad Nadeem

Muhammad Nadeem

  • NA
  • 548
  • 66.9k

mail not send in asp.net

Sep 5 2016 6:03 AM
my mail not sent I also include gmailsend.dll but failure sending mail. message will show in catch block.

Answers (6)

0
Ajay Malik

Ajay Malik

  • 0
  • 4.7k
  • 1.6m
Sep 6 2016 7:47 AM
try this code
 
  1. protected void btn_send_Click(object sender, EventArgs e)  
  2. {  
  3.   
  4.     System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();  
  5.     mail.To.Add("to gmail address");  
  6.     mail.From = new MailAddress("from gmail address""Email head", System.Text.Encoding.UTF8);  
  7.     mail.Subject = "This mail is send from asp.net application";  
  8.     mail.SubjectEncoding = System.Text.Encoding.UTF8;  
  9.     mail.Body = "This is Email Body Text";  
  10.     mail.BodyEncoding = System.Text.Encoding.UTF8;  
  11.     mail.IsBodyHtml = true;  
  12.     mail.Priority = MailPriority.High;  
  13.     SmtpClient client = new SmtpClient();  
  14.     client.Credentials = new System.Net.NetworkCredential("from gmail address""your gmail account password");  
  15.     client.Port = 587;  
  16.     client.Host = "smtp.gmail.com";  
  17.     client.EnableSsl = true;  
  18.     try  
  19.     {  
  20.         client.Send(mail);  
  21.         Page.RegisterStartupScript("UserMsg""<script>alert('Successfully Send...');if(alert){ window.location='SendMail.aspx';}</script>");  
  22.     }  
  23.     catch (Exception ex)  
  24.     {  
  25.         Exception ex2 = ex;  
  26.         string errorMessage = string.Empty;  
  27.         while (ex2 != null)  
  28.         {  
  29.             errorMessage += ex2.ToString();  
  30.             ex2 = ex2.InnerException;  
  31.         }  
  32.         Page.RegisterStartupScript("UserMsg""<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");  
  33.     }  
 
Accepted Answer
0
Rajeesh Menoth

Rajeesh Menoth

  • 67
  • 27k
  • 2.7m
Sep 6 2016 6:37 AM
Hi,
 
Instead of 465 Use 587 port id.
  1. SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp  
Reference :
 
http://www.c-sharpcorner.com/blogs/smtp-server-requires-a-secure-connection1 
0
Muhammad Nadeem

Muhammad Nadeem

  • 0
  • 548
  • 66.9k
Sep 6 2016 6:34 AM
I try this code but still I cannot send mail.
protected void Page_Load(object sender, EventArgs e)
{
string to = "tazeemjaan73@gmail.com"; //To address
string from = "tazeemjaan73@gmail.com"; //From address
MailMessage message = new MailMessage(from, to);
string mailbody = "In this article you will learn how to send a email using Asp.Net & C#";
message.Subject = "Sending Email Using Asp.Net & C#";
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 465); //Gmail smtp
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("tazeemjaan73@gmail.com", "12345");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
client.Send(message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
0
Rajeesh Menoth

Rajeesh Menoth

  • 67
  • 27k
  • 2.7m
Sep 5 2016 7:41 AM
Hi,
 
First of all check the given gmail smtp port id is "587" then it will work. You need to check more about sending mail using C#.
Please check my article and blog content for solving Gmail mail sending issues.
 
Reference :
 
http://www.c-sharpcorner.com/UploadFile/2a6dc5/how-to-send-a-email-using-Asp-Net-C-Sharp/
 
http://www.c-sharpcorner.com/blogs/smtp-server-requires-a-secure-connection1
 
https://rajeeshmenoth.wordpress.com/2014/07/31/how-to-send-a-email-using-asp-net-c/
 
http://stackoverflow.com/questions/7806944/failure-sending-mail-via-google-smtp 
0
Manas Mohapatra

Manas Mohapatra

  • 89
  • 20.4k
  • 16.7m
Sep 5 2016 7:00 AM
What is exception message you are getting in catch block. Provide details.
0
Vishal Jadav

Vishal Jadav

  • 0
  • 1.3k
  • 253.2k
Sep 5 2016 6:13 AM
Hi,
Can you try following code ?
public string SendEmail(string SendTo, string Subject, string MessageBody)
{
try
{
MailMessage mailmsg = new MailMessage("EMAIL ID", SendTo, Subject, MessageBody);
mailmsg.IsBodyHtml = true;
mailmsg.BodyEncoding = System.Text.Encoding.ASCII;
SmtpClient client = new SmtpClient("SMTP", 25);
client.UseDefaultCredentials = false;
//client.EnableSsl = true;
client.Credentials = new NetworkCredential("EMAIL ID", "PASSWORD");
client.Send(mailmsg);
}
catch (Exception ex)
{
return ex.ToString();
}
return "1";
}