0

I am trying to add a picture to the mail I am sending in C#, and I found some code in here - stackoverflow. (the question: Add Attachment base64 image in MailMessage and read it in html body). but this code is giving me syntax errors in C# I don't know how to solve correctly... here is my code:

using System.Net.Mail;
using System.Net.Mime;
public static string FixBase64ForImage(string Image)
        {
            System.Text.StringBuilder sbText = new System.Text.StringBuilder(Image, Image.Length);
            sbText.Replace("\r\n", string.Empty); sbText.Replace(" ", string.Empty);
            return sbText.ToString();
        }
        public static bool SendEmail2(string mailToGet, string subject, string message, string image)
        {
            try
            {
                Byte[] bitmapData = Convert.FromBase64String(FixBase64ForImage(image));
                System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData);
                MailMessage mail = new MailMessage();
                var imageToInline = new LinkedResource(streamBitmap, MediaTypeNames.Image.Jpeg);
                imageToInline.ContentId = "MyImage";
                alternateView.LinkedResources.Add(imageToInline);//here there is an error
                mail.AlternateViews.Add(body);//here there is an error
                //from now on - my code:
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.IsBodyHtml = true;
                message+= "<img alt ='' src ='cid: MyImage'/>";
                mail.Body = message;
                mail.Subject = subject;                
                mail.To.Add(mailToGet);
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("myEmailAdress", "myPassword");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
                return true;
            }catch(Exception e)
            {
                throw e;
            }
        }

I have two errors: one on line alternateView.LinkedResources.Add(imageToInline);, where it says: The name 'alternateView' does not exist in the current context, and one on line mail.AlternateViews.Add(body);, where it gives the same - on the body. I tried creating these elements here, but it only got me into more trouble... I don't even know if I am using the right usings (by the way, I put these by the demand of C#, not as part of the code I copied). Can anyone tell me what this code means and where I was wrong? and if you know, what should I do to repair this?

ns120
  • 3
  • 1

1 Answers1

0

Firstly, your alternateView does not exist because you did not instanciate it, to do this you need to add this line before:

AlternateView alternateView = new AlternateView("MyImage");

Where "MyImage" is the name of the file. And in regards to the body error you need to add that alternating view that you just created, so instead of "body" you just need to put alternateView like so:

mail.AlternateViews.Add(alternateView);
  • `new AlternativeView("MyImage")` doesn't work, because the image is not a file saved somewhere - it's a string representing it, it came like that from SQL and so is not saved anywhere. Do you have an advice for this situation? – ns120 Jun 01 '21 at 19:48