>> Thought of that too, but I decided to let it be in UTF8 cause its the most >> compatible format nowadays whereas ASCII is somewhat antique. What do >you >> think? > >IIUC ASCII would have been the implicit body encoding for log4net < >1.2.12 so when looking for a backwards compatible default this would be >the most natural choice. > >I'm not sure whether setting the encoding to UTF8 triggers some sort of >different handling inside of the framweork's SMTP code even if the test >was pure ASCII. If it doesn't, then I'm fine with UTF8. > >Maybe we should send two messages with a body only containing ASCII >letters and compare the raw messages created with encoding set to ASCII >or UTF8 respectively.
Agreed. FWIW, I found this: http://w3techs.com/technologies/overview/character_encoding/all and checking a few of my Gmail's mails it seems they default to UTF8. I've just spammed test messages, this is the code: static void Main(string[] args) { MailAddress from = new MailAddress("[email protected]"); MailAddress to = new MailAddress("[email protected]"); string host = "smtp.gmail.com"; int port = 25; NetworkCredential networkCredentials = new NetworkCredential("[email protected]", "bar"); bool enableSsl = true; Encoding[] subjectEncodings = new Encoding[] { Encoding.ASCII, Encoding.UTF8 }; Encoding[] bodyEncodings = new Encoding[] { Encoding.ASCII, Encoding.UTF8 }; List<byte> content = new List<byte>(); for (byte i = 0x20; i <= 0x7F; i++) content.Add(i); string[] subjects = new string[] { "ASCII:\t" + Encoding.ASCII.GetString(content.ToArray()), "UTF8:\t" + Encoding.UTF8.GetString(content.ToArray()) }; string[] bodies = new string[] { "ASCII:\t" + Encoding.ASCII.GetString(content.ToArray()), "UTF8:\t" + Encoding.UTF8.GetString(content.ToArray()) }; foreach (Encoding subjectEncoding in subjectEncodings) { foreach (Encoding bodyEncoding in bodyEncodings) { foreach (string subject in subjects) { foreach(string body in bodies) { Console.WriteLine(@" SubjectEncoding= {0} BodyEncoding= {1} Subject= {2} Body= {3}", subjectEncoding.EncodingName, bodyEncoding.EncodingName, subject, body); SendMail(from, to, string.Format("[LOG4NET-405 Test Mail] [{0}] [{1}] {2}", subjectEncoding.EncodingName, bodyEncoding.EncodingName, subject), subjectEncoding, body, bodyEncoding, host, port, networkCredentials, enableSsl); } } } } Console.ReadKey(); } private static void SendMail(MailAddress from, MailAddress to, string subject, Encoding subjectEncoding, string body, Encoding bodyEncoding, string host, int port, NetworkCredential credentials = null, bool enableSsl = true, SmtpDeliveryMethod deliveryMethod = SmtpDeliveryMethod.Network) { try { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(TrustAllCertificatesPolicy); using (SmtpClient client = new SmtpClient(host, port)) { client.Timeout = 30 * 1000; // 10 seconds // set properties client.DeliveryMethod = deliveryMethod; client.EnableSsl = enableSsl; client.UseDefaultCredentials = (credentials == null); client.Credentials = credentials; // send message using (MailMessage msg = new MailMessage()) { msg.From = from; msg.To.Add(to); msg.Body = body; msg.BodyEncoding = bodyEncoding; msg.Subject = subject; msg.SubjectEncoding = subjectEncoding; msg.Priority = MailPriority.Normal; // create and add attachments List<Attachment> tmpAttachments = new List<Attachment>(); client.Send(msg); } } } finally { // restore default certificate validation ServicePointManager.ServerCertificateValidationCallback = null; } } private static bool TrustAllCertificatesPolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } }
