EWS Managed API 2.0 message encoding - c#

I have a question bout the encoding of email messages when sending via EWS Managed API 2.
I haven't been able to find any clear answer to this on the MSDN pages so I'll try you guys.
When using the standard .NET SmtpClient I can set the encoding of both the body and the subject attribute (I need utf-8) - this doesn't seem to be the case when using EWS Managed API 2.
Or, is it in fact the MimeContent attribute of the EmailMessage that is used for this and if so, how do I do the same thing for the subject of the email message?
For the body - is this the correct way to do it?
EmailMessage.MimeContent =
new MimeContent("utf-8", Encoding.UTF8.GetBytes("<body text html or plain text>"));
And then I don't set the EmailMessage.Body attribute or?

Never mind, it appears that I don't have the specify utf-8. I have now tried to set the body attribute with both text and HTML that included special characters and in both cases the result looked correct in the mail.
EmailMessage.Body = new MessageBody(BodyType.HTML, "<html content>");
EmailMessage.Body = new MessageBody(BodyType.Text, "plain text content>");

Related

C# MailKit HTML email received as plain text

I have a complex email template that contains many div, section and other HTML elements. The HTML template has reference to CSS(uploaded to server). I am using the below code to send HTML email via MailKit :
var message = new MimeMessage();
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = File.ReadAllText(pathToHtmlFIle);
message.Body = bodyBuilder.ToMessageBody();
client.Send(message);
But the client only receives everything in plain-text, no colors, no formatting. Is this the expected result ?
I think you need to use either inline CSS or CSS embedded in the head section. Since most webmail clients block links to external stylesheets, it is rare to see this method employed in an email.
After struggling a lot with this topic, I finally figure out the error was the double quotes inside the html...Use notepad++ to replace (quick launched with ctrl+f) all " for '. Holy remedy, after only receiving plain/text I finally received the text/html.
PD: Do not use bootstrap nor try to link an external source 'cause mail clients block external css providers. Use strictly style attributes for all tags.

Why at sign will be replaced as question mark in sms text message?

I am working on a C# application that will send SMS message to customer by using smtp service. Currently, the at sign will be replaced with question mark both in message body and message subject. I try many kinds of encoding such as unicode, UTF-8, UCS-2, ASCII but either can work well. Does anyone know how to avoid it?
var mailMessage = new MailMessage
{
IsBodyHtml = true,
SubjectEncoding = Encoding.Unicode,
Subject = "##" + subject + "##",
BodyEncoding = Encoding.UTF8,
Body = messageBody ?? "",
From = new MailAddress(from, "My Name"),
};
using (var smtpClient = new SmtpClient())
{
using (mailMessage)
{
smtpClient.Send(mailMessage);
}
}
...
Your code most likely is not to blame; and is most likely a true case of Lost in Translation
The encoding used by the majority of the cell-phones in the world for SMS is GSM 03.38 which is not something found outside of the phones and the respective carriers.
The standard encoding for email is ASCII with optional UTF-8 support.
The problem you are having is the translation between the 2 standards when you cross the bridge from SMTP (email) to SMS, which is not part of your code. It is most likely a free service being provided by the phone carrier. This is something that most likely will not get fixed by them as there is no money in it.
It also appears to have been known for quite some time. If we go back 5 years ago we can find this SO post: converting Twilio sms body to a string - encoding error for # symbol which sounds mighty similar to what you are experiencing.
So, the only real possible fix is going to leave the realm of using SMTP to send text messages. There are many SMS Gateway services that you can use to send the messages via relatively easy API's. They may cost money but as they say; you get what you pay for

Is there matching property for MailMessage Subject like for MailMessage Body (IsBodyHtml)?

I'm using SmtpClient for sending mails in project and got stuck on mail formatting. Actually, I'm trying to make subject field able to render HTML but without success. Tried with setting SubjectEncoding property of MailMessage to "System.Text.Encoding.UTF8", setting AlternateView on subject with text/html property but nothing was helpful. As I colud see, there is
**IsBodyHtml = true;**
property which applied on MailMessage body makes it able to render HTML elements. Even with BodyEncoding set to Encoding.UTF8, without this "IsBodyHtml = true;", body of mail is not able render HTML so I think that it's key. But there is no built in such property for subject field. Is there another way to make subject field able to render HTML elements?
P.S: I want to add unicode character (ex. ☑) to subject field to mark email (ex. as good, as bad etc.).

MailMessage is adding two dots for one dot when email is opened in Outlook or other clients

I am generating and sending email using C#.
The mail message is html formatted, and everything appears fine just before the Send method is called i.e. there is only a single dot just before aspx in the href URL, but when I open the sent email in Outlook or any other email client, it shows a double dot for a href as in code below.
<a href='http://localhost/xyz/invitation..aspx?invitecode=92EFB482-1792-4BC6-9507-70D2E3F06FE0'>Click Here </a>
My question: Why would this be happening, and is there any way to resolve this problem like some special encoding for MailMessage.BodyEncoding ? I am using the default encoding (ASCII) for MailMessage.BodyEncoding.
Try using UTF8 encoding. This should work
mail.BodyEncoding = System.Text.Encoding.UTF8;

Example of creating a draft with an attachment in C#?

I've written a test application in C# that creates a draft message using the new Gmail API. It works fine when the message has no attachment.
I'm moving from the IMAP API and have used the MailBee.NET components with that API. The MailBee.NET components includes a class that produces an RFC 2822 message, so I've re-used this and have Base64-encoded the message and have assigned to the "Raw" property as described here:
https://developers.google.com/gmail/api/guides/drafts
MailMessage msg = new MailMessage();
msg.Subject = "test!";
msg.BodyPlainText = "Test content";
msg.Attachments.Add(#"D:\Trace.log", "Trace.log", Guid.NewGuid().ToString(), null, null, NewAttachmentOptions.Inline, MailTransferEncoding.Base64);
Message m = new Message();
m.Raw = Convert.ToBase64String(msg.GetMessageRawData());
Draft d = new Draft();
d.Message = m;
d = gs.Users.Drafts.Create(d, "me").Execute();
It works fine when no attachment is added, but fails with a 500 response when one is added:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "Backend Error"
}
],
"code": 500,
"message": "Backend Error"
}
}
Could somebody please provide an example of how to do this using the .NET API? The example on the API page is very barebones and doesn't really give much in the way of useful information and the documentation isn't great. It would probably be best to use the Message / MessagePart / MessagePartBody classes included with the .NET Client, however I can't find any clear guidance or examples on their use so don't know where to begin.
Questions:
1) Can anybody provide some example code of creating a draft message with an attachment using the classes within the .NET Client?
2) Is it possible to attach more than one file? The documentation refers to a single file throughout and the Multipart guidance refers to exactly two parts: metadata and attachment.
Providing a sample "raw" field that you're uploading would definitely be helpful to debug (either base64 encoded or just directly).
However this sounds related to:
GMail API : unable to add an attachment in a draft
also about this:
m.Raw = Convert.ToBase64String(msg.GetMessageRawData());
you want to make sure you're using "web safe" (aka "url safe") base64 encoding alphabet from https://www.rfc-editor.org/rfc/rfc4648#section-5
as it says in the docs at the URL you mentioned:
"""
Your application can create drafts using the drafts.create method. The general process is to:
Create a MIME message that complies with RFC 2822.
Convert the message to a URL-safe base64-encoded string.
Create a draft, setting the value of the drafts.message.raw field to the encoded string.
"""
Google APIs use the
Much like for the poster of the other question GmailGuy referred to, this has magically started working overnight. So it must've been a Gmail-side problem after all.
Regarding:
m.Raw = Convert.ToBase64String(msg.GetMessageRawData());
Thanks for the heads-up on this; I had actually encoded it previously but while trying 20 different things to get things working I removed it and forgot to add it back in!
Also, to confirm: yes, you're able to add more than one attachment when you use the raw message approach.

Categories

Resources