Cyrillic subject encoding in c# mails - c#

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...

Related

Sending emails with outlook reminder

Introduction
I currently have a C# method that allows me to send emails (see below). The emails are being sent via a Microsoft Exchange server, and the emails are read using an Outlook client. I would like to add additional functionality to this method so that I can include a follow-up reminder for 24 hours after the send time. From what I have read online, I need to use Microsoft.Office.Interop.Outlook, but I haven't been able to figure out how to use it.
Current method
using System.Net.Mail;
public void SendEmail(string host, string un, string pw, string from, string to, string subject, string body)
{
MailMessage email = new MailMessage(from, to);
email.Subject = subject;
email.Body = body;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(un, pw);
client.Host = host;
client.Send(email);
}
What I Have Tried
I believe I need to use the Microsoft.Office.Interop.Outlook.MailItem class to create the email instead of the MailMessage class I have been using. However I am unsure about 2 things:
How to connect to the server and send the email. I tried using the SmtpClient class as I was before, but I can't pass it a MailItem.
I'm not sure if my code to create the reminder is correct. I haven't been able to send the message, so I also haven't been able to see if the reminder is displayed in Outlook correctly. If anyone has experience using reminders, comments on how I am creating the reminder would also be appreciated.
Here is the code I have used to create the email and reminder:
using Microsoft.Office.Interop.Outlook;
public void SendEmail(string host, string un, string pw, string from, string to, string subject, string body)
{
MailItem email = new MailItem();
email.ReminderTime = new DateTime();
email.ReminderTime = DateTime.Now;
email.ReminderTime.AddDays(1);
email.ReminderSet = true;
email.Subject = subject;
email.Body = body;
}

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/

C# having trouble in sending email with both **Plain-text** & **Html** content

I am having trouble in sending email with both Plain-text & Html content.
In Outlook 2007 the plain-text message is never displayed.
And in Gmail email client by default plain-text is displaying. I need the HTML message to be displayed as default in both Outlook & Gmail, but user can change to plain-text if such settings are made on that system.
I am using following C# code to send emails:
private void SendEmail(string server, string from, string userName, string password, int port , string recipients)
{
MailMessage message = new MailMessage(from,recipients);
message.Subject = "This email message has multiple views.";
message.From = new MailAddress(from);
message.To.Add(recipients);
message.IsBodyHtml = false;
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>This is some HTML text2</h1></body></html>", null, MediaTypeNames.Text.Html));
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("This is some plain text2", null, MediaTypeNames.Text.Plain));
SmtpClient client = new SmtpClient(server);
client.Port = port;
NetworkCredential SMTPUserInfo = new NetworkCredential(userName, password);
client.UseDefaultCredentials = false;
client.Credentials = SMTPUserInfo;
client.Send(message);
}
The problem with this code is that (I am not sure), that in Outlook 2007 the email content is displayed initially like below image (default rendering HTML body as plain text):
When I right click & choose "Display as HTML", then the email content is displays like following image (rendering HTML body):
And when I use gmail.com to see the email then email content is displayed like following image (default rendering plain text):
EDIT 1: As suggested I have made changes to my code, now I can see the HTML Email content in Gmail. But in Outlook 2007 after setting plain-text as default view I see the HTML tags as string and it does not show Plain Text which i have drafted which is slightly different from html. Also how do i test the plain text email in Gmail as now in gmail i am receiving only HTML View and i didnt find any way to see plain text view though i have clicked on view original and changed view=om to view=dom in the URL as i have read it on google somewhere.
private void SendEmail(string server, string from, string userName, string password, int port , string recipients)
{
MailMessage message = new MailMessage(from,recipients);
message.Subject = "This email message has multiple views.";
message.From = new MailAddress(from);
message.To.Add(recipients);
message.Body = "This is some plain text2";
message.IsBodyHtml = true;
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<html><head></head><body><h1>This is some HTML text2</h1></body></html>", null, MediaTypeNames.Text.Html));
//message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("This is some plain text2", null, MediaTypeNames.Text.Plain));
SmtpClient client = new SmtpClient(server);
client.Port = port;
NetworkCredential SMTPUserInfo = new NetworkCredential(userName, password);
client.UseDefaultCredentials = false;
client.Credentials = SMTPUserInfo;
client.Send(message);
}
I would try adding the html view after the plain text view.
Also, I usually set the IsBodyHtml flag to true, even when I've got both views defined.
You should set MailMessage.Body to the clear text. Then add the html text as you do via AlternateViews.
At least this is how I have always sent emails with both text and html.
The email client should automatically choose html if it's capable of showing that. It shouldn't display the 'Alternate view' as it does now.
Edit:
I would not set 'IsBodyHtml' to true, it might confuse the mail program, since it's plain text.
Also I would set Encoding to Encoding.UTF8, but I doubt that would solve this problem.
Edit2:
Just checked my mail sender code. I always set a 'message id' header, don't know if that matters:
mailMessage.Headers.Add("message-id", "<" + DateTime.Now.Ticks + "#example.com>");

Umlauts in iPhone v-card

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.

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