0

I have a list of email id in a excel and each email id have a image to be attched whose url will be written in the next row of the excel. Please help me to attach each image with its corresponding image. I have written program to send mail. This shows error Illegal characters in path at the point where it has to attach file. Thanks in advance.

Please help me with this.

{

        DataTable dtValues = SQLHelper.ExecuteDataset("Stored Procedure").Tables[2];
        if (dtValues.Rows.Count > 0)
            System.Console.Clear();
        foreach (DataRow row in dtValues.Rows)
        {
            string mailname = Convert.ToString(row["row1"]);
            string mailemail = Convert.ToString(row["row2"]);
            string Code = Convert.ToString(row["row3"]);
            string Code_new = Code.Replace("\t", "");
            string Id = Convert.ToString(row["row4"]);
            string str = AppDomain.CurrentDomain.BaseDirectory;
            string image = str + "Codes\\" + Code_new + ".jpg";
            //string path = Microsoft.SqlServer.Server.MapPath("/HTMLPage.html");
            //string strPath = HttpContext.Current.Server.MapPath("~/HTML");

            //string body = string.Empty;
            StreamReader reader = new StreamReader("HTMLPage1.html");
            string readfile = reader.ReadToEnd();
            string body = "";
            body = readfile;
            body = body.Replace("{USERNAME}", mailname);
            body = body.Replace("{EMAILID}", mailemail);
            body = body.Replace("{CODE}", QRCode);
            body = body.Replace("{IMAGE}", image);

            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress("xxx@gmail.com"); // Sender e-mail address.
            Msg.To.Add(mailemail);
            Console.WriteLine(Id + " : " + mailemail);

            Msg.Subject = "QR Code";
            Msg.Body = "Hi";
            Attachment attachFile = new Attachment(body);
            Msg.Attachments.Add(attachFile);
            Msg.IsBodyHtml = true;
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            SmtpServer.Port = 587;
            SmtpServer.EnableSsl = true;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential("xxx2694@gmail.com", "984621232312");
            SmtpServer.Send(Msg);

        }
 }
  • What have you tried already? Please share your code, that isn't working so we can help. – nilsK Jan 09 '18 at 13:41
  • 1
    Possible duplicate of [Send inline image in email](https://stackoverflow.com/questions/18358534/send-inline-image-in-email) – SSD Jan 09 '18 at 13:42
  • I have shared the code which I have used. I am not able to add the html page as attachment.. I want to add it as attachment. – LINCY VARKEY Jan 11 '18 at 04:55

1 Answers1

3

It should add an attachment:

// Create file attachment
Attachment ImageAttachment = new Attachment(Server.MapPath(Convert.ToString(row["FilePath"])));

// Set the ContentId of the attachment, used in body HTML
ImageAttachment.ContentId = Convert.ToString(row["FileName"]);

// Add an image as file attachment
Msg.Attachments.Add(ImageAttachment);
Iniamudhan
  • 478
  • 4
  • 17
Bruno Quintella
  • 371
  • 2
  • 9
  • this works only for a web application, and only if the file to attach is local to the web application itself: OP request seems more general – Gian Paolo Jan 09 '18 at 15:28
  • @BrunoQuintella . Thanks for your reply.. I have created an html page and have to replace few words with another and send this html as attachment.. The current code i have posted in the question. – LINCY VARKEY Jan 11 '18 at 12:41
  • I have shared the code which I have used. I am not able to add the html page as attachment.. I want to add it as attachment. – LINCY VARKEY Jan 11 '18 at 12:41
  • I am looking at your last edit and you`re trying to attache the body variable: **Attachment attachFile = new Attachment(body);** You should pass the path of a file here. – Bruno Quintella Jan 11 '18 at 13:49