Image is displayed as an attachment in the body of an email via the exchange protocol

I want to send an image embedded in the body of an email via aspose.email , using the exchange protocol. but I receive the image as an attachment and it doesn’t appear in the body of the email.

In Gmail :

image.png (19.7 KB)

@venturis

Could you please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue and provide you more information on it.

Private Sub sendmailembdeddobject()
Dim mail As Aspose.Email.Mail.MailMessage = New Aspose.Email.Mail.MailMessage()
mail.From = New Aspose.Email.Mail.MailAddress(“ctx@venturis.be”)
mail.To.Add(“venturis@gmail.com”)
mail.Subject = “test mail with embdedd object”

    Dim plainView As Aspose.Email.Mail.AlternateView = Aspose.Email.Mail.AlternateView.CreateAlternateViewFromString("This is my plain text content.<img src=cid:barcode>", Nothing, "text/plain")
    Dim htmlView As Aspose.Email.Mail.AlternateView = Aspose.Email.Mail.AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:barcode>", Nothing, "text/html")
    Dim barcode As Aspose.Email.Mail.LinkedResource = New Aspose.Email.Mail.LinkedResource("C:\images\qrcodeEPC.jpg", MediaTypeNames.Image.Jpeg)
    barcode.ContentId = "barcode"
    mail.LinkedResources.Add(barcode)
    mail.AlternateViews.Add(plainView)
    mail.AlternateViews.Add(htmlView)

    Dim client As ExchangeWebServiceClient = GetAsposeEWSClient()
    client.Send(mail)

End Sub
Private Function GetAsposeEWSClient() As ExchangeWebServiceClient
    ' Exchange Server 2010 web service URL
    Dim mailboxURI As String = "https://ServerName/ews/exchange.asmx"
    Dim username As String = "XXXXXX"
    Dim password As String = "XXXXXX"
    Dim domain As String = "XXXXXX"
    ' Connect to the Exchange Server
    Dim credential As NetworkCredential = New NetworkCredential(username, password, domain)
    Dim client As ExchangeWebServiceClient = New ExchangeWebServiceClient(mailboxURI, credential)
    ' return the instance of ExchangeWebServiceClient class
    Return client
End Function

you can try with this code and test sending to a GMAIL address

// For complete examples and data files, please go to GitHub - aspose-email/Aspose.Email-for-.NET: Aspose.Email for .NET examples, plugins and showcase projects
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
string dstEmail = dataDir + “EmbeddedImage.msg”;

// Create an instance of the MailMessage class and Set the addresses and Set the content
MailMessage mail = new MailMessage();
mail.From = new MailAddress(“test001@gmail.com”);
mail.To.Add(“test001@gmail.com”);
mail.Subject = “This is an email”;

// Create the plain text part It is viewable by those clients that don’t support HTML
AlternateView plainView = AlternateView.CreateAlternateViewFromString(“This is my plain text content”, null, “text/plain”);

/* Create the HTML part.To embed images, we need to use the prefix ‘cid’ in the img src value.
The cid value will map to the Content-Id of a Linked resource. Thus will map to a LinkedResource with a ContentId of //‘barcode’. */
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(“Here is an embedded image.”, null, “text/html”);

// Create the LinkedResource (embedded image) and Add the LinkedResource to the appropriate view
LinkedResource barcode = new LinkedResource(dataDir + “1.jpg”, MediaTypeNames.Image.Jpeg)
{
ContentId = “barcode”
};
mail.LinkedResources.Add(barcode);
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
mail.Save(dataDir + “EmbeddedImage_out.msg”, SaveOptions.DefaultMsgUnicode);

@venturis

We have logged this problem in our issue tracking system as EMAILNET-40609. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@venturis

You are using old version of Aspose.Email. Please use the latest version of Aspose.Email for .NET 22.5 and following code to avoid this issue.

Dim credentials As NetworkCredential = New NetworkCredential("from@domain.com", "password")
Dim client As IEWSClient = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", credentials)
Dim mail As MailMessage = New MailMessage()
mail.From = New MailAddress("from@domain.com")
mail.[To].Add("one@gmail.com")
mail.[To].Add("two@gmail.com")
mail.Subject = "This is an email"

Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString("This is my plain text content", Nothing, "text/plain")

' Create the HTML part.To embed images, we need to use the prefix ‘cid’ in the img src value.
' The cid value will map to the Content-Id of a Linked resource. Thus  will map to a LinkedResource with a ContentId of //‘barcode’. 
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:barcode>", Nothing, "text/html")

Dim barcode As LinkedResource = New LinkedResource("11.jpg", MediaTypeNames.Image.Jpeg) With {
    .ContentId = "barcode"
        }
mail.LinkedResources.Add(barcode)
mail.AlternateViews.Add(plainView)
mail.AlternateViews.Add(htmlView)
mail.Save("mail_emb_img.msg", SaveOptions.DefaultMsgUnicode)

client.Send(mail)