Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 特殊字符,例如@/防止在电子邮件中使用换行符_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 特殊字符,例如@/防止在电子邮件中使用换行符

C# 特殊字符,例如@/防止在电子邮件中使用换行符,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个ASP.NET MVC应用程序,使用System.NET.Mail类发送电子邮件 SmtpClient client = new SmtpClient(AppConstants.SmptHostName); client.Credentials = new NetworkCredential(AppConstants.SmptUsername, AppConstants.SmptPassword); MailAddress from = new MailAddress(emailSett

我有一个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
我的问题出现在值中有特殊字符,特别是正斜杠/或at符号@时。不是每个字段:值组合在单独的行上,而是在1行上。例如

Field1:Value1
Field2:Value2
Field3:Different/Value Field4:Extra / LongValue Field5:PeanutsAreGood
Field6:Another Value
电子邮件将以“纯文本”形式发送。我不能对特殊字符进行编码,也不能对正斜杠等字符进行转义


我目前唯一的解决方案是用空格替换字符,但是这会破坏功能。有其他解决方案吗?

1解决方案可以使用
message以HTML格式发送电子邮件。IsBodyHtml=true
1解决方案可以使用
message.IsBodyHtml=true
以HTML格式发送电子邮件。您可以这样做吗?

 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();
    }
类程序
{
静态void Main(字符串[]参数)
{
SmtpClient=新的SmtpClient(“smtp.gmail.com”);
client.enablesl=true;
client.Port=587;
client.Credentials=newnetworkcredential(“someEmail”、“somePass”);
MailAddress from=新的邮寄地址(“发件人”、“姓名”);
邮寄地址至=新邮寄地址(“toEmail”);
MailMessage=新的MailMessage(从,到);
字符串字段名=@“$%^^字段”;
字符串字段值=@“测试值)(/%”;
string string1=string.Format(“{0}:{1}{2}”,fieldName,fieldValue,Environment.NewLine);
字符串fieldName2=@”anotheremail@mail.com/\";
字符串fieldValue2=@“测试值2”;
string string2=string.Format(“{0}:{1}{2}”,fieldName2,fieldValue2,Environment.NewLine);
List messageBody=新列表();
messageBody.Add(string1);
messageBody.Add(string2);
foreach(messageBody中的字符串str)
{
message.Body+=str;
}
message.Subject=“新应用程序”;
客户端。发送(消息);
//非静态类cls=新的非静态类();
//cls.GetVariable();
}

你能这样做吗?

 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();
    }
类程序
{
静态void Main(字符串[]参数)
{
SmtpClient=新的SmtpClient(“smtp.gmail.com”);
client.enablesl=true;
client.Port=587;
client.Credentials=newnetworkcredential(“someEmail”、“somePass”);
MailAddress from=新的邮寄地址(“发件人”、“姓名”);
邮寄地址至=新邮寄地址(“toEmail”);
MailMessage=新的MailMessage(从,到);
字符串字段名=@“$%^^字段”;
字符串字段值=@“测试值)(/%”;
string string1=string.Format(“{0}:{1}{2}”,fieldName,fieldValue,Environment.NewLine);
字符串fieldName2=@”anotheremail@mail.com/\";
字符串fieldValue2=@“测试值2”;
string string2=string.Format(“{0}:{1}{2}”,fieldName2,fieldValue2,Environment.NewLine);
List messageBody=新列表();
messageBody.Add(string1);
messageBody.Add(string2);
foreach(messageBody中的字符串str)
{
message.Body+=str;
}
message.Subject=“新应用程序”;
客户端。发送(消息);
//非静态类cls=新的非静态类();
//cls.GetVariable();
}


在这种情况下,我无法发送HTML编码的电子邮件。您的代码对我来说很好,您可以发布GetApplicationMail()方法的代码吗?我的代码对我来说也很好。这就是我发布此问题的原因。;-)我已使用GetApplicationMail方法的简化版本更新了我的原始帖子。在这种情况下,我无法发送HTML编码的电子邮件。我觉得您的代码不错,您能发布GetApplicationMail()的代码吗方法?我的代码对我来说也很好。这就是我发布这个问题的原因。;-)我用GetApplicationMail方法的简化版本更新了我的原始帖子。有了提供的代码,没有理由让它们保持在同一行。你可能在
GetApplicationMail
方法中出错。@MikeMcCaughan-GetApplicationMail方法只是在数据库字段列表中循环,并针对每个字段而不是“FieldName”和“FieldValue”-您可以替换field.Property。这两个都是字符串值。原始帖子已使用GetApplicationMail方法的简化版本进行了更新。是的,没有任何说明为什么它会在同一行上。字段值的编码方式可能有问题。顺便说一句,您可以使用
StringBuilder
class而不是串联字符串;它有一个
AppendFormat()
方法,该方法采用与
String.Format
相同的参数。有了提供的代码,就没有理由让它们保持在同一行上。您可能在
GetApplicationMail
方法中出错。@Mikemcaughan-GetApplicationMail方法只是在数据库字段列表中循环,并针对每个字段“FieldName”和“FieldValue”的ead-您可以替换field.Property。这两个都是字符串值。原始帖子已使用GetApplicationMail方法的简化版本进行了更新。是的,没有任何说明为什么它会在同一行上。字段值的编码方式可能有问题。顺便说一句,您可以使用
StringBuilder
class而不是串联字符串;它有一个
AppendFormat()
方法,该方法采用与
字符串相同的参数。Format
。我尝试使用您建议的文本字符串,但没有任何区别。哇,这很奇怪,我刚刚测试了它,效果很好。您使用的是exchange电子邮件服务器还是私人电子邮件服务器。我想知道它是否有自己的解析/格式化功能?在我的测试环境中,我使用我国最大的ISP之一发送。当我上线时,我使用我的