示例#1
0
 /// <summary>
 /// Adds an custom header to the MailMessage.
 ///	NOTE: some SMTP servers will reject improper custom headers
 ///</summary>
 /// <param name="mailheader">MailHeader instance</param>
 /// <example>
 /// <code>
 ///     MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**");
 ///		MailHeader header = new MailHeader("X-Something", "HeaderValue");
 ///		msg.AddCustomHeader(header);
 /// </code>
 /// </example>
 public void AddCustomHeader(MailHeader mailheader)
 {
     if (mailheader.Name != null && mailheader.Body != null)
     {
         CustomHeaders.Add(mailheader);
     }
 }
示例#2
0
 /// <summary>
 /// Adds an custom header to the MailMessage.
 ///	NOTE: some SMTP servers will reject improper custom headers
 ///</summary>
 /// <param name="mailheader">MailHeader instance</param>
 /// <example>
 /// <code>
 /// 	MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**");
 ///		MailHeader header = new MailHeader("X-Something", "HeaderValue");		
 ///		msg.AddCustomHeader(header);
 /// </code>
 /// </example>
 public void AddCustomHeader(MailHeader mailheader)
 {
     if (mailheader.Name != null && mailheader.Body != null)
     {
         CustomHeaders.Add(mailheader);
     }
 }
示例#3
0
        /// <summary>Returns the MailMessage as a RFC 822 formatted message</summary>
        /// <example>
        /// <code>
        ///     MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**");
        ///		msg.Body = "body";
        ///		msg.Subject = "subject";
        ///		string message = msg.ToString();
        /// </code>
        /// </example>
        /// <returns>Mailmessage as RFC 822 formatted string</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            if (!string.IsNullOrEmpty(ReplyTo.DisplayName))
            {
                sb.AppendFormat("Reply-To: \"{0}\" <{1}>\r\n", MailEncoder.ConvertHeaderToQP(ReplyTo.DisplayName, Charset), ReplyTo.Address);
            }
            else
            {
                sb.AppendFormat("Reply-To: <{0}>\r\n", ReplyTo.Address);
            }

            if (!string.IsNullOrEmpty(From.DisplayName))
            {
                sb.AppendFormat("From: \"{0}\" <{1}>\r\n", MailEncoder.ConvertHeaderToQP(From.DisplayName, Charset), From.Address);
            }
            else
            {
                sb.AppendFormat("From: <{0}>\r\n", From.Address);
            }

            sb.AppendFormat("To: {0}\r\n", CreateAddressList(To));

            if (_ccList.Count != 0)
            {
                sb.AppendFormat("CC: {0}\r\n", CreateAddressList(CC));
            }

            if (!string.IsNullOrEmpty(Subject))
            {
                StringBuilder cleanSubject = new StringBuilder(Subject);
                cleanSubject.Replace("\r\n", null);
                cleanSubject.Replace("\n", null);

                sb.AppendFormat("Subject: {0}\r\n", MailEncoder.ConvertHeaderToQP(cleanSubject.ToString(), Charset));
            }

            sb.AppendFormat("Date: {0}\r\n", DateTime.Now.ToUniversalTime().ToString("R"));
            sb.AppendFormat("{0}\r\n", X_MAILER_HEADER);

            if (Notification)
            {
                if (string.IsNullOrEmpty(ReplyTo.DisplayName))
                {
                    sb.AppendFormat("Disposition-Notification-To: <{0}>\r\n", ReplyTo.Address);
                }
                else
                {
                    sb.AppendFormat("Disposition-Notification-To: {0} <{1}>\r\n", MailEncoder.ConvertHeaderToQP(ReplyTo.DisplayName, Charset), ReplyTo.Address);
                }
            }

            if (Priority != null)
            {
                sb.AppendFormat("X-Priority: {0}\r\n", Priority);
            }

            if (CustomHeaders != null)
            {
                for (IEnumerator i = _customHeaders.GetEnumerator(); i.MoveNext();)
                {
                    MailHeader mailHeader = (MailHeader)i.Current;

                    if (mailHeader != null)
                    {
                        if (mailHeader.Name.Length >= 0 && mailHeader.Body.Length >= 0)
                        {
                            sb.AppendFormat("{0}:{1}\r\n", mailHeader.Name, MailEncoder.ConvertHeaderToQP(mailHeader.Body, Charset));
                        }
                        else
                        {
                            // TODO: Check if below is within RFC spec.
                            sb.AppendFormat("{0}:\r\n", mailHeader.Name);
                        }
                    }
                }
            }

            sb.Append("MIME-Version: 1.0\r\n");
            sb.Append(GetMessageBody());

            return(sb.ToString());
        }