When i want to reply a mail via EWS like below, if I retrieve a email body in html format like below. How can I put some reply text conveniently, just at the top of the original message?
Although I can parse the html, I am wondering there is any smart way doing it. thank you
EmailMessage mesg = email.CreateReply(false).Save();
mesg.Load();
MessageBody lvMessageBody = mesg.Body;
You should be able to do this by setting the BodyPrefix property on the reply. See https://msdn.microsoft.com/EN-US/library/office/dn617213(v=exchg.150).aspx
Related
I'm working on a Teams bot and I'm trying various ways to send rich messages to users (we need clickable buttons) from my bot. I have tried adaptive cards, which are almost perfect, but noticed that on the toast popup for the message it just says "Testbot sent a card". This isn't ideal as we'd like an overview of the message to appear instead.
I noticed that when you use the weather app to send a card to another user, it has the desired effect- a short summary of the weather appears in the toast popup. Looking inside the JSON that represents that message shows that the "card" is actually an HTML attachment.
Am I able to replicate this by sending an HTML attachment? I have (naively) tried the following but it causes an exception:
Activity reply = new Activity();
reply.Type = ActivityTypes.Message;
reply.Text = "Test the toast";
reply.Conversation = new ConversationAccount()
{
Id = conversationResponse.Id
};
reply.Attachments.Add(new Attachment()
{
ContentType = "text/html",
Content = "<div>Some generated html</div>"
});
Am I just barking up the wrong tree completely?
Thanks.
Edit:
The exception I'm getting is "Operation returned an invalid status code 'BadRequest'" from within mscorlib.dll.
For an adaptive card, I can do the following, and it works fine:
reply.Attachments.Add(new Attachment()
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = JsonConvert.DeserializeObject("{Some generated JSON}")
});
When trying to send the attachment as HTML, I assume I need to do more than simply send a string as Content. I tried rendering an adaptive card to HTML and attaching the resultant RenderedAdaptiveCard object, but still got the same exception.
--
Edit 2:
When a colleague sends me a card using the weather app:
https://i.imgur.com/BlBk557.png
I get the following toast:
https://i.imgur.com/EoO8COj.png
When using that tool to send a message to the bot, I can see a message with an attachment of type text/html.
I was attempting to replicate this by sending a message from the bot to a user with an attachment of type text/html and some HTML in the content field. I now see that when I do this there is a 400 response from the serviceUrl which says
{"error":{"code":"BadArgument","message":"Unknown attachment type"}}
I think maybe the assumption I made was based on a misunderstanding. I take it that we can't have informative text on the toast for a card in the same manner as the weather app?
It seems like this is impossible. See comment on OP:
"Bots does not support an Html Attachment" - Gousia-MSFT
I'm very confused about a problem with sending multipart emails in ASP.NET. I'm using code which I'm sure has worked before, but the received email appears to have no body.
The key bit of code is this:
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainBody, null, MediaTypeNames.Text.Plain));
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html));
I can verify that plainBody is valid text and htmlBody is valid HTML. Apart from this I'm creating a very simple MailMessage with subject, from and to. I'm not setting any other properties of MailMessage and I'm sending with the standard System.Net.Mail.SmtpClient using Google credentials I've used many times before.
When the email arrives it appears to be completely empty.
If I replace the above two lines with the following (no other changes) then the HTML email arrives correctly.
message.Body = htmlBody;
message.IsBodyHtml = true;
So what could be the cause of my empty emails? I've tried simplifying the HTML right down to a single word wrapped in <b> tags so it's not related to the body content and, as I say, I've used similar code many times before with no problems. I've tested receipt of the emails in GMail and Office 365, both with same result.
Suggestions very welcome.
Why don't you break that piece of the code into two objects.
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
message.AlternateViews.Add(alternate);
Put a break point in the second line and see whats in the alternate object. Maybe that might give you a little more insight into what's going on in your code.
You cannot set the mail with two different bodies. Since one of the parts is HTML, you need to treat all mail as HTML, just appending the plaintext normally - since the plaintext has no HTML encode inside, the text will appear as plaintext.
How do I format the body of the email so it doesn't look so old school :)
I've tried sending html through it, but it just comes out as html in the body of the email.
Thanks for any help and assistance, cheers!
Set the IsBodyHtml property to true.
Including the above, I use StringBuilder's to format the contents of my emails like so:
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0} submitted the following information from the site:", txtName.Text);
I just find it's helpful in visualizing the outcome rather than doing +='s and such.
I have decided to use custom headers in email
message.Headers.Add("Origin", "My Company");
The problem is that when the email is forwarded these original headers seem to get lost, is there a way to ensure the original headers are kept during a forwarding operation.
Either this... or a more permanent way to store values in an html formatted message.
Yes this happens. to get round it read the header from the orginal email then re add them to the Fwd email to persist.
I'm sending mail from my C# Application, using the SmtpClient. Works great, but I have to decide if I want to send the mail as Plain Text or HTML. I wonder, is there a way to send both? I think that's called multipart.
I googled a bit, but most examples essentially did not use SmtpClient but composed the whole SMTP-Body themselves, which is a bit "scary", so I wonder if something is built in the .net Framework 3.0?
If not, is there any really well used/robust Third Party Library for sending e-Mails?
The MSDN Documentation seems to miss one thing though, I had to set the content type manually, but otherwise, it works like a charm :-)
MailMessage msg = new MailMessage(username, nu.email, subject, body);
msg.BodyEncoding = Encoding.UTF8;
msg.SubjectEncoding = Encoding.UTF8;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent);
htmlView.ContentType = new System.Net.Mime.ContentType("text/html");
msg.AlternateViews.Add(htmlView);
What you want to do is use the AlternateViews property on the MailMessage
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews.aspx
Just want to add that you can use defined constants MediaTypeNames.Text.Html and MediaTypeNames.Text.Plain instead of "text/html" and "text/plain", which is always a preferable way. It's in System.Net.Mime namespace.
So in the example above, it would be:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);
I'm just going to put a note here for anyone that's having problems and finds their way to this page - sometimes, Outlook SMTP servers will reconvert outgoing email. If you're seeing your plain-text body vanish entirely, and nothing but base64-encoded attachments, it might be because your server is reencoding the email. Google's SMTP server does not reencode email - try sending through there and see what happens.
On top of using AlternateViews views to add both the html and the plain text view, make sure you are not also setting the body of the Mail Message object.
// do not do this:
var msg = new MailMessage(model.From, model.To);
msg.Body = compiledHtml;
As it will make your email contain the html content in both views, overriding the alternative views.
For the people(like me) who've had the problem of gmail displaying the plaintext part instead of the html part.
Gmail seems to always display the last part in your message.
So if you've added the html part before your plain text part chances are gmail will always show the plain text variant.
To fix this you can simply add the plain text part before your html part.
For anyone who bumped into this issue you might want to check if you have preheader tags in your html.
In my html I've added a tag with a phrase of "Activate your client admin account by clicking the link.".
It seems like gmail is flagging the phrase "clicking the link" after removing it, all my emails that has been sent, are going straight to the inbox.