0

Im trying to send an email via HTTP Client in c# by sending a html string and i wish to send with the image that is inside the html body but the mail sends the html raw code and send 2 .dat attachments

I try to use alternative view and linked resource to send the image

My Email Method is this:

 try
        {
            SmtpClient client = new SmtpClient();
            {
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.EnableSsl = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = "mailaccount@gmail.com",
                    Password = "mypaswrd"
                    
                };
            };
            MailAddress fromEmail = new MailAddress("mailaccount@gmail.com", "Name");
            MailAddress toEmail = new MailAddress("mailaccount@gmail.com", "Name");
          

            AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlString, null, "image/jpg");
            LinkedResource linkedResource = new LinkedResource(@"routeofmyimage");
            alternateView.ContentId = "logo";
            alternateView.LinkedResources.Add(linkedResource);




            MailMessage message = new MailMessage()
            {
                From = fromEmail,
                Subject = "Subject",
                IsBodyHtml = true,
                Body = htmlString,
            };
            if (alternateView != null)
            {
                message.AlternateViews.Add(alternateView);
            }
            message.To.Add(toEmail);
            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {

            }
        }
        catch (Exception ex)
        {

        }

My CreateHTMLBody Method

  public static string getHtmlTruckData(/*string IDTruck, DataTable data*/)
    {
        try
        {
            string messageBody = "";
            string styleFormat = "<style>" +
                                        "table.border {" +
                                            "border-collapse:collapse;" +
                                        "}" +
                                            "table-border th {" +
                                                "padding: 5px; " +
                                                "border: 1px solid black" +
                                            "}" +
                                            "table.border td {" +
                                                "padding: 5px; " +
                                                "border: 1px solid black" +
                                            "}" +
                                      "</style>";

            string htmlMainTableStart = "<table style=\"width:100%;\">";
            string htmlMainTableEnd = "</table>";
            string headerHTMLFormat = "<tr>" +
                "<td style=\"width:20%;\">" +
                "<td style=\"width:60%\" align=\"center\">" +
                "<table>" +
                "<img src=\"cid:logo\"/>"+
                "</table>" +
                "</td>" +
                "<td style=\"width:20%;\">" +
                "<table class=\"border\" style=\"width:100%\">" +
                "<tr><td align=\"center\">"+ DateTime.Now.ToShortDateString() + "</td> </tr>" +
                "<tr><td align=\"center\">" + "Daily Trip Report" + "</td> </tr>" +
                "</table>"+ 
                "</td>" +
                "</td>" +
                "</tr>";

            messageBody = styleFormat;
            messageBody += htmlMainTableStart;
            messageBody += headerHTMLFormat;
            messageBody += htmlMainTableEnd;

            return messageBody;

        }
        catch (Exception ex)
        {
            return null;
        }
    }

The Sent Mail:

<style>table.border {border-collapse:collapse;}table-border th {padding: 5px; border: 1px solid black}table.border td {padding: 5px; border: 1px solid black}</style><table style="width:100%;"><tr><td style="width:20%;"><td style="width:60%" align="center"><table><img src="cid:logo"/></table></td><td style="width:20%;"><table class="border" style="width:100%"><tr><td align="center">10/7/2022</td> </tr><tr><td align="center">Daily Trip Report</td> </tr></table></td></td></tr></table>
itsFerxis
  • 31
  • 6
  • So... what is your question? – Dai Oct 07 '22 at 21:41
  • You can either convert your image to a data url (`data:png,...`) and link it to your HTML through an `` tag, or upload it to your servers and link it from there. – code Oct 07 '22 at 21:48
  • @Dai how to visualize the image in my mail that its inside of the html body – itsFerxis Oct 07 '22 at 21:56
  • @code All of those are bad choices (`data:` URIs balloon message size, and externally-hosted images are blocked by most e-mail clients). What's wrong with `cid:` attachments, exactly? – Dai Oct 07 '22 at 21:58
  • @itsFerxis `AlternateView.CreateAlternateViewFromString(htmlString, null, "image/jpg");` <-- This line is completely wrong: your `htmlString` is not a JPEG image.... Where is the actual image file or binary data originating from? – Dai Oct 07 '22 at 21:59
  • @Dai my image right now is inside my project in Resources/logo.jpg – itsFerxis Oct 07 '22 at 22:04

0 Answers0