Email Inline attachments not working using Azure Communication Service SMTP

Andreas Sundberg 5 Reputation points
2024-11-28T11:15:27.9533333+00:00

Hi!

Do anybody know why email inline attachments don't not work when using Azure Communication Service SMTP (System.Net.Mail library, not the new Azure.Communication.Email library)? Below are some sample code. I tried the same code to send the email via Gmail (smtp.gmail.com) and Office365 (smtp.office365.com) and using those SMTP servers the attachments work perfectly.


Result from Gmail (OK)

User's image

Result from Office365 (OK)

User's image

Result from Azure Communication Service SMTP (NOT OK)

User's image


using EmailTest;

namespace EmailTestConsoleApp

{

internal class Program

{

static void Main(string[] args)

{

  EmailService emailService = new EmailService

  (

    recipient: "******@contoso.com",

    sender: "******@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net",

    smtpServer: "smtp.azurecomm.net",

    smtpAuthUsername: "<Azure Communication Services Resource name>|<Entra Application Id>|<Entra Application Tenant Id>",

    smtpAuthPassword: "<Entra Application Client Secret>"

  );

  emailService.SendEmail();

}

}

}


using System;

using System.IO;

using System.Net;

using System.Net.Mail;

using System.Net.Mime;

namespace EmailTest

{

public class EmailService

{

private string _recipient;

private string _sender;

private string _smtpAuthUsername;

private string _smtpAuthPassword;

private string _smtpServer;

public EmailService(string recipient, string sender, string smtpServer,string smtpAuthUsername, string smtpAuthPassword)

{

  _recipient = recipient;

  _sender = sender;

  _smtpServer = smtpServer;

  _smtpAuthUsername = smtpAuthUsername;

  _smtpAuthPassword = smtpAuthPassword;

}

public void SendEmail()

{

  // Set up the email message

  MailMessage mail = new MailMessage { 

    From = new MailAddress(_sender), 

    Subject = "Test Email with inline Image",

    IsBodyHtml = true

  };

  mail.To.Add(_recipient);

  // Create the HTML body

  string htmlBody = "<html><body><h1>Hello!</h1><p>This is an email with an image as an inline attachment.</p><img src='cid:InlineImage'></body></html>";

  // Create an AlternateView for the HTML

  AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);

  // Load the image into a stream

  using (FileStream imageStream = new FileStream(@"C:\temp\logo.png", FileMode.Open, FileAccess.Read))

  {

    // Create the LinkedResource for the image from the stream

    LinkedResource inlineImage = new LinkedResource(imageStream, "image/png") { ContentId = "InlineImage" };

    // Add the LinkedResource to the AlternateView

    htmlView.LinkedResources.Add(inlineImage);

    // Add the AlternateView to the MailMessage

    mail.AlternateViews.Add(htmlView);

    // Set up the SMTP client

    SmtpClient smtpClient = new SmtpClient(_smtpServer)

    {

      Port = 587, // or your SMTP port

      Credentials = new NetworkCredential(_smtpAuthUsername, _smtpAuthPassword),

      EnableSsl = true

    };

    try

    {

      // Send the email

      smtpClient.Send(mail);

      Console.Write("Email sent successfully!");

    }

    catch (Exception ex)

    {

      Console.Write("Error sending email: " + ex.Message);

    }

  }

}

}

}


Best regards,

Andreas

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

1 answer

Sort by: Most helpful
  1. VenkateshDodda-MSFT 23,811 Reputation points Microsoft Employee
    2024-11-28T15:44:17.5333333+00:00

    @Andreas Sundberg Thanks for posting your question in Microsoft Q&A, apologize for any inconvenience caused on this.

    I have modified the above shared code and using the below I am able to send an email from ACS using SMTP library (System.Net.Mail) with inline image successfully without any issues.

    Modified Code:

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Mime;
    
    namespace EmailTest
    
    {
    
        public class EmailService
    
        {
    
            private string _recipient;
            private string _sender;
            private string _smtpAuthUsername;
            private string _smtpAuthPassword;
            private string _smtpServer;
    
            public EmailService(string recipient, string sender, string smtpServer, string smtpAuthUsername, string smtpAuthPassword)
            {
                _recipient = recipient;
                _sender = sender;
                _smtpServer = smtpServer;
                _smtpAuthUsername = smtpAuthUsername;
                _smtpAuthPassword = smtpAuthPassword;
            }
    
            public void SendEmail()
            {
    
                // Set up the email message
                MailMessage mail = new MailMessage
                {
    
                    From = new MailAddress(_sender),
    
                    Subject = "Test Email with inline Image",
    
                    IsBodyHtml = true
    
                };
    
                mail.To.Add(_recipient);           
    
    
                SmtpClient smtpClient = new SmtpClient(_smtpServer)
    
                {
    
                    Port = 587, // or your SMTP port
    
                    Credentials = new NetworkCredential(_smtpAuthUsername, _smtpAuthPassword),
    
                    EnableSsl = true
    
                };
    
                try
    
                {
    
                    // Send the email
    
                    string fileName = "D:\\acssmtpimage.png";
                    FileStream imageStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    
                    Attachment imageAttachment = new Attachment(imageStream, new ContentType(MediaTypeNames.Image.Jpeg));
                    imageAttachment.ContentDisposition.Inline = true;
                    imageAttachment.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
                    imageAttachment.ContentId = "image1";
                    mail.Attachments.Add(imageAttachment);
    
    
                    string htmlBody = "<html><body><h1>Hello!</h1><p>This is the content of the email.Below email is sent from local machine.<br>Thanks & regards <\br> venkatesh </p><img src=\"cid:image1\"></body></html>";
    
                    // Create an AlternateView for the HTML
                    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
    
                    // Add the AlternateView to the email message
                    mail.AlternateViews.Add(htmlView);
                    smtpClient.Send(mail);
    
                    Console.Write("Email sent successfully!");
                }
    
                catch (Exception ex)
    
                {
    
                    Console.Write("Error sending email: " + ex.Message);
    
                }
    
            }
    
        }
        }
    

    Here is the sample output for your reference:

    User's image

    Hope this helps, let me know if you have any further questions on this.

    Please accept as "Yes" if the answer is helpful so that it can help others in the community.


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.