简体   繁体   English

C#mvc2在代码中创建纯文本电子邮件

[英]C# mvc2 create an plain text email in code

I need to create an plain-test email in code. 我需要在代码中创建一个普通测试电子邮件。 This is required because the information in the email will be read by an application. 这是必需的,因为应用程序将读取电子邮件中的信息。

I've created the following value in an constant string as setup for my email. 我在常量字符串中创建了以下值作为我的电子邮件的设置。 These are the fields that I want to be in the e-mail because the application requires them. 这些是我想要在电子邮件中的字段,因为应用程序需要它们。

public static string TestMail = @"
[Begin Message]
Name = {0}
Phone = {1}
Postalcode = {2}
HomeNumber = {3}
[End message]";

When sending the email using the code below, the application which needs to read information from the email, receives it as following; 当使用下面的代码发送电子邮件时,需要从电子邮件中读取信息的应用程序接收如下:

=0D=0A        [Begin Message]=0D=0A        Name =3D nam=
e=0D=0A        Phone =3D 0612345678=0D=0A        Postalcode =3D =
1234ab=0D=0A        HomeNumber =3D 5=0D=0A        [End messa=
ge]=0D=0A       =20

The code I used to send this email is as following; 我用来发送此电子邮件的代码如下:

var mailBody = String.Format(Constants.TestMail, name, phone, postalCode, homeNumber);

var mail = new MailMessage
{
    Subject = Constants.Subject,
    Body = mailBody,
    IsBodyHtml = false,
};

mail.To.Add(receveiver);

var smtpClient = new SmtpClient();
smtpClient.Send(mail);

This isn't the result I expected and after digging around a bit I found out that the problem lied in the fact that it still seems to be an HTML-email while I need it to be plain-text. 这不是我预期的结果,经过深入挖掘后我发现问题在于它似乎仍然是一个HTML电子邮件,而我需要它是纯文本。 While reading about this problem I found this example in VB.net on the internet. 在阅读有关此问题的同时,我在互联网上的VB.net中找到了这个例子。 So i modified the constant to the one below; 所以我将常数修改为下面的常数;

public static string TestMail = @"[Begin message]\r\nName = {0}\r\nPhone = {1}\r\nPostalcode = {2}\r\nHomenumber = {3}\r\n[End message]";

Then I used the following code to create and sent the email to my client (testing in outlook) 然后我使用以下代码创建并将电子邮件发送给我的客户端(在outlook中测试)

var mail = new MailMessage
{
    Subject = Constants.Subject,
};
var alternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailBody, Encoding.ASCII,"text/plain");

mail.AlternateViews.Add(alternateView);

mail.To.Add(receveiver);

var smtpClient = new SmtpClient();
smtpClient.Send(mail);

After running this code i'm receiving an email in my outlook (can't test the application at the moment) containing the content below; 运行此代码后,我在Outlook中收到一封电子邮件(目前无法测试该应用程序),其中包含以下内容;

[Start message]\r\nName = John\r\nPhone = 0612345678\r\nPostalcode = 1234ab\r\nHomeNumber = 5\r\n[End Message]

The last result doesn't seem an plain-text email to me. 最后的结果对我来说似乎不是一封纯文本邮件。 Is it just outlook 2007 having problems to show the email? 只是2007年的Outlook有问题才能显示电子邮件吗? Or am I still missing something? 还是我还缺少什么? I hope someone can help me out here and can tell me what's going wrong. 我希望有人可以帮助我,并告诉我出了什么问题。

You should remove @ character because then it will correctly handle escape characters . 您应该删除@字符,因为它将正确处理转义字符 If you have @ then all escape characters are treated as a plain text instead of new line etc. 如果你有@那么所有转义字符都被视为纯文本而不是新行等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM