c# MailMessage custom headers - using System.Net.Mail;
Solution 1
I think this falls under 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.
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.
Solution 2
From 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
SilverLight
WEB DEVELOPER & C# PROGRAMMER ASP.NET C# JQUERY JAVASCRIPT MICROSOFT AJAX MICROSOFT SQL SERVER VISUAL STUDIO
Updated on June 04, 2022Comments
-
SilverLight almost 2 years
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
After answer to my question i will figure out how can i combine multiple Content-Types :
such as -> html - plain/text - octet - etc -
SilverLight over 8 yearsthanks for the answer, Is there a way to view mail headers and work on them? In Chrome or FF or another way?
-
Loathing over 8 yearsSeems like you are falling into the XY problem: meta.stackexchange.com/questions/66377/what-is-the-xy-problem
-
SilverLight over 8 yearsso what is the benefit of headers property man? i did n't mentioned on that MSDN link -> Don't set these!!!!
-
jsanalytics over 8 yearsMy understanding is that some headers could be set using the
Headers
property, but not all because there are specific properties for those already. -
SilverLight over 8 yearsalso some properties in MailMessage Class are Read-Only. What is this horrible class?
-
jsanalytics over 8 yearsBTW, the text i posted come from the MSDN link. So, it DOES say, Don't set these...:O)
-
jsanalytics over 8 yearsThe properties that are read-only normally must be set through the corresponding constructor.
-
SilverLight over 8 yearsSo sorry for this comment and my prev comment's question is not a stackoverflow.com question and this is why i asked it in comments!