Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
For that code as follows

C#
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("rajesh@gmail.com");
mail.To.Add("sathish@gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(@"C:\Users\551386\Desktop\DataDump\Excel.xls");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("rajesh@gmail.com", "12345");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);


when i execute the above code shows error as follows

Failure sending mail
the above error shows in below line as follows

C#
SmtpServer.Send(mail);



please help me what is the mistake in my above code.

What I have tried:

i send mail excel file to my mail

when i execute the above code shows error as follows

Failure sending mail
the above error shows in below line as follows

C#
SmtpServer.Send(mail);
Posted
Updated 25-Aug-16 10:27am
v2
Comments
David_Wimbley 25-Aug-16 9:43am    
What exactly is your exception you get. You copied it by stating Failure sending mail but we don't have access to your gmail account/computer and can't really duplicate this on our own. So if you could provide the exact exception that is the first place to start.
F-ES Sitecore 25-Aug-16 9:46am    
Google how to send email through gmail, this question is asked daily. Chances are you haven't enabled SMTP access on your account, there are many articles covering the things to check. Ultimately you shouldn't be sending email through gmail at all but through the server supplied by your web host or ISP.

1 solution

Please have a look at this solution:
.net - sending email fails in C# 3.5 - Stack Overflow[^]
 
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