Share via


Add Embedded Image to Body of Email

Question

Wednesday, February 13, 2019 9:30 PM

I'm trying to include our company logo in the body of an email. 

code:

//Email Receipt
            var Logo = @"~/App_Data/Images/Logo_Banner.png";
            var ServerBody = Logo + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Purchase Date: " + Purchase_Date + Environment.NewLine + "Transaction ID: " + WBGString.Http_Response + Environment.NewLine + "Product: " + UserPurchase.Product_Name + Environment.NewLine + "Cost: " + UserPurchase.WBG_Amount.ToString() + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Thank you for your purchase. For additional savings & deals. Please check out our promotional codes & offers for free coins, gear and other items or download our app in the Google Play Store 'WBG Mobile'. We appreciate your business and once again, we thank you for your purchase and supporting White Box Gaming. A portion of your purchase may be donated to your local animal shelter." + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "For technical support or general feedback, please contact us [email protected]. Thank you.";

            _WBG_Functions.SendEMail(ServerBody, UserPurchase.Email);

Update:

I have updated the code to get the path but i dont think its working still:

 //Email Receipt
            string banner = HttpContext.Current.Server.MapPath("~/App_Data/Images/Logo 4.gif");
            var linkedResource = new LinkedResource(banner, MediaTypeNames.Image.Gif);
            var ServerBody = "<img src =\"cid:{linkedResource.ContentId}\"/>" + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Purchase Date: " + Purchase_Date + Environment.NewLine + "Transaction ID: " + WBGString.Http_Response + Environment.NewLine + "Product: " + UserPurchase.Product_Name + Environment.NewLine + "Cost: " + UserPurchase.WBG_Amount.ToString() + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Thank you for your purchase. For additional savings & deals. Please check out our promotional codes & offers for free coins, gear and other items or download our app in the Google Play Store 'WBG Mobile'. We appreciate your business and once again, we thank you for your purchase and supporting White Box Gaming. A portion of your purchase may be donated to your local animal shelter." + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "For technical support or general feedback, please contact us [email protected]. Thank you.";

            _WBG_Functions.SendEMail(ServerBody, UserPurchase.Email);

Update:

I got the image kind of but I think I'm getting the wrong path because it wants to use the alt now:

code:

 //Email Receipt
            const string quote = "\"";
            string banner = HttpContext.Current.Server.MapPath("~/App_Data/Images/Logo_4.gif");
            string logo = @"<img src=" + quote + banner + quote + "alt="+ quote + "White Box Gaming" + quote + ">";
            var ServerBody = logo + @"<br><br><br><br>" + "Purchase Date: " + Purchase_Date + @"<br>" + "Transaction ID: " + WBGString.Http_Response + @"<br>" + "Product: " + UserPurchase.Product_Name + @"<br>" + "Cost: " + UserPurchase.WBG_Amount.ToString() + @"<br><br><br><br>" + "Thank you for your purchase. For additional savings & deals. Please check out our promotional codes & offers for free coins, gear and other items or download our app in the Google Play Store 'WBG Mobile'. We appreciate your business and once again, we thank you for your purchase and supporting White Box Gaming. A portion of your purchase may be donated to your local animal shelter." + @"<br><br><br><br>" + "For technical support or general feedback, please contact us [email protected]. Thank you.";

            _WBG_Functions.SendEMail(ServerBody, UserPurchase.Email);

            return WBGString;

All replies (5)

Thursday, February 14, 2019 4:17 PM ✅Answered

I got it working:

 public WBGString UserPurchaseProduct([FromBody]WBG_Purchase UserPurchase)
        {
            //Returns Purchase ID
            WBGString WBGString = new WBGString();
            string Purchase_Date = DateTime.Now.ToString();

            //Save Purchase
            WBGString.Http_Response = _DataHelper.User_Purchase(UserPurchase.Email, UserPurchase.Product_Name, UserPurchase.WBG_Amount);

            //Email Receipt
            const string quote = "\"";
            string banner = "https://i.postimg.cc/qRCQczLM/Logo-Email.png?w=400";
            string logo = @"<img src=" + quote + banner + quote + "alt=" + quote + "White Box Gaming" + quote + ">";

            var ServerBody = logo + @"<br><br><br><br>" + "Purchase Date: " + Purchase_Date + @"<br>" + "Transaction ID: " + WBGString.Http_Response + @"<br>" + "Product: " + UserPurchase.Product_Name + @"<br>" + "Cost: " + UserPurchase.WBG_Amount.ToString() + @"<br><br><br><br>" + "Thank you for your purchase. For additional savings & deals. Please check out our promotional codes & offers for free coins, gear and other items or download our app in the Google Play Store 'WBG Mobile'. We appreciate your business and once again, we thank you for your purchase and supporting White Box Gaming. A portion of your purchase may be donated to your local animal shelter." + @"<br><br><br><br>" + "For technical support or general feedback, please contact us [email protected]. Thank you.";

            _WBG_Functions.SendEMail(ServerBody, UserPurchase.Email);

            return WBGString;
        }

Thursday, February 14, 2019 5:27 AM

Hi old_School,

Thank you for posting here.

A easy way to use EASendMail. You could install EASendMail from NuGet.

Here is the code.

 SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            // Set sender email address, please change it to yours
            oMail.From = "[email protected]";

            // Set recipient email address, please change it to yours
            oMail.To = "[email protected]";

            // Set email subject
            oMail.Subject = "test html email with attachment";

            // Your SMTP server address
            SmtpServer oServer = new SmtpServer("smtp.live.com");

            // User and password for ESMTP authentication, if your server doesn't require
            // User authentication, please remove the following codes.
            oServer.User = "[email protected]";
            oServer.Password = "xxxx";
            oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
            // If your SMTP server requires SSL connection, please add this line
            // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

            try
            {
                // Add image attachment from local disk
                Attachment oAttachment = oMail.AddAttachment("1.jpg");

                // Specifies the attachment as an embedded image
                // contentid can be any string.
                string contentID = "test001@host";
                oAttachment.ContentID = contentID;

                oMail.HtmlBody = "<html><body>this is a <img src=\"cid:"
                     + contentID + "\"> embedded image.</body></html>";

                Console.WriteLine("start to send email with embedded image...");
                oSmtp.SendMail(oServer, oMail);
                Console.WriteLine("email was sent successfully!");
            }
            catch (Exception ep)
            {
                Console.WriteLine("failed to send email with the following error:");
                Console.WriteLine(ep.Message);
            }

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Thursday, February 14, 2019 3:11 PM

Thanks but not a fan of third party tools and I know you can do this in .Net. Here is my most recent code but still not working

//Email Receipt
            const string quote = "\"";
            string banner = "http://whiteboxgaming.xyz/Images/Logo_Email.png";
            string logo = @"<img src=" + quote + banner + quote + "alt="+ quote + "White Box Gaming" + quote + ">";
            var ServerBody = logo + @"<br><br><br><br>" + "Purchase Date: " + Purchase_Date + @"<br>" + "Transaction ID: " + WBGString.Http_Response + @"<br>" + "Product: " + UserPurchase.Product_Name + @"<br>" + "Cost: " + UserPurchase.WBG_Amount.ToString() + @"<br><br><br><br>" + "Thank you for your purchase. For additional savings & deals. Please check out our promotional codes & offers for free coins, gear and other items or download our app in the Google Play Store 'WBG Mobile'. We appreciate your business and once again, we thank you for your purchase and supporting White Box Gaming. A portion of your purchase may be donated to your local animal shelter." + @"<br><br><br><br>" + "For technical support or general feedback, please contact us [email protected]. Thank you.";

            _WBG_Functions.SendEMail(ServerBody, UserPurchase.Email);

The image is on ,y local server just need to use it in the header of the email. 


Thursday, February 14, 2019 3:44 PM

Just noticed your tool requires SSL, I dont have SSL so this will likely not work 

Code:

public void SendEMail(string Body, string SendTo)
        {
            try
            {
                //Email Token
                var ServerEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"].ToString();
                var ServerDisplayName = ConfigurationManager.AppSettings["FromEmailDisplayName"].ToString();
                var ServerEmail = ConfigurationManager.AppSettings["FromUserName"].ToString();
                var ServerPassword = ConfigurationManager.AppSettings["FromEmailPassword"].ToString();
                var ServerBody = Body;

                // Credentials
                var credentials = new NetworkCredential(ServerEmailAddress, ServerPassword);

                // Mail message
                var mail = new MailMessage()
                {
                    From = new MailAddress(ServerEmailAddress),
                    Subject = ServerDisplayName,
                    Body = ServerBody
                };

                mail.IsBodyHtml = true;
                mail.To.Add(new MailAddress(SendTo));

                // Smtp client
                var client = new SmtpClient()
                {
                    Port = 587,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Host = "smtp.gmail.com",
                    EnableSsl = true,
                    Credentials = credentials
                };

                // Send it...         
                client.Send(mail);
            }
            catch(Exception ex)
            {
                //todo: Capture Exception & Log It
            }

Thursday, February 14, 2019 4:00 PM

Update:

I got this to work but it sends as a attachment rather then embeded:

 //Email Receipt
            const string quote = "\"";
            string banner = "http://whiteboxgaming.xyz/Images/Logo_Email.png";
            string logo = @"<img src=" + quote + banner + quote + "alt="+ quote + "White Box Gaming" + quote + ">";
            var ServerBody = logo + @"<br><br><br><br>" + "Purchase Date: " + Purchase_Date + @"<br>" + "Transaction ID: " + WBGString.Http_Response + @"<br>" + "Product: " + UserPurchase.Product_Name + @"<br>" + "Cost: " + UserPurchase.WBG_Amount.ToString() + @"<br><br><br><br>" + "Thank you for your purchase. For additional savings & deals. Please check out our promotional codes & offers for free coins, gear and other items or download our app in the Google Play Store 'WBG Mobile'. We appreciate your business and once again, we thank you for your purchase and supporting White Box Gaming. A portion of your purchase may be donated to your local animal shelter." + @"<br><br><br><br>" + "For technical support or general feedback, please contact us [email protected]. Thank you.";

            _WBG_Functions.SendEMail(ServerBody, UserPurchase.Email);
//Email Token
                var ServerEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"].ToString();
                var ServerDisplayName = ConfigurationManager.AppSettings["FromEmailDisplayName"].ToString();
                var ServerEmail = ConfigurationManager.AppSettings["FromUserName"].ToString();
                var ServerPassword = ConfigurationManager.AppSettings["FromEmailPassword"].ToString();
                var ServerBody = Body;

                // Credentials
                var credentials = new NetworkCredential(ServerEmailAddress, ServerPassword);

                // Mail message
                var mail = new MailMessage()
                {
                    From = new MailAddress(ServerEmailAddress),
                    Subject = ServerDisplayName,
                    Body = ServerBody
                };

                mail.IsBodyHtml = true;
                mail.To.Add(new MailAddress(SendTo));

                // Smtp client
                var client = new SmtpClient()
                {
                    Port = 587,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Host = "smtp.gmail.com",
                    EnableSsl = true,
                    Credentials = credentials
                };

                Attachment attachment;
                attachment = new Attachment(HttpContext.Current.Server.MapPath("~/Images/Logo_Email.png"));
                mail.Attachments.Add(attachment);

                // Send it...         
                client.Send(mail);