Пример #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 (ReplyTo.name != null && ReplyTo.name.Length != 0)
            {
                sb.Append("Reply-To: \"" + MailEncoder.ConvertHeaderToQP(ReplyTo.name, charset) + "\" <" + ReplyTo.address + ">\r\n");
            }
            else
            {
                sb.Append("Reply-To: <" + ReplyTo.address + ">\r\n");
            }

            if (from.name != null && from.name.Length != 0)
            {
                sb.Append("From: \"" + MailEncoder.ConvertHeaderToQP(from.name, charset) + "\" <" + from.address + ">\r\n");
            }
            else
            {
                sb.Append("From: <" + from.address + ">\r\n");
            }

            sb.Append("To: " + CreateAddressList(recipientList) + "\r\n");

            if (ccList.Count != 0)
            {
                sb.Append("CC: " + CreateAddressList(ccList) + "\r\n");
            }

            if (subject != null && subject.Length > 0)
            {
                StringBuilder cleanSubject = new StringBuilder(subject);
                cleanSubject.Replace("\r\n", null);
                cleanSubject.Replace("\n", null);

                sb.Append("Subject: " + MailEncoder.ConvertHeaderToQP(cleanSubject.ToString(), charset) + "\r\n");
            }

            sb.Append("Date: " + DateTime.Now.ToUniversalTime().ToString("R") + "\r\n");
            sb.Append(SmtpConfig.X_MAILER_HEADER + "\r\n");

            if (notification)
            {
                if (ReplyTo.name != null && ReplyTo.name.Length != 0)
                {
                    sb.Append("Disposition-Notification-To: " + MailEncoder.ConvertHeaderToQP(ReplyTo.name, charset) + " <" + ReplyTo.address + ">\r\n");
                }
                else
                {
                    sb.Append("Disposition-Notification-To: <" + ReplyTo.address + ">\r\n");
                }
            }

            if (priority != null)
            {
                sb.Append("X-Priority: " + priority + "\r\n");
            }

            if (customHeaders != null)
            {
                for (IEnumerator i = customHeaders.GetEnumerator(); i.MoveNext();)
                {
                    MailHeader m = (MailHeader)i.Current;

                    if (m.name.Length >= 0 && m.body.Length >= 0)
                    {
                        sb.Append(m.name + ":" + MailEncoder.ConvertHeaderToQP(m.body, charset) + "\r\n");
                    }
                    else
                    {
                        // TODO: Check if below is within RFC spec.
                        sb.Append(m.name + ":\r\n");
                    }
                }
            }

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

            return(sb.ToString());
        }