3
Answers

Sending mail with attachment file with direct path

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 = "mhs.vivekvishwas@gmail.com"//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("mhs.vivekvishwas@gmail.com""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)
3
Nainil
NA 660 0 15y

.NET:   
   string
userString = textBox1.Text;
   string replacedString = userString.Replace("'", "''");
 
SQL:
   replacedString = Replace(userString,"'","''")
 
Hope this helps.
Accepted
1
Nainil
NA 660 0 15y

Hi Giorgio,
    Whenever you have single quotes, replace them with 2 single quotes. That should work.
0
Gustavo
NA 1.8k 386.6k 15y

Nainil:
 
Yes, thank you.
0
Gustavo
NA 1.8k 386.6k 15y

Nainil:
 
Can you give me a sample code of the 2 types?
 
0
Nainil
NA 660 0 15y

Giorgio,
   You can either use .NET string.Replace() method to replace any single quotes with 2 single qoutes or you can use T-SQL's Replace function. Since the value being inserted is passed in as a SqlParameter.Value property, you can run the Replace function through it and ensure that any string being passed to the stored procedure is clean. Hope this helps.
0
Gustavo
NA 1.8k 386.6k 15y

Nainil:
 
Yes, if it was a string. But it will be some text that the user entered. Is there a way to do it automatically when I put it into the sql command to save it?