MailMessage encoding on Azure - c#

I have some fairly simple code to send an email with Unicode characters in the body. When I run this in a normal MVC app there is no problem. But when running from within a WebJob the encoding is lost and the body becomes gibberish. It doesn't matter if I run the WebJob locally or deployed, same result.
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(from);
message.To.Add(new MailAddress(email));
message.Subject = subject;
message.Body = "ὠμέγα";
message.BodyEncoding = System.Text.Encoding.UTF8;

Related

C# mailing with BCC to PHP ready mail server using Custom Headers

Got an issue about C# mailing.
According to rfc format has no problem when BCC added to the custom header!
However according to MSDN BCC info that is added to custom header will be deleted!
So in this particular situation I have to send an email trough C# code to a server that accepts BCC info in the custom header only...
How can I achive that? So far what I've tried is below;
// Mail Configurations
MailMessage mail = new MailMessage();
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.None;
mail.Priority = MailPriority.Normal;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.HeadersEncoding = System.Text.Encoding.UTF8;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.BodyTransferEncoding = System.Net.Mime.TransferEncoding.Base64;
mail.From = new MailAddress(FROM, FROM, System.Text.Encoding.UTF8); // From
mail.To.Add(new MailAddress(TO, TO, System.Text.Encoding.UTF8)); // EmailTo
// mail.CC.Add(new MailAddress(CC, CC, System.Text.Encoding.UTF8)); // CC
// mail.Bcc.Add(new MailAddress(BCC, BCC, System.Text.Encoding.UTF8)); // BCC
mail.Headers.Add("BCC", BCC); // BCC 2nd method adding in headers as PHP does
mail.Subject = Subject;
mail.IsBodyHtml = true;
mail.Body = "<div>Hello World!<div>";
mail.Attachments.Add(new Attachment(FileName));
// SMTP Configurations
SmtpClient SmtpServer = new SmtpClient(SMTP, PORT);
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryFormat = SmtpDeliveryFormat.International;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new NetworkCredential(SMTPUser, SMTPPass);
// Send Mail
SmtpServer.Send(mail);
// Clear All
mail.To.Clear();
mail.Bcc.Clear();
mail.Headers.Clear();
mail.Attachments.Clear();
Edit: Depending on the comments I've also tried MailKit library.
It had no effect on BCC the code I've used is;
// Mail
var message = new MimeMessage();
message.From.Add(new MailboxAddress(System.Text.Encoding.UTF8, FROMName, FROM));
message.To.Add(new MailboxAddress(System.Text.Encoding.UTF8, TO, TO));
message.Cc.Add(new MailboxAddress(System.Text.Encoding.UTF8, CC, CC));
message.Bcc.Add(new MailboxAddress(System.Text.Encoding.UTF8, BCC, BCC));
message.Subject = Subject;
message.Priority = MessagePriority.Normal;
message.Importance = MessageImportance.Normal;
message.XPriority = XMessagePriority.Normal;
// With Body Builder
var builder = new BodyBuilder();
builder.HtmlBody = "<div> Hello World! </div>"; // Set the html version of the message text
builder.Attachments.Add(FileName); // We may also want to attach some files
message.Body = builder.ToMessageBody(); // Now we just need to set the message body and we're done
// SMTP connect
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect(SMTP, SMTPPort, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate(SMTPUser, SMTPPass);
client.Send(message);
client.Disconnect(true);
}
I'm not sure if I'm following along correctly, but it sounds like you need to send a message where the raw Bcc: header is sent in the headers to the SMTP server?
This is highly non-standard and very suspect since the whole point of the Bcc: header is that it gets stripped at send time because recipients in the Bcc: header field are meant to be BLIND Carbon Copy recipients (aka, hidden to everyone who receives the message).
That said... if you REALLY REALLY do need to include the Bcc: header in the message data that gets uploaded to the SMTP server, then you can do this:
var options = FormatOptions.Default.Clone ();
options.HiddenHeaders.Add (HeaderId.ContentLength);
options.HiddenHeaders.Remove (HeaderId.ResentBcc);
options.HiddenHeaders.Remove (HeaderId.Bcc);
options.NewLineFormat = NewLineFormat.Dos;
client.Send (options, message);
Note: Currently the Resent-Bcc and Bcc headers are not hidden in the default FormatOptions, I just added those .Remove() calls to illustrate how to control which headers get hidden vs not.
The SmtpClient.Send() methods that do not take a FormatOptions argument use an internal FormatOptions that add the Bcc, Resent-Bcc, and Content-Length headers to the hidden list.

C# SmtpServer "The operation has timed out"

I have problem in sending mail by smtp and gmail in C# winform application.
without attachment sending is successfully but when attachment is added application was thrown to System.Net.Mail.SmtpException and it's message is "The operation has timed out."
what's the problem?!
SmtpClient smtpClient = new SmtpClient();
Attachment att=new System.Net.Mail.Attachment("Path");
smtpClient.EnableSsl = true;
smtpClient.Port = 25;
smtpClient.Timeout = 20000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Credentials = new System.Net.NetworkCredential
(from#gmail.com, "password" );
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(from#gmail.com,
from,
System.Text.Encoding.UTF8);
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("to#gmail.com");
MailMessage message = new MailMessage(from, to);
message.Body = "some text";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "Subject";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Bcc.Add(bcc#gmail.com);
message.Attachments.Add(att);
try
{
smtpClient.Send(message);
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show(ex.Message); // The operation has timed out
}
Edit
i have this problem both of gmail and yahoo!
I don't see any major flaw in your code and I think GMail raised its attachment size limit to 50Mb some time ago... anyway, I suggest you to proceed with the following modifications:
Increase your timeout as much as possible, this alone could solve your problem if you are dealing with very big attachments: smtpClient.Timeout = Int32.MaxValue;.
Try to use different server port. Maybe you have other applications (Outlook, for example) using the port 25 to communicate and this creates a conflict. Switch to 465. If it doesn't work, try with 587.
Try to make a test using a different account.

Email Attachment Size Limit

I am currently using this code in C# to email a file to my hotmail address with an attachment. I ran this and it worked great when attaching a 800KB file, but when I try to attach a 12MB file it just won't do it, I put it in a try catch but there were no exceptions, like it just skipped it. The email attachment size limit for hotmail is 25MB, would this affect the email code, or is there a separate limit when doing it through code? Thanks.
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
mail.From = new MailAddress("email#hotmail.co.uk");
mail.To.Add("receiving#hotmail.co.uk");
mail.Subject = "Emailed from C#";
mail.Body = "Emailed with attachment";
Attachment attachment;
attachment = new Attachment(#"C:\file.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new NetworkCredential("email#hotmail.co.uk", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Accepted answer is Mohamed's link to bug fix question - https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=30226

setting mail header in c#

hi i need to set header content type in c#.
when i send mail from c#, i'm getting the mail with html tags. how can set content type in mail sending code?
In case that you asking how can I send email in c#:
MailMessage mail = new MailMessage();
mail.To = "me#mycompany.com";
mail.From = "you#yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
mail.IsBodyHtml = false;
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send( mail );
Source: here.
MailMessage mail = new MailMessage();
// need to set this property
mail.IsBodyHtml = false;
For more alterations, you can do with templates in your email, see
Can I set up HTML/Email Templates in
C# on ASP.NET?
Hope this helps

SmtpClient sends email to junk

I tried to send email from c# using SmtpClient.Send() but it always goes to the junk box. It works fine if I send it from Outlook. Is there anyway to solve this? Someone told me to modify the email header but I don't know how.
Thanks in advance.
Here is my code
SmtpClient client = new SmtpClient();
client.Host = "smtp.server.com";
client.Credentials = new System.Net.NetworkCredential("user", "password");
MailAddress mailFrom = new MailAddress("mymail#server.com");
MailAddress mailTo = new MailAddress("yourmail#server.com");
MailAddress mailReply = new MailAddress("mymail#server.com");
MailMessage message = new MailMessage(mailFrom, mailTo);
message.Body = "This is a test message.";
message.Subject = "test message";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(message);
a) The code sample doesn't actually use the mailReply address.
b) The problem will probably disappear when you send a more realistic message. If it doesn't then you will have to find out why the message is being marked junk, fishing a message from the spambox and looking at the headers or something like that.
Spam filters may discard messages that have invalid entries.
Try putting in valid (existing) addresses of sender, reply and from.

Categories