Umlauts in iPhone v-card - c#

I need to send e-mails to iPhone users with .vcf files for adding contacts. The problem is that contact name has umlaut symbols and they displays incorrectly.
Also I noticed that if I send the same text in the body of email or open composed vcf file in notepad the symbols displays correctly.
public void SendEmail(string to, string subject, string body)
{
using (var message = new MailMessage())
{
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.SubjectEncoding = Encoding.UTF8;
message.BodyEncoding = Encoding.UTF8;
message.HeadersEncoding = Encoding.UTF8;
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(body)))
{
string attachamentName = string.Format("{0}.vcf", subject);
Attachment attachment = new Attachment(stream, MediaTypeNames.Application.Octet) { Name = attachamentName };
attachment.ContentDisposition.DispositionType = DispositionTypeNames.Attachment;
message.Attachments.Add(attachment);
using (var client = new SmtpClient())
{
client.Send(message);
}
}
}
}
Can someone please help me?
UPDATE: Sorry, have to edit code sample, I've accidentally submit the wrong one.
UPDATE #2: It looks like it is not only iPhone problem, Outlook also does not recognize umlauts.
UPDATE #3: Added full code for sending e-mail

Try changing to:
BEGIN:VCARD\r\nVERSION:2.1\r\nN;CHARSET=LATIN1:Fältskog;Agnetha\r\nFN;CHARSET=LATIN1:Agnetha Fältskog\r\nORG:\r\nTITLE:\r\nEND:VCARD
Just from reading elsewhere - looks like the format needs this CHARSET tag on each field, and seems that either LATIN1 or iso-8859-1 character sets, rather than utf-8 need to be specified for these.

Try to change
VERSION:2.1\r\n
to
VERSION:3.0\r\n
After that you don't need CHARSET-Tags for fields with umlauts,
it should work as expected.

Related

Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email .

The official Gmail API documentation is horrendous. Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email.
It would be a three step process -
Define an HTML template which which describes how your mail should be presented.
Write a small c# code to replace all place holders like your form fields , user name, etc.
private string createEmailBody(string userName, string title, string message)
{
string body = string.Empty;
//using streamreader for reading my htmltemplate
using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName); //replacing the required things
body = body.Replace("{Title}", title);
body = body.Replace("{message}", message);
//// Instead of message add you own parameters.
return body;
}
When form is submitted, call step 2 code first. Then use it's output to set mail body.
Code would be something like -
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
/// This mail from can just be a display only mail I'd
string emailFrom = "no-reply#gmail.com";
string subject = "your subject";
string body = createEmailBody();
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
/// Add more to IDs if you want to send it to multiple people.
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// This is required to keep formatting of your html contemts
/// Add attachments if you want, this is optional
mail.Attachments.Add(new Attachment(yourfilepath));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(your-smtp-account-email, your-smtp-account-password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Refer this link for working example
https://www.c-sharpcorner.com/UploadFile/33b051/sending-mail-with-html-template/
EDIT: For using GMail API
Using GMAIL APIs you will need two nuget packages:
1. Install-Package Google.Apis.Gmail.v1
2. Install-Package AE.Net.Mail
Code is very similar to what we have for normal SMTP mail send. It is explained at: http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/

Hiding Inline Attachments from Recipient

I'm trying to generate a MailMessage and set various attachments to it. The inline attachments always appear to the recipient as either jpeg, png, or other image files if I add them as an Attachment type. The code I used for this approach:
var mailMessage = new MailMessage();
// Set To, From, Body, Subject, etc.
foreach(var att in self.Attachments) {
byte[] content = att.GetBytes();
var attachment = new Attachment(new MemoryStream(content), att.Name);
if(att.IsInline){
attachment.ContentId = att.Name;
attachment.ContentDisposition.Inline = true;
attachment.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
}
mailMessage.Attachments.Add(attachment)
}
var view = AlternateView.CreateAlternateViewFromString(mailMessage.Body, Encoding.UTF8, MediaTypeNames.Text.Html);
mailMessage.AlternateViews.Add(view);
If I add them as a LinkedResource then they show up as dat files in the attachment section. Code:
var mailMessage = new MailMessage();
// Set To, From, Body, Subject, etc.
var view = AlternateView.CreateAlternateViewFromString(mailMessage.Body, Encoding.UTF8, MediaTypeNames.Text.Html);
foreach(var att in self.Attachments) {
byte[] content = att.GetBytes();
if(att.IsInline) {
var inline = new LinkedResource(new MemoryStream(content), att.ContentType);
inline.ContentId = att.Name;
view.LinkedResources.Add(inline);
}
else {
mailMessage.Attachments.Add(new Attachment(new MemoryStream(content),
att.Name));
}
}
mailMessage.AlternateViews.Add(view);
Both approaches generate the correct email and inject the inline attachments into the body of the email. Neither show the inline attachments in the attachment section while previewing the email in outlook. Both show the inline attachments when receiving the email in outlook. I have made sure that the outlook settings are as follows in the mail section of Outlook Options: "Compose message in this format: HTML" and "When sending messages in Rich Text format to Internet recipients: Convert to HTML format".
Any suggestions would be greatly appreciated as the additional attachments are creating confusion to the end users.
I stumbled on this question having had the same problem, and for me the solution was to add the type of the attachment.
Change this
var inline = new LinkedResource("logo.png");
To this
var inline = new LinkedResource("logo.png", "image/png");
It looks as if you are already doing this, but perhaps that wasn't working as intended?
Thanks to this page for providing the answer:
https://www.codeproject.com/articles/31897/embed-an-image-in-email-using-asp-net

Cyrillic subject encoding in c# mails

I`m trying to send e-mail via c# and use the following code:
public static bool SendSMTPMail(string smtphost, int smtpport, string smtplogin, string smtppassword, string from, string to, string subject, string body, bool isHtml)
{
try
{
using (MailMessage message = new MailMessage(from, to, subject, body))
{
message.IsBodyHtml = isHtml;
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
SmtpClient mailClient = new SmtpClient(smtphost, smtpport);
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Credentials = new NetworkCredential(smtplogin, smtppassword);
mailClient.Send(message);
return true;
}
}
catch
{
return false;
}
}
It works fine, when mails recieved in Windows, but when user trying to read them in MacOS - subject header is in wrong encoding.
If I set subject encoding to Windows-1251, it works good, but only for cyrillic subjects, and I`m going to send asian too...
How can I send emails using pure Unicode?
And the second question - if I`ll add any attachment to the mail, it will be added with extra files - "filelist.xml" and "header.htm".
How to get rid of them?
Thaks!
The better solution is to create a structure or an automation that impse to system different encodings following the receiver format:
generic utf8
iso xxx for cyrillic
iso xxy for chinese
and so on.
About my second question found this, and it`s helps
For encoding issues I choose to send mails with subjects in english only...

C# mvc2 create an plain text email in code

I need to create an plain-test email in code. This is required because the information in the email will be read by an application.
I've created the following value in an constant string as setup for my email. These are the fields that I want to be in the e-mail because the application requires them.
public static string TestMail = #"
[Begin Message]
Name = {0}
Phone = {1}
Postalcode = {2}
HomeNumber = {3}
[End message]";
When sending the email using the code below, the application which needs to read information from the email, receives it as following;
=0D=0A        [Begin Message]=0D=0A        Name =3D nam=
e=0D=0A        Phone =3D 0612345678=0D=0A        Postalcode =3D =
1234ab=0D=0A        HomeNumber =3D 5=0D=0A        [End messa=
ge]=0D=0A       =20
The code I used to send this email is as following;
var mailBody = String.Format(Constants.TestMail, name, phone, postalCode, homeNumber);
var mail = new MailMessage
{
Subject = Constants.Subject,
Body = mailBody,
IsBodyHtml = false,
};
mail.To.Add(receveiver);
var smtpClient = new SmtpClient();
smtpClient.Send(mail);
This isn't the result I expected and after digging around a bit I found out that the problem lied in the fact that it still seems to be an HTML-email while I need it to be plain-text. While reading about this problem I found this example in VB.net on the internet. So i modified the constant to the one below;
public static string TestMail = #"[Begin message]\r\nName = {0}\r\nPhone = {1}\r\nPostalcode = {2}\r\nHomenumber = {3}\r\n[End message]";
Then I used the following code to create and sent the email to my client (testing in outlook)
var mail = new MailMessage
{
Subject = Constants.Subject,
};
var alternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailBody, Encoding.ASCII,"text/plain");
mail.AlternateViews.Add(alternateView);
mail.To.Add(receveiver);
var smtpClient = new SmtpClient();
smtpClient.Send(mail);
After running this code i'm receiving an email in my outlook (can't test the application at the moment) containing the content below;
[Start message]\r\nName = John\r\nPhone = 0612345678\r\nPostalcode = 1234ab\r\nHomeNumber = 5\r\n[End Message]
The last result doesn't seem an plain-text email to me. Is it just outlook 2007 having problems to show the email? Or am I still missing something? I hope someone can help me out here and can tell me what's going wrong.
You should remove # character because then it will correctly handle escape characters. If you have # then all escape characters are treated as a plain text instead of new line etc.

How to send email in richtext format to Outlook?

It works great to send emails (to Outlook) in HTML format by assigning the text/html content type string like so:
using (MailMessage message = new MailMessage())
{
message.From = new MailAddress("--#---.com");
message.ReplyTo = new MailAddress("--#---.com");
message.To.Add(new MailAddress("---#---.com"));
message.Subject = "This subject";
message.Body = "This content is in plain text";
message.IsBodyHtml = false;
string bodyHtml = "<p>This is the HTML <strong>content</strong>.</p>";
using (AlternateView altView = AlternateView.CreateAlternateViewFromString(bodyHtml,
new ContentType(MediaTypeNames.Text.Html)))
{
message.AlternateViews.Add(altView);
SmtpClient smtp = new SmtpClient(smtpAddress);
smtp.Send(message);
}
}
The email is correctly recognized as HTML in Outlook (2003).
But if I try rich text:
MediaTypeNames.RichText;
Outlook doesn't detect this, it falls back to plain text.
How do I send email in rich text format?
The bottom line is, you can't do this easily using System.Net.Mail.
The rich text in Outlook is sent as a winmail.dat file in the SMTP world (outside of Exchange).
The winmail.dat file is a TNEF message. So, you would need to create your richtext inside of the winmail.dat file (formatted to TNEF rules).
However, that's not all. Outlook uses a special version of compressed RTF, so, you would also need to compress your RTF down, before it's added to the winmail.dat file.
The bottom line, is this is difficult to do, and unless the client really, really needs this functionality, I would rethink it.
This isn't something you can do with a few lines of code in .NET.
You can also achieve this by adding another alternate view before your calendar view as below:
var body = AlternateView.CreateAlternateViewFromString(bodyHtml, new System.Net.Mime.ContentType("text/html"));
mailMessage.AlternateViews.Add(body);
This worked for me..
public void sendUsersMail(string recipientMailId, string ccMailList, string body, string subject)
{
try
{
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress("norepl#xyz.com", "Tracker Tool");
Msg.To.Add(recipientMailId);
if (ccMailList != "")
Msg.CC.Add(ccMailList);
Msg.Subject = subject;
var AltBody = AlternateView.CreateAlternateViewFromString(body, new System.Net.Mime.ContentType("text/html"));
Msg.AlternateViews.Add(AltBody);
Msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("mail.xyz.com");
smtp.Send(Msg);
smtp.Dispose();
}
catch (Exception ex)
{
}
}

Categories

Resources