如何发送本地化电子邮件

时间:2012-05-22 10:32:27

标签: c# localization

我想向用户发送本地化的电子邮件,但似乎从资源文件中检索的文本是基于我的文化。

SmtpClient client = new SmtpClient();
client.Host = "xxx.xxx.xxx";
client.Credentials = new NetworkCredential("name", "password");
MailMessage mm = new MailMessage();
mm.Sender = new MailAddress("xxx@xxx.com");
mm.From = new MailAddress("xxx@xxx.com");
mm.To.Add(new MailAddress(email));
mm.Subject = Localization.EmailUserActiveTitle;
mm.Body = "<div><h3>" + Localization.EmailUserActiveBodyPart1 + "</h3></div></br>" +
            "<div>" + Localization.EmailUserActiveBodyPart2 + "</div>" +
            "<div><b>" + content + "</b></div></br>" +
            "<div>" + Localization.EmailUserActiveBodyPart3 + "</div>" +
            "<div>" + Localization.EmailUserActiveBodyPart4 + "</div>";
mm.IsBodyHtml = true;
mm.Priority = MailPriority.Normal;
client.Send(mm);

但是当我检索Localization.EmailUserActiveBodyPart1时,它会根据我当前的文化进行本地化。

如何检索指定的文化资源文件?

1 个答案:

答案 0 :(得分:1)

ResourceManager使用Thread.CurrentThread.CurrentUICulture属性来确定要加载的资源的本地化语言版本。

因此,如果您想强制使用特定语言进行本地化(例如与您要向其发送电子邮件的用户相关联的语言首选项),那么只需在代码之前执行此操作:

var previousUICulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR"); // Replace with the relevant culture name for your user

在你的代码之后清理*:

Thread.CurrentThread.CurrentUICulture = previousUICulture;

*显然这不是一次可靠的清理工作。在finally中使用IDisposable阻止或包含此语言切换功能并使用using块会阻止您的代码在发生故障时以随机语言运行,但这超出了这一点