我有一个ASP.NET MVC应用程序,我使用System.Net.Mail类发送电子邮件。
SmtpClient client = new SmtpClient(AppConstants.SmptHostName);
client.Credentials = new NetworkCredential(AppConstants.SmptUsername, AppConstants.SmptPassword);
MailAddress from = new MailAddress(emailSettings.AdminEmailAddress, emailSettings.EmailSender);
MailAddress to = new MailAddress(emailSettings.ApplicationEmailAddress);
MailMessage message = new MailMessage(from, to);
message.Body = GetApplicationEmail();
message.Subject = "New Application";
client.Send(message);
我正在构建要由其他应用程序导入的电子邮件,格式为:
String.Format("{0}:{1}{2}", "FieldName", "FieldValue", Environment.NewLine);
private string GetApplicationEmail()
{
string messageContents = "";
var fieldList = _fieldList;
foreach (var field in fieldList)
{
messageContents += String.Format("{0}:{1}{2}", field.Name, field.Value, Environment.NewLine);
}
return messageContents;
}
电子邮件将如下所示:
Field1:Value1
Field2:Value2
Field3:Value3
特殊字符特别是我的问题出现了。例如正斜杠/或符号@在值中。而不是每个字段:值组合在单独的行上,它们在1行上。例如
Field1:Value1
Field2:Value2
Field3:Different/Value Field4:Extra / LongValue Field5:PeanutsAreGood
Field6:Another Value
电子邮件将作为“纯文本”发送。我不能对特殊字符进行编码,也不能转义正斜杠等字符。
我目前唯一的解决方案是用空格替换字符,但这会破坏功能。还有其他解决方案吗?
答案 0 :(得分:0)
1解决方案可以使用message.IsBodyHtml = true
答案 1 :(得分:0)
你能做这样的事吗?
class Program
{
static void Main(string[] args)
{
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.Port = 587;
client.Credentials = new NetworkCredential("someEmail", "somePass");
MailAddress from = new MailAddress("from", "Name");
MailAddress to = new MailAddress("toEmail");
MailMessage message = new MailMessage(from, to);
string fieldName = @"$%^^TheField";
string fieldValue = @"the test value)(/%";
string string1 = String.Format("{0}:{1}{2}", fieldName, fieldValue, Environment.NewLine);
string fieldName2 = @"anotheremail@mail.com/\";
string fieldValue2 = @"\#the test value2";
string string2 = String.Format("{0}:{1}{2}", fieldName2, fieldValue2, Environment.NewLine);
List<string> messageBody = new List<string>();
messageBody.Add(string1);
messageBody.Add(string2);
foreach (string str in messageBody)
{
message.Body += str;
}
message.Subject = "New Application";
client.Send(message);
//NonStaticClass cls = new NonStaticClass();
//cls.GetVariable();
}