2

I'm sending e-mails with System.Net.Mail.SmtpClient. The problem is that when I add an attachment to the MailMessage the received e-mails subject and the attached filenames decoding goes wrong.

When the subject or attachment name string contains only one special character like 'ü' the decoding fails, but if it contains more like 'ÍŰÁÉÚŐÓÜÖíűáéúőóüö' the decoding is ok.

Code to reproduce the problem:

var message = new MailMessage();
message.From = new MailAddress("test@test.com", "test");
message.To.Add(new MailAddress("test@test.com", "test"));
message.Subject = "Teszt üzenet";
message.Body = "Teszt üzenet";

// if i remove the line below en/decoding is ok
message.Attachments.Add(new Attachment(@"document.pdf") { Name = "fájl.pdf" });

using (var smtp = new SmtpClient() {
  DeliveryFormat = SmtpDeliveryFormat.International,
  DeliveryMethod = SmtpDeliveryMethod.Network,
  EnableSsl = Settings.UseSsl,
  Host = Settings.Host,
  Port = Settings.Port,
  UseDefaultCredentials = false,
  Credentials = new NetworkCredential(Settings.Username, Settings.Password)
}) {
  smtp.Send(message);
}

Setting message.SubjectEcoding and others doesn't seem to matter. The default is UTF-8 which i would use anyway.

I'm using gmail smtp for testing and this is part of a webapi 2 project.

darph
  • 29
  • 5
  • This probably has to do with the SMTP server, the receiving server or the mail client. I used your code (exactly) to send to a mailtrap.io mailbox, and it worked fine. – gnud May 16 '18 at 14:09
  • it should help to dump smtp session as described, for example, [here](http://www.systemnetmail.com/faq/4.10.aspx#4.10) – max630 May 16 '18 at 17:28
  • @gnud - thanks! This mailtrap.io stuff is great! I've tested sending mail thru their smtp, then forward to local outlook client and encoding was ok, while the same code with gmail smtp settings resulted in faulty decoding so this must be some gmail specific stuff. Please add your comment as an answer so i can mark it as a solution! – darph May 16 '18 at 21:15
  • @max630 - thanks! this logging is great, i didn't know such thing existed – darph May 16 '18 at 21:16

1 Answers1

0

The reason for the encoding/decoding failures was the gmail smtp.

darph
  • 29
  • 5