I could't embed image into email. The image was been appended to attachment and I set the contentid, but it could't displayed in the email body. My code is below:
SendGrid.Helpers.Mail.Mail mail = new Mail();
if (mailMessage.Attachments != null)
{
foreach (var attachment in mailMessage.Attachments)
{
if (attachment.Stream == null) continue;
var imageStream = attachment.Stream;
var imageSrc = attachment.DisplayName;
imageStream.Seek(0, SeekOrigin.Begin);
byte[] imageBytes = ReadFully(imageStream);
SendGrid.Helpers.Mail.Attachment mailAttachment = new SendGrid.Helpers.Mail.Attachment();
mailAttachment.Content = Convert.ToBase64String(imageBytes);
mailAttachment.Filename = imageSrc;
if (attachment.IsEmbedd)
{
mailAttachment.ContentId = attachment.EmbeddedName;
}
mail.AddAttachment(mailAttachment);
}
}

I have research, Some people said I need to add the X-SMTPAPI, but I can't ensure what information need to attach. Thanks
Hi @itzxpeng,
Can you please provide the source code for your email?
@thinkingserious Thanks for your reply.
This is my sendemail function:
`private async Task
{
try
{
dynamic _client = new SendGrid.SendGridAPIClient(ApiKey”);
SendGrid.Helpers.Mail.Mail mail = new Mail();
mail.CustomArgs = new Dictionary<string, string>();
SendGrid.Helpers.Mail.Email from = null;
if(mailMessage.From != null)
from = new SendGrid.Helpers.Mail.Email(mailMessage.From.Address, mailMessage.From.DisplayName);
else
{
from = new SendGrid.Helpers.Mail.Email(_fromAddress, !String.IsNullOrWhiteSpace(_fromDisplayName) ? _fromDisplayName : _fromAddress);
}
string subject = mailMessage.Subject;
var htmlBody = mailMessage.Body;
if (mailMessage.Attachments != null)
{
foreach (var attachment in mailMessage.Attachments)
{
if (attachment.Stream == null) continue;
var imageStream = attachment.Stream;
var imageSrc = attachment.DisplayName;
imageStream.Seek(0, SeekOrigin.Begin);
byte[] imageBytes = ReadFully(imageStream);
SendGrid.Helpers.Mail.Attachment mailAttachment = new SendGrid.Helpers.Mail.Attachment();
mailAttachment.Content = Convert.ToBase64String(imageBytes);
mailAttachment.Filename = imageSrc;
if (attachment.IsEmbedd)
{
mailAttachment.ContentId = attachment.EmbeddedName;
}
mail.AddAttachment(mailAttachment);
}
}
SendGrid.Helpers.Mail.Content content = new Content("text/html", htmlBody);
SendGrid.Helpers.Mail.Personalization personalization = new Personalization();
for (int i = 0; i < mailMessage.To.Count; i++)
{
personalization.AddTo(new SendGrid.Helpers.Mail.Email(mailMessage.To[i].Address, mailMessage.To[i].DisplayName));
}
foreach (V2.MailAddress receiver in mailMessage.Cc)
{
personalization.AddCc(new SendGrid.Helpers.Mail.Email(receiver.Address, receiver.DisplayName));
}
foreach (V2.MailAddress receiver in mailMessage.Bcc)
{
personalization.AddBcc(new SendGrid.Helpers.Mail.Email(receiver.Address, receiver.DisplayName));
}
//
mail.From = from;
mail.Subject = subject;
mail.AddContent(content);
mail.AddPersonalization(personalization);
if(mailMessage.ReplyTo != null)
{
foreach (V2.MailAddress receiver in mailMessage.ReplyTo)
{
mail.ReplyTo = new SendGrid.Helpers.Mail.Email(receiver.Address, receiver.DisplayName);
}
}
if (mailMessage.Headers != null)
{
foreach (var header in mailMessage.Headers)
{
mail.AddHeader(header.Key, header.Value);
}
}
dynamic response = await _client.client.mail.send.post(requestBody: mail.Get());
return response.StatusCode.ToString();
}
catch (Exception ex)
{
LogUtil.LogWarning("Cannot delivery email to " + mailMessage.To.ToString(), ex.StackTrace);
throw ex;
}
}`
This is the model V2.MailMessage:
`public class MailMessage
{
public MailMessage()
{
To = new List
Bcc = new List
Cc = new List
ReplyTo = new List
Headers = new Dictionary
Attachments = new List
}
public List<MailAddress> To { get; set; }
public List<MailAddress> Bcc { get; set; }
public List<MailAddress> Cc { get; set; }
public List<MailAddress> ReplyTo { get; set; }
public MailAddress From { get; set; }
public Dictionary<string, string> Headers { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public List<MailAttachment> Attachments { get; set; }
}`
This is the email body, that I want to send:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style type="text/css" media="screen">
.white-content{
width:545px;
border-collapse: collapse;
border:1px solid #e4e5e5;
background-color: #fff;
padding:40px 60px;
}
</style>
</head>
<body style="margin: 0; padding: 0;height: 750px;font-family:Arial,sans-serif,Helvetica;">
<tr>
<td align="center">
<table width="600">
<tr style="padding-top:50px;padding-bottom: 50px">
<td width="42px" valign="top">
<img src=cid:0AAA371A-B43D-4835-B9D0-1E86ABA4E0C4 width="42" height="42" style="vertical-align: top" />
</td>
</tr>
</table>
</td>
</tr>
</body>
<html>
I think if you make the Disposition = "inline", that will do the trick, please see: https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L216
@thinkingserious Good answer. Thank you very much!