Reputation: 121
I have an issue when sending an attachment file that contains Arabic characters which appear as ???? for file name. However, this issue not appear if the file name is English. I tried to use UTF-8 fro encoding but not work.
private static void GenerateAndSendMail(string subject, string body, List<string> filesFullPath = null)
{
try
{
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(MyAppConfigService.CurrentUser.Email);
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = body;
mailMessage.SubjectEncoding = Encoding.Unicode;
mailMessage.HeadersEncoding = Encoding.Unicode;
mailMessage.BodyEncoding = Encoding.Unicode;
if (filesFullPath != null && filesFullPath.Count > 0)
{
foreach (var item in filesFullPath)
{
var Attachment = new Attachment(item);
Attachment.NameEncoding = Encoding.Unicode;
mailMessage.Attachments.Add(Attachment);
}
}
//save the MailMessage to the filesystem
var filename = Path.GetTempPath() + "mymessage.eml";
mailMessage.Save(filename);
//Open the file with the default associated application registered on the local machine
Process.Start(filename);
}
catch (Exception ex)
{
throw ex;
}
}
Upvotes: 1
Views: 550
Reputation: 121
After more than 6 hours of research on internet and trying different type of Encodings such as UTF-8, UTF-16 and Unicode I found the solution.
The issue was in Attachment encoding, it was unable to encode Arabic character properly and the encoding result is wrong. The solution is by adding the line code below under NameEncoding:
Attachment.ContentType.CharSet = "UTF-8";
Upvotes: 2