Vivek Kumar Vishwas

Vivek Kumar Vishwas

  • NA
  • 115
  • 56.5k

Sending mail with attachment file with direct path

Nov 3 2017 7:47 AM
Hi,
I am having an error while passing a direct  path to mail attachment.
 
Please check and update me soon. Dead line till today.
  1. // Code   as below  
  2.    
  3. public void sendmail_to_user()  
  4. {  
  5. string to = txtemail.Text; //To address  
  6. string from = "[email protected]"//From address  
  7. string[] Multiple = to.Split(',');  
  8. MailMessage message = new MailMessage();  
  9. message.From = new MailAddress(from);  
  10. foreach (string multiple_email in Multiple)  
  11. {  
  12. message.To.Add(new MailAddress(multiple_email));  
  13. }  
  14. //http://localhost:1728/InsertRecordMSAccess(CSharp)/file/pitamber.pdfhttp://localhost:1728/InsertRecordMSAccess(CSharp)/file/CrystalReportViewer1.pdf  
  15. //message.Attachments.Add(new Attachment("/file/" + txtContact.Text + ".pdf"));  
  16. // Below i am passing path of my document  
  17. message.Attachments.Add(new Attachment("file/CrystalReportViewer1.pdf"));  
  18. //if (FileUpload2.HasFile)//Attaching document  
  19. //{  
  20. // string FileName = Path.GetFileName(FileUpload2.PostedFile.FileName);  
  21. // message.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileName));  
  22. //}  
  23. string mailbody = "Message Body";  
  24. message.Subject = "Subject";  
  25. message.Body = mailbody;  
  26. message.BodyEncoding = Encoding.UTF8;  
  27. message.IsBodyHtml = true;  
  28. SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp  
  29. System.Net.NetworkCredential basicCredential1 = new  
  30. System.Net.NetworkCredential("[email protected]""Password");  
  31. client.EnableSsl = true;  
  32. client.UseDefaultCredentials = false;  
  33. client.Credentials = basicCredential1;  
  34. try  
  35. {  
  36. client.Send(message);  
  37. }  
  38. catch (Exception ex)  
  39. {  
  40. throw ex;  
  41. }  
  42. } 

Answers (3)