Microsoft.Graph Forward as attachment - c#

I'm using updating out Exchange Mail Client written in .NET 4 Framework using Exchange Web Services (EWS) to .NET Core 6 using Microsoft.Graph. I've managed to port most of the functions but I'm having trouble working out how to Forward a Message as an Attachment.
In EWS
// This method results in a GetItem call to EWS.
var msgToAttach = EWS.EmailMessage.Bind(
ServiceInstance,
message.Source.Id,
new EWS.PropertySet(EWS.ItemSchema.MimeContent, EWS.ItemSchema.Subject));
// Create an email message and set properties on the message.
var forward = new EWS.EmailMessage(ServiceInstance);
if (!bodyType.HasValue)
bodyType = message.BodyType;
forward.Subject = subject == null ? $"FW: {message.Subject}" : subject;
forward.Body = new EWS.MessageBody(
bodyType.Value == BodyTypes.Html ? EWS.BodyType.HTML : EWS.BodyType.Text,
body ?? "Please refer to the attachment message");
// Add additional recipients to the reply email message.
if (to?.Any() ?? false)
forward.ToRecipients.AddRange(to);
if (cc?.Any() ?? false)
forward.CcRecipients.AddRange(cc);
if (bcc?.Any() ?? false)
forward.BccRecipients.AddRange(bcc);
// before we can add the attachments, we need to save the draft
// according to the docoumentation this isn't required but without
// it throws an exception on Save/SendAndSaveCopy if the attachments
// are added to a message which doesn't exist
forward.Save();
// Add an email message item attachment and set properties on the item.
EWS.ItemAttachment<EWS.EmailMessage> itemAttachment = forward.Attachments.AddItemAttachment<EWS.EmailMessage>();
itemAttachment.Item.MimeContent = msgToAttach.MimeContent;
itemAttachment.Name = msgToAttach.Subject;
// Send the mail and save a copy in the Sent Items folder.
// This method results in a CreateItem and SendItem call to EWS.
forward.Update(EWS.ConflictResolutionMode.AlwaysOverwrite);
forward.Send();
UPDATE: Solution
You can read the mime content using
_serviceInstance.Users[UserId].Messages[message.Id].Content.GetAsync()
which returns a stream that can be saved as .eml file. The attachment name must end with .eml or it doesn't work.
public void ForwardMessageAsAttachment(
Message message,
IEnumerable<string> to,
string subject = null,
string body = null,
BodyType? bodyType = null)
{
// Download the mime content for the specific message
var mimeContentStream = _serviceInstance
.Users[UserId]
.Messages[message.Id]
.Content
.Request()
.GetAsync()
.GetAwaiter()
.GetResult();
// the mine content is returned as a Stream, so write it to buffer
var buffer = new Span<Byte>(new byte[mimeContentStream.Length]);
var mimeContent = mimeContentStream.Read(buffer);
// The attachment Name must ends with .eml else Outlook wont recognize its an email
// even with the contentType = message/rfc822
var messageAsAttachment = new FileAttachment
{
ContentBytes = buffer.ToArray(),
ContentType = "message/rfc822",
Name = $"{message.Subject}.eml"
};
// create a new message
var forward = new Message()
{
Attachments = new MessageAttachmentsCollectionPage(),
Subject = String.IsNullOrWhiteSpace(subject) ? subject : $"FW: {message.Subject}".Trim(),
Body = new ItemBody
{
ContentType = bodyType,
Content = body
},
ToRecipients = to.Select(x => new Recipient { EmailAddress = new EmailAddress { Address = x } })
};
// add the EML attachment
forward.Attachments.Add(messageAsAttachment);
_serviceInstance
.Users[UserId]
.SendMail(forward)
.Request()
.PostAsync()
.GetAwaiter()
.GetResult();
}

In the Graph there is no equivalent to
EWS.ItemAttachment<EWS.EmailMessage> itemAttachment =
forward.Attachments.AddItemAttachment<EWS.EmailMessage>();
itemAttachment.Item.MimeContent = msgToAttach.MimeContent;
So your options are to attach the message as an EML file as a File Attachment
or you can build the MIME message outside with a MIME parser like mailkit and then send the message as MIME https://github.com/microsoftgraph/microsoft-graph-docs/blob/main/concepts/outlook-send-mime-message.md you will be limited on size with this method you can have anything larger then 4MB and with mimebloat that usually means around 2-3 MB

Mime content from a message can be read using MS Graph and converted to .EML, then sent FileAttachment on a new message. You need to make sure the attachment's name ends with .eml or outlook will not recognize attachment as a message. Adding content type of message/rfc822 does not appear to affect how the attachment is read by gmail, hotmail or Outlook desktop.
public void ForwardMessageAsAttachment(
Message message,
IEnumerable<string> to,
string subject = null,
string body = null,
BodyType? bodyType = null)
{
// Download the mime content for the specific message
// _serviceInstance is an instance of GraphServiceClient
var mimeContentStream = _serviceInstance
.Users[UserId]
.Messages[message.Id]
.Content
.Request()
.GetAsync()
.GetAwaiter()
.GetResult();
// the mine content is returned as a Stream, so write it to buffer
var buffer = new Span<Byte>(new byte[mimeContentStream.Length]);
var mimeContent = mimeContentStream.Read(buffer);
// The attachment Name must ends with .eml else Outlook wont recognize its an email
// even with the contentType = message/rfc822
var messageAsAttachment = new FileAttachment
{
ContentBytes = buffer.ToArray(),
ContentType = "message/rfc822",
Name = $"{message.Subject}.eml"
};
// create a new message
var forward = new Message()
{
Attachments = new MessageAttachmentsCollectionPage(),
Subject = String.IsNullOrWhiteSpace(subject) ? subject : $"FW: {message.Subject}".Trim(),
Body = new ItemBody
{
ContentType = bodyType,
Content = body
},
ToRecipients = to.Select(x => new Recipient { EmailAddress = new EmailAddress { Address = x } })
};
// add the EML attachment
forward.Attachments.Add(messageAsAttachment);
_serviceInstance
.Users[UserId]
.SendMail(forward)
.Request()
.PostAsync()
.GetAwaiter()
.GetResult();
}

Related

LinkedResource fails in MailMessage with CC recipients, succeeds otherwise

I am developing a Windows Background Service with .NET and C#. It periodically queries a REST API and sends emails if certain conditions are met. Sometimes the data returned from the API will have image files and sometimes not. When image files are returned, the service embeds them into the email body before sending. I'm embedding the image file in four steps:
Download the image as a byte[]
Convert byte[] to MemoryStream
pass the stream to a new LinkedResource
Add the linked resource to the AlternateView for the MailMessage
This works if the message has a single "To" recipient and no CC or BCC recipients. The image displays correctly in the body of the email. The weird problem I'm having is that if I add multiple recipients in To, CC or BCC, the image fails to display in the email body. This problem seems to be limited to Outlook. I did a test with two recipients, one internal to the organization and one Gmail account. The image displayed correctly in Gmail but was broken in Outlook.
Here is the snippet of code where the service builds the LinkedResource and sends the email.
htmlBody = htmlBody.Replace("{incident_time}", incident.IncidentTime)
.Replace("{incident_type}", incident.IncidentType)
.Replace("{incident_location}", incident.IncidentLocation)
.Replace("{reporting_agency}", incident.ReportingAgency)
.Replace("{severity}", incident.Severity)
.Replace("{details}", incident.Details);
// embed attached images
var attachmentsResult = await arcGisRestClient.QueryAttachmentsAsync(
incidentSublayerUrl,
tokenResponse.Token,
objectId: incident.ObjectId);
IList<LinkedResource> linkedResources = new List<LinkedResource>();
if (!string.IsNullOrEmpty(attachmentsResult))
{
var attachmentsObj = JObject.Parse(attachmentsResult);
var attachments = FeatureAttachmentConverter.FromJObject(attachmentsObj);
// if attachments > 0
if (attachments.Count > 0)
{
string imgHtml = "";
foreach (var attachment in attachments)
{
string attachmentUrl = $"{incidentSublayerUrl}/{attachment.ParentObjectId}/attachments/{attachment.Id}?token={tokenResponse.Token}";
WebClient webClient = new WebClient();
byte[] bytes = webClient.DownloadData(attachmentUrl);
MemoryStream stream = new MemoryStream(bytes);
LinkedResource linkedResource = new LinkedResource(stream, MediaTypeNames.Image.Jpeg);
imgHtml += #"<img src='cid:" + linkedResource.ContentId + #"' width='450'/>";
linkedResources.Add(linkedResource);
}
htmlBody = htmlBody.Replace("{attachments}", imgHtml);
}
else
{
htmlBody = htmlBody.Replace("{attachments}", "");
}
}
AlternateView htmlAlternate = AlternateView.CreateAlternateViewFromString(htmlBody, new ContentType(MediaTypeNames.Text.Html));
foreach (var lr in linkedResources)
{
htmlAlternate.LinkedResources.Add(lr);
}
// create email
MailMessage message = new MailMessage
{
Subject = "TEST Incident Notification",
};
message.AlternateViews.Add(htmlAlternate);
string[] emailToAddresses = _config.GetSection("EmailNotificationTo").Get<string[]>();
message.To.Add(string.Join(',', emailToAddresses));
string[] emailCcAddresses = _config.GetSection("EmailNotificationCc").Get<string[]>();
message.CC.Add(string.Join(',', emailCcAddresses));
message.From = new MailAddress(_config["EmailNotificationFrom"]);
// send email
SmtpClient smtpClient = new SmtpClient("mail.domain.org", 25);
using (smtpClient)
{
smtpClient.Send(message);
logger.Info($"Email notification sent; objectid: {incident.ObjectId}");
}
Could this be an issue with our internal STMP server or Outlook settings rather than a problem with the above code?

How to attach a file as an attachment in email using SendGrid?

I am wanting to attach a file as an attachment using SendGrid and C# - I have the code below which runs but the response.StatusCode that is returned is
BadResponse
How do I alter this code so that the file is attached and an email sent successfully?
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var listAtta = new List<FileAttachment>();
emailProducts.Select(o => o.tp).ToList().ForEach(o =>
{
string file = o.ProductPdf;
var fileBytes = FileToByteArray(o.ProductPdf);
if (fileBytes != null && fileBytes.Count() > 0)
{
listAtta.Add(new FileAttachment
{
FileData = fileBytes,
FileName = o.ProductPdf
}); ;
}
msg.AddAttachment(o.ProductPdf, fileBytes.ToString());
});
var response = await client.SendEmailAsync(msg);
var success = response.StatusCode;
you need to base64 encode it
msg.AddAttachment(o.ProductPdf, Convert.ToBase64String(bytes) )

How to read attachment content from bot framework C#?

I am writing a bot and expecting the user to send me an attachment, which I want to read and translate into objects.
I have the following code so far:
if (message.Attachments != null && message.Attachments.Any())
{
var attachment = message.Attachments.First();
using (HttpClient httpClient = new HttpClient())
{
if ((message.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || message.ChannelId.Equals("msteams", StringComparison.InvariantCultureIgnoreCase)) && new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
{
var token = await new MicrosoftAppCredentials().GetTokenAsync();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
var contentLenghtBytes = responseMessage.Content.Headers.ContentLength; // this is populated correctly
if(attachment.Name.ToLower().Equals("opportunity.xlsx"))
{
var temp = attachment.Content; // This Content is always null, even though everything else is populated.
}
}
}
Anyone can suggest how can I read the attachment xlsx content please?
Thanks
The attachment is not available in the Content property. You first need to download the attachment using the ContentUrl and then perform whatever you want, using the response message after downloading the file.
Take a look at the Receive-Attachments C# sample.
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;
if (message.Attachments != null && message.Attachments.Any())
{
var attachment = message.Attachments.First();
using (HttpClient httpClient = new HttpClient())
{
// Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
if ((message.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || message.ChannelId.Equals("msteams", StringComparison.InvariantCultureIgnoreCase))
&& new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
{
var token = await new MicrosoftAppCredentials().GetTokenAsync();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
await context.PostAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");
}
}
else
{
await context.PostAsync("Hi there! I'm a bot created to show you how I can receive message attachments, but no attachment was sent to me. Please, try again sending a new message including an attachment.");
}
context.Wait(this.MessageReceivedAsync);
}
You will have to use some other dll to read the excel data, if that is what you mean. Do u mean to read the contents of the excel file after it has been uploded then you could use https://github.com/ExcelDataReader/ExcelDataReader

Incorrect file recieved when user replies with attachment

My bot prompts the user for an attachment in a dialog and is suppose to receive an image from the user and save the content name, url and type of that file to variables. However, the file I am receiving is not the file the user sent. Instead its retrieving a file named 'blob' (no extension) from this link:
https://webchat.botframework.com/attachments/GkLhiqJcvH019mP0iGBKvo/0000052/0/blob?t=X5ICiUrhFas.dAA.RwBrAEwAaABpAHEASgBjAHYASAAwADEAOQBtAFAAMABpAEcAQgBLAHYAbwAtADAAMAAwADAAMAA1ADIA.mcEiHxuH0gE.VMcp6Yduqgc.4xT1ZTOvCX-B7A0nLto6eZNFrFi-0xSzGk5AKmA-EPE
That file contains:
{"type":"message","from":{"id":"9n0I1ZqSrLF","name":"You"},"locale":"en-US","timestamp":"2017-02-14T21:12:32.074Z","channelData":{"clientActivityId":"1487106689866.9060198022610071.8"}}
Here is the code for the attachment prompt. This prompt is within a dialog:
private async Task getAttach(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
{
IEnumerable<Attachment> list = await result;
Attachment attachment = list.FirstOrDefault();
string filename = attachment.Name;
string url = attachment.ContentUrl;
string contentType = attachment.ContentType;
//Set attachmentFileNames
if (attachmentFileNames == null)
{
attachmentFileNames = "";
}
attachmentFileNames += contentType;
attachmentFileNames += ",";
numberOfFiles++;
//Set attachmentFileURLs
if (attachmentFileURLs == null)
{
attachmentFileURLs = "";
}
attachmentFileURLs += url;
attachmentFileURLs += ",";
attachmentHasBeenAdded = true;
await FirstMessageReceivedAsync(context, result);
}
Here is how I am handling attachments in the message controller. This is within the Post Task:
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
if (activity.Attachments != null && activity.Attachments.Any())
{
hasaAttachment = true;
var attachment = activity.Attachments.First();
using (HttpClient httpClient = new HttpClient())
{
// Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
if ((activity.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || activity.ChannelId.Equals("msteams", StringComparison.InvariantCultureIgnoreCase))
&& new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
{
var token = await new MicrosoftAppCredentials().GetTokenAsync();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
Activity reply = activity.CreateReply($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received. URL: {attachment.ContentUrl}");
await connector.Conversations.ReplyToActivityAsync(reply);
}
}
I am following this example:
Receive Attachment Bot Sample
In the emulator it works correctly but the published version on azure does not. The code is very similar to the example so I don't see why the bot is not detecting the user's files.
How do I receive the correct file (the file the user replied with)?
The attachments will be in elements 1 onwards. E.g.
Attachment attachment = list.ElementAt(1);
The webchat (and maybe other platforms) has a weird quirk where it adds a blob file at the start of IEnumerable 'result' when prompting and getting attachments from the user.
private async Task getAttach(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
{...}

Send raw bulkmail using aws ses ApI interface(Empty required header 'To'.)

Hi I am trying to send bulkmail with attachment using amazon ses. I can able to send mails with attachment but my to-mails are appearing for all the users that I have send. I am trying to add those destination mails in bcc fields but it is throwing an error Empty required header 'To'.
This is what I've already tried:
private static BodyBuilder GetMessageBody()
{
var body = new BodyBuilder()
{
HtmlBody = #"<p>Amazon SES Test body</p>",
TextBody = "Amazon SES Test body",
};
body.Attachments.Add(#"G:\me.jpg");
return body;
}
private static MimeMessage GetMessage()
{
var message = new MimeMessage();
List<string> to = new List<string>(50);
to.Add("xxxxxx#gmail.com");
to.Add("xxxxxx#gmail.com");
message.From.Add(new MailboxAddress(ConfigurationManager.AppSettings["senderaddress"], ConfigurationManager.AppSettings["senderaddress"]));
for (int i = 0; i < to.Count; i++)
{
message.Bcc.Add(new MailboxAddress(string.Empty,to[i]));
//message.To.Add(new MailboxAddress(string.Empty, "xxxxx#gmail.com"));
//message.To.Add(new MailboxAddress(string.Empty, "xxxxxx#gmail.com"));
}
message.Subject = "Amazon SES Test";
message.Body = GetMessageBody().ToMessageBody();
return message;
}
private static MemoryStream GetMessageStream()
{
var stream = new MemoryStream();
GetMessage().WriteTo(stream);
return stream;
}
private void SendEmails()
{
var credentals = new BasicAWSCredentials(ConfigurationManager.AppSettings["AccessKey"], ConfigurationManager.AppSettings["SecretAccessKey"]);
using (var client = new AmazonSimpleEmailServiceClient(credentals, RegionEndpoint.USEast1))
{
var sendRequest = new SendRawEmailRequest { RawMessage = new RawMessage(GetMessageStream()) };
try
{
var response = client.SendRawEmail(sendRequest);
}
catch (Exception e)
{
}
}
}
You need at least 1 email address in to 'To' field. Perhaps send the email to yourself and add the others as a BCC.
Instead of using the BCC header to send out bulk mails, IMHO you should send one email with a clear "to" header to each of the recipients. So instead looping through recipients and adding them to BCC you should rather create and send a message there.

Categories