c# MailMessage 自定义 headers - 使用 System.Net.Mail;
c# MailMessage custom headers - using System.Net.Mail;
这是我测试 MailMessage headers 的方法:
private void Send_Email(string smtp_server, int port, string display_name, string from, string to, string subject, string html_body, string sender_email, string sender_password)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from, display_name, Encoding.UTF8);
mail.To.Add(to);
mail.SubjectEncoding = Encoding.UTF8;
mail.Subject = subject;
mail.Headers.Add("Reply-To", "Reply-To__ <" + from + ">");
mail.Headers.Add("Sender", from);
mail.Headers.Add("Return-Path", from);
mail.Headers.Add("MIME-Version", "1.0");
string boundary = Guid.NewGuid().ToString();
mail.Headers.Add("Content-Type", "multipart/mixed; boundary=--" + boundary);
//mail.Headers.Add("", Environment.NewLine);
//mail.Headers.Add("", Environment.NewLine);
//mail.Headers.Add("", "--" + boundary);
//mail.Headers.Add("Content-Type", "text/html; charset=utf-8");
//mail.Headers.Add("Content-Transfer-Encoding", "base64");
//mail.Headers.Add("", Environment.NewLine);
//var bytes = Encoding.UTF8.GetBytes(html_body);
//var base64 = Convert.ToBase64String(bytes);
//mail.Headers.Add("", base64.ToString());
//mail.Headers.Add("", "--" + boundary);
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = true;
//smtp.Credentials = new System.Net.NetworkCredential(sender_email, sender_password);
smtp.Host = smtp_server;
smtp.Port = port;
smtp.Timeout = 60000;
smtp.SendCompleted += (s, e) =>
{
if (e.Cancelled)
{
MessageBox.Show("sending email was canceled");
}
if (e.Error != null)
{
MessageBox.Show("sending email was failed -> Error : " + e.Error.ToString());
}
else
{
MessageBox.Show("email was sent successfully");
}
mail.Dispose();
};
try
{
smtp.SendAsync(mail, null);
}
catch (System.Net.Mail.SmtpException exp)
{
MessageBox.Show("sending email was failed, SmtpException -> Error : " + exp.ToString());
mail.Dispose();
}
}
在php你的手是自由的,可以很好地制作headers和邮件结构,
看看这个例子:
http://www.sanwebe.com/2011/01/send-php-mail-with-attachment
我想在 .Net 中做类似 php 的事情,并使用 headers.
制作我的邮件结构
但是我的代码中的错误与这些行有关:
mail.Headers.Add("", "blo blo blo");
和错误:
The name part can not be empty...
我如何绕过该错误并使用 headers :
例如 -> Body - 附件 - 等
回答我的问题后,我会弄清楚如何组合多个 Content-Types :
例如 -> html - plain/text - octet - 等等
我认为这属于 RFC 2822,https://www.rfc-editor.org/rfc/rfc2822#section-2.2:
2.2. Header Fields
Header fields are lines composed of a field name, followed by a
colon (":"), followed by a field body, and terminated by CRLF. A
field name MUST be composed of printable US-ASCII characters (i.e.,
characters that have values between 33 and 126, inclusive), except
colon. A field body may be composed of any US-ASCII characters,
except for CR and LF. However, a field body may contain CRLF when
used in header "folding" and "unfolding" as described in section
2.2.3. All field bodies MUST conform to the syntax described in sections 3 and 4 of this standard.
似乎不允许空字符串。
使用 5 小时的 SMTP 超时时间过长。 5秒到60秒比较合理
来自MSDN:
The following list of mail headers should not be added using the
Headers property and any values set for these headers using the
Headers property will be discarded or overwritten when the message is
sent:
Bcc
Cc
Content-ID
Content-Location
Content-Transfer-Encoding
Content-Type
Date
From
Importance
MIME-Version
Priority
Reply-To
Sender
To
X-Priority
这是我测试 MailMessage headers 的方法:
private void Send_Email(string smtp_server, int port, string display_name, string from, string to, string subject, string html_body, string sender_email, string sender_password)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from, display_name, Encoding.UTF8);
mail.To.Add(to);
mail.SubjectEncoding = Encoding.UTF8;
mail.Subject = subject;
mail.Headers.Add("Reply-To", "Reply-To__ <" + from + ">");
mail.Headers.Add("Sender", from);
mail.Headers.Add("Return-Path", from);
mail.Headers.Add("MIME-Version", "1.0");
string boundary = Guid.NewGuid().ToString();
mail.Headers.Add("Content-Type", "multipart/mixed; boundary=--" + boundary);
//mail.Headers.Add("", Environment.NewLine);
//mail.Headers.Add("", Environment.NewLine);
//mail.Headers.Add("", "--" + boundary);
//mail.Headers.Add("Content-Type", "text/html; charset=utf-8");
//mail.Headers.Add("Content-Transfer-Encoding", "base64");
//mail.Headers.Add("", Environment.NewLine);
//var bytes = Encoding.UTF8.GetBytes(html_body);
//var base64 = Convert.ToBase64String(bytes);
//mail.Headers.Add("", base64.ToString());
//mail.Headers.Add("", "--" + boundary);
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = true;
//smtp.Credentials = new System.Net.NetworkCredential(sender_email, sender_password);
smtp.Host = smtp_server;
smtp.Port = port;
smtp.Timeout = 60000;
smtp.SendCompleted += (s, e) =>
{
if (e.Cancelled)
{
MessageBox.Show("sending email was canceled");
}
if (e.Error != null)
{
MessageBox.Show("sending email was failed -> Error : " + e.Error.ToString());
}
else
{
MessageBox.Show("email was sent successfully");
}
mail.Dispose();
};
try
{
smtp.SendAsync(mail, null);
}
catch (System.Net.Mail.SmtpException exp)
{
MessageBox.Show("sending email was failed, SmtpException -> Error : " + exp.ToString());
mail.Dispose();
}
}
在php你的手是自由的,可以很好地制作headers和邮件结构,
看看这个例子:
http://www.sanwebe.com/2011/01/send-php-mail-with-attachment
我想在 .Net 中做类似 php 的事情,并使用 headers.
制作我的邮件结构
但是我的代码中的错误与这些行有关:
mail.Headers.Add("", "blo blo blo");
和错误:
The name part can not be empty...
我如何绕过该错误并使用 headers :
例如 -> Body - 附件 - 等
回答我的问题后,我会弄清楚如何组合多个 Content-Types :
例如 -> html - plain/text - octet - 等等
我认为这属于 RFC 2822,https://www.rfc-editor.org/rfc/rfc2822#section-2.2:
2.2. Header Fields
Header fields are lines composed of a field name, followed by a colon (":"), followed by a field body, and terminated by CRLF. A field name MUST be composed of printable US-ASCII characters (i.e., characters that have values between 33 and 126, inclusive), except
colon. A field body may be composed of any US-ASCII characters,
except for CR and LF. However, a field body may contain CRLF when
used in header "folding" and "unfolding" as described in section 2.2.3. All field bodies MUST conform to the syntax described in sections 3 and 4 of this standard.
似乎不允许空字符串。
使用 5 小时的 SMTP 超时时间过长。 5秒到60秒比较合理
来自MSDN:
The following list of mail headers should not be added using the Headers property and any values set for these headers using the Headers property will be discarded or overwritten when the message is sent:
Bcc Cc Content-ID Content-Location Content-Transfer-Encoding Content-Type Date From Importance MIME-Version Priority Reply-To Sender To X-Priority