void sendMail(string invoiceNumber)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("Smtp.gmail.com");
mail.From = new MailAddress("*******@gmail.com");
mail.To.Add("******@gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = invoiceNumber;
mail.Subject = "PDFs Attached";
DirectoryInfo di = new DirectoryInfo(@"C:\Users\User\Desktop\test\");
foreach (var file in di.GetFileSystemInfos("*.pdf*"))
mail.Attachments.Add(new Attachment(file.FullName));
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("*******@gmail.com", "*********");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Asked
Active
Viewed 29 times
-5
-
3Please see [ask] and [The perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). – rene Sep 24 '16 at 09:49
-
Welcome to SO. You should read about how we write a well formed question. The link that rene has already mentioned is a very good start. For this time, I formmated you code. However, this is not still a question. It's just code with a titke `mail was not sending`. You have to describe what is going wrong? What are your findings until this moment and where you have stuck. – Christos Sep 24 '16 at 09:53
-
Possible duplicate of [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Igor Sep 24 '16 at 10:56
1 Answers
0
https://www.google.com/settings/security/lesssecureapps use this link to Allow less secure apps on of your gmail id....
void sendMail(string invoiceNumber)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add("recevermailid");
mail.From = new MailAddress("sender id");
mail.Subject = "Confirmation";
string Body = "your message body";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("sender email", "seneder email password");// Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

vijay chaudhary
- 39
- 2