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?