When I am trying to send a mail using MailKit that has a html body with embedded images, the images are showing in the email body but also as attachments.
I don’t want attachments, just as mail body like the following
The code for my mailbody composition is as follows:
<code> private async Task<MimeMessage> MailCompose(Command request)
{
var to = request.EmailAddresses.Select(x => MailboxAddress.Parse(x));
var message = new MimeMessage();
message.From.Add(MailboxAddress.Parse(request.MailSettingsRequest.SMTPEmailID));
message.To.AddRange(to);
message.Subject = request.EmailTemplateRequest.TemplateSubject;
message.Body = UpdateImageTags(request.EmailTemplateRequest.MessageBody).ToMessageBody();
return message;
}
static BodyBuilder UpdateImageTags(string input)
{
var bodyBuilder = new BodyBuilder();
string pattern = @"<img[^>]*s+src=['""]([^'""]+)['""][^>]*>"; // Regex to match <img> tags and extract src
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
string imgTag = match.Value;
string srcValue = match.Groups[1].Value;
// Check if src is base64
if (srcValue.StartsWith("data:image/"))
{
// Extract base64 string
string base64Data = srcValue.Substring(srcValue.IndexOf(",") + 1);
string imageFormat = srcValue.Substring(5, srcValue.IndexOf(";") - 5); // Get image format (e.g., png, jpeg)
// Convert base64 to linked resource and add to BodyBuilder
byte[] imageBytes = Convert.FromBase64String(base64Data);
var stream = new MemoryStream(imageBytes);
var id = MimeUtils.GenerateMessageId();
var newpart = new MimePart()
{
ContentId = id,
Content = new MimeContent(stream),
ContentDisposition = new ContentDisposition(ContentDisposition.Inline),
ContentTransferEncoding = ContentEncoding.Base64
};
bodyBuilder.LinkedResources.Add(newpart);
string newImgTag = $"<img src='cid:{id}' />";
input = input.Replace(imgTag, newImgTag);
}
}
bodyBuilder.HtmlBody = input;
return bodyBuilder;
}
</code>
<code> private async Task<MimeMessage> MailCompose(Command request)
{
var to = request.EmailAddresses.Select(x => MailboxAddress.Parse(x));
var message = new MimeMessage();
message.From.Add(MailboxAddress.Parse(request.MailSettingsRequest.SMTPEmailID));
message.To.AddRange(to);
message.Subject = request.EmailTemplateRequest.TemplateSubject;
message.Body = UpdateImageTags(request.EmailTemplateRequest.MessageBody).ToMessageBody();
return message;
}
static BodyBuilder UpdateImageTags(string input)
{
var bodyBuilder = new BodyBuilder();
string pattern = @"<img[^>]*s+src=['""]([^'""]+)['""][^>]*>"; // Regex to match <img> tags and extract src
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
string imgTag = match.Value;
string srcValue = match.Groups[1].Value;
// Check if src is base64
if (srcValue.StartsWith("data:image/"))
{
// Extract base64 string
string base64Data = srcValue.Substring(srcValue.IndexOf(",") + 1);
string imageFormat = srcValue.Substring(5, srcValue.IndexOf(";") - 5); // Get image format (e.g., png, jpeg)
// Convert base64 to linked resource and add to BodyBuilder
byte[] imageBytes = Convert.FromBase64String(base64Data);
var stream = new MemoryStream(imageBytes);
var id = MimeUtils.GenerateMessageId();
var newpart = new MimePart()
{
ContentId = id,
Content = new MimeContent(stream),
ContentDisposition = new ContentDisposition(ContentDisposition.Inline),
ContentTransferEncoding = ContentEncoding.Base64
};
bodyBuilder.LinkedResources.Add(newpart);
string newImgTag = $"<img src='cid:{id}' />";
input = input.Replace(imgTag, newImgTag);
}
}
bodyBuilder.HtmlBody = input;
return bodyBuilder;
}
</code>
private async Task<MimeMessage> MailCompose(Command request)
{
var to = request.EmailAddresses.Select(x => MailboxAddress.Parse(x));
var message = new MimeMessage();
message.From.Add(MailboxAddress.Parse(request.MailSettingsRequest.SMTPEmailID));
message.To.AddRange(to);
message.Subject = request.EmailTemplateRequest.TemplateSubject;
message.Body = UpdateImageTags(request.EmailTemplateRequest.MessageBody).ToMessageBody();
return message;
}
static BodyBuilder UpdateImageTags(string input)
{
var bodyBuilder = new BodyBuilder();
string pattern = @"<img[^>]*s+src=['""]([^'""]+)['""][^>]*>"; // Regex to match <img> tags and extract src
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
string imgTag = match.Value;
string srcValue = match.Groups[1].Value;
// Check if src is base64
if (srcValue.StartsWith("data:image/"))
{
// Extract base64 string
string base64Data = srcValue.Substring(srcValue.IndexOf(",") + 1);
string imageFormat = srcValue.Substring(5, srcValue.IndexOf(";") - 5); // Get image format (e.g., png, jpeg)
// Convert base64 to linked resource and add to BodyBuilder
byte[] imageBytes = Convert.FromBase64String(base64Data);
var stream = new MemoryStream(imageBytes);
var id = MimeUtils.GenerateMessageId();
var newpart = new MimePart()
{
ContentId = id,
Content = new MimeContent(stream),
ContentDisposition = new ContentDisposition(ContentDisposition.Inline),
ContentTransferEncoding = ContentEncoding.Base64
};
bodyBuilder.LinkedResources.Add(newpart);
string newImgTag = $"<img src='cid:{id}' />";
input = input.Replace(imgTag, newImgTag);
}
}
bodyBuilder.HtmlBody = input;
return bodyBuilder;
}
And sample MessageBody that I am getting from the editor is(the full image byte is omitted for brevity)-
"<p>this is a image email</p>n<p><img src="data:image/png;base64,iVBORw0KGgoA..."></p>