Sending Image screenshot to email

Donald Symmons 3,066 Reputation points
2024-11-28T11:26:55.09+00:00

Hi,

I was trying to capture div contents and send to email but when I tested it, I got this Ststem.Byte[]

Here is what I gotmail test

I then tried this, but may I please know the correct one to use, please? I have image controls to the AlternateView

protected void SendToMail_Click1(object sender, EventArgs e)
        {
            using (MailMessage mm = new MailMessage("******@email.com", maillbl.Text))
            {
                mm.Subject = "Event";
                mm.AlternateViews.Add(Mail_Body1());
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient
                {
                    Host = "relay-hosting.secureserver.net",
                    EnableSsl = false
                };
                NetworkCredential NetworkCred = new NetworkCredential("******@email.com", "****************");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 25;
                smtp.Send(mm);
            }
        }
        private AlternateView Mail_Body1()
        {
            string platform = "Signed by management";
            string user = username.Text;
            string base64 = Request.Form[HiddenField1.UniqueID].Split(',')[1];
            byte[] bytes = Convert.FromBase64String(base64);
            string body = "<p Style='font-size: 14px;'>Hello " + user + ",</p>";
            body += "<img src=\"cid:" + bytes + "\">"; //This is the first Image control I added
            body += "<img src=cid:" + bytes + "id='img' alt='' width='100px' height='100px'/>"; //This is another Image control I added
            body += "<p Style='height: 300px; width: 300px;'>" + bytes + "</p>"; //This is what I used initially to test, which gave me tha error
            body += "<p Style='font-size: 14px;'>Best Regards,</p>";
            body += "<p Style='font-size: 14px;'>" + platform + "</p>";
            ContentType mimeType = new System.Net.Mime.ContentType("text/html");
            AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
            return alternate;
        }
Developer technologies .NET Other
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-11-28T16:24:23.43+00:00

    You are not embedding the image correctly. You have a couple options.

    • use url link in the image tag. Many email clients will not load
    • use a base64 string of the image and <img src=“data:image/…>. Outlook and other email clients may strip.
    • use email attachment. <img src=“cid:<attachment cid>”>. You add the image as an attachment to the email, then reference. This is the most supported method.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.