Unable to send embedded images with Azure Communication Service SMTP

Aalap 0 Reputation points
2025-02-25T14:52:53.9+00:00

I'm unable to send an email with embedded images with ACS SMTP. The same code works for other non-ACS SMTP servers. I'm creating an alternate view and a linked resource with matching ContentID.
User's image

public void SendEmail()
		{
			MailMessage mail = new MailMessage
			{
				From = new MailAddress(_sender),

				Subject = "Test Email with embedded image",

				IsBodyHtml = true
			};

			mail.To.Add(_recipient);

			string htmlBody = "<html><body><p>This is an email with an embedded image.</p><img src='cid:image001'></body></html>";

			AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
			using (FileStream imageStream = new FileStream(@"D:\Pictures\logo.jpg", FileMode.Open, FileAccess.Read))
			{
				LinkedResource inlineImage = new LinkedResource(imageStream, "image/jpeg") { ContentId = "image001" };
				htmlView.LinkedResources.Add(inlineImage);
				mail.AlternateViews.Add(htmlView);
				SmtpClient smtpClient = new SmtpClient(_smtpHost)
				{
					Credentials = new NetworkCredential(_smtpUsername, _smtpPassword),
					Port = 587,
					EnableSsl = true
				};

				try
				{
					smtpClient.Send(mail);
					Console.Write("Email sent successfully!");
				}
				catch (Exception ex)
				{
					Console.Write("Error sending email: " + ex.Message);
				}
			}
		}

Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
1,020 questions
0 comments No comments
{count} votes

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.