Here is my method for testing 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();
}
}
In php your hands are free and can make headers and mail structure very well,
Take a look at this example :
http://www.sanwebe.com/2011/01/send-php-mail-with-attachment
I want to do something like php in .Net and make my mail structure using with headers.
But errors in my codes are about these lines :
mail.Headers.Add("", "blo blo blo");
And Error :
The name part can not be empty...
How can i bypass that error and create every peace of my mail using with headers :
such as -> Body - Attachments - etc
I think this falls under RFC 2822, https://tools.ietf.org/html/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.
Seems like empty string is not allowed.
Using a 5 hour SMTP timeout is excessive. A range 5 seconds to 60 seconds is more reasonable.