Example #1
0
        internal void PrepareEnvelopeHeaders(bool sendEnvelope, bool allowUnicode)
        {
            if (_headersEncoding == null)
            {
                _headersEncoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
            }

            EncodeHeaders(EnvelopeHeaders, allowUnicode);

            // Only add X-Sender header if it wasn't already set by the user
            string xSenderHeader = MailHeaderInfo.GetString(MailHeaderID.XSender) !;

            if (!IsHeaderSet(xSenderHeader))
            {
                MailAddress sender = Sender ?? From !;
                EnvelopeHeaders.InternalSet(xSenderHeader, sender.Encode(xSenderHeader.Length, allowUnicode));
            }

            string headerName = MailHeaderInfo.GetString(MailHeaderID.XReceiver) !;

            EnvelopeHeaders.Remove(headerName);

            foreach (MailAddress address in To)
            {
                EnvelopeHeaders.InternalAdd(headerName, address.Encode(headerName.Length, allowUnicode));
            }
            foreach (MailAddress address in CC)
            {
                EnvelopeHeaders.InternalAdd(headerName, address.Encode(headerName.Length, allowUnicode));
            }
            foreach (MailAddress address in Bcc)
            {
                EnvelopeHeaders.InternalAdd(headerName, address.Encode(headerName.Length, allowUnicode));
            }
        }
        internal void PrepareEnvelopeHeaders(bool sendEnvelope)
        {
            if (this.headersEncoding == null)
            {
                this.headersEncoding = Encoding.GetEncoding("utf-8");
            }
            this.EncodeHeaders(this.EnvelopeHeaders);
            string headerName = MailHeaderInfo.GetString(MailHeaderID.XSender);

            if (!this.IsHeaderSet(headerName))
            {
                this.EnvelopeHeaders.InternalSet(headerName, this.From.Encode(headerName.Length));
            }
            this.EnvelopeHeaders.Remove(MailHeaderInfo.GetString(MailHeaderID.XReceiver));
            foreach (MailAddress address in this.To)
            {
                this.EnvelopeHeaders.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.XReceiver), address.Encode(MailHeaderInfo.GetString(MailHeaderID.XReceiver).Length));
            }
            foreach (MailAddress address2 in this.CC)
            {
                this.EnvelopeHeaders.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.XReceiver), address2.Encode(MailHeaderInfo.GetString(MailHeaderID.XReceiver).Length));
            }
            foreach (MailAddress address3 in this.Bcc)
            {
                this.EnvelopeHeaders.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.XReceiver), address3.Encode(MailHeaderInfo.GetString(MailHeaderID.XReceiver).Length));
            }
        }
Example #3
0
        internal void PrepareHeaders(bool sendEnvelope, bool allowUnicode)
        {
            string headerName;

            if (_headersEncoding == null)
            {
                _headersEncoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
            }

            //ContentType is written directly to the stream so remove potential user duplicate
            Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ContentType) !);

            Headers[MailHeaderInfo.GetString(MailHeaderID.MimeVersion)] = "1.0";

            // add sender to headers first so that it is written first to allow the IIS smtp svc to
            // send MAIL FROM with the sender if both sender and from are present
            headerName = MailHeaderInfo.GetString(MailHeaderID.Sender) !;
            if (Sender != null)
            {
                Headers.InternalAdd(headerName, Sender.Encode(headerName.Length, allowUnicode));
            }
            else
            {
                Headers.Remove(headerName);
            }

            headerName = MailHeaderInfo.GetString(MailHeaderID.From) !;
            Headers.InternalAdd(headerName, From !.Encode(headerName.Length, allowUnicode));

            headerName = MailHeaderInfo.GetString(MailHeaderID.To) !;
            if (To.Count > 0)
            {
                Headers.InternalAdd(headerName, To.Encode(headerName.Length, allowUnicode));
            }
            else
            {
                Headers.Remove(headerName);
            }

            headerName = MailHeaderInfo.GetString(MailHeaderID.Cc) !;
            if (CC.Count > 0)
            {
                Headers.InternalAdd(headerName, CC.Encode(headerName.Length, allowUnicode));
            }
            else
            {
                Headers.Remove(headerName);
            }

            headerName = MailHeaderInfo.GetString(MailHeaderID.ReplyTo) !;
            if (ReplyTo != null)
            {
                Headers.InternalAdd(headerName, ReplyTo.Encode(headerName.Length, allowUnicode));
            }
            else if (ReplyToList.Count > 0)
            {
                Headers.InternalAdd(headerName, ReplyToList.Encode(headerName.Length, allowUnicode));
            }
            else
            {
                Headers.Remove(headerName);
            }

            Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Bcc) !);

            if (_priority == MailPriority.High)
            {
                Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "1";
                Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "urgent";
                Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "high";
            }
            else if (_priority == MailPriority.Low)
            {
                Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "5";
                Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "non-urgent";
                Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "low";
            }
            //if the priority was never set, allow the app to set the headers directly.
            else if (((int)_priority) != -1)
            {
                Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.XPriority) !);
                Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Priority) !);
                Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Importance) !);
            }

            Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Date) !,
                                MailBnfHelper.GetDateTimeString(DateTime.Now, null) !);

            headerName = MailHeaderInfo.GetString(MailHeaderID.Subject) !;
            if (!string.IsNullOrEmpty(_subject))
            {
                if (allowUnicode)
                {
                    Headers.InternalAdd(headerName, _subject);
                }
                else
                {
                    Headers.InternalAdd(headerName,
                                        MimeBasePart.EncodeHeaderValue(_subject, _subjectEncoding,
                                                                       MimeBasePart.ShouldUseBase64Encoding(_subjectEncoding),
                                                                       headerName.Length));
                }
            }
            else
            {
                Headers.Remove(headerName);
            }

            EncodeHeaders(_headers !, allowUnicode);
        }
 internal void PrepareHeaders(bool sendEnvelope)
 {
     if (this.headersEncoding == null)
     {
         this.headersEncoding = Encoding.GetEncoding("utf-8");
     }
     this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ContentType));
     this.Headers[MailHeaderInfo.GetString(MailHeaderID.MimeVersion)] = "1.0";
     if (this.Sender != null)
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Sender), this.Sender.Encode(MailHeaderInfo.GetString(MailHeaderID.Sender).ToString().Length));
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Sender));
     }
     this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.From), this.From.Encode(MailHeaderInfo.GetString(MailHeaderID.From).ToString().Length));
     if (this.To.Count > 0)
     {
         string str = this.To.Encode(MailHeaderInfo.GetString(MailHeaderID.To).Length);
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.To), str);
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.To));
     }
     if (this.CC.Count > 0)
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Cc), this.CC.Encode(MailHeaderInfo.GetString(MailHeaderID.Cc).Length));
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Cc));
     }
     if (this.ReplyTo != null)
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.ReplyTo), this.ReplyTo.Encode(MailHeaderInfo.GetString(MailHeaderID.ReplyTo).Length));
     }
     else if (this.ReplyToList.Count > 0)
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.ReplyTo), this.ReplyToList.Encode(MailHeaderInfo.GetString(MailHeaderID.ReplyTo).Length));
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ReplyTo));
     }
     this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Bcc));
     if (this.priority == MailPriority.High)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "1";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "urgent";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "high";
     }
     else if (this.priority == MailPriority.Low)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "5";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "non-urgent";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "low";
     }
     else if (this.priority != ~MailPriority.Normal)
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.XPriority));
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Priority));
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Importance));
     }
     this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Date), MailBnfHelper.GetDateTimeString(DateTime.Now, null));
     if (!string.IsNullOrEmpty(this.subject))
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Subject), MimeBasePart.EncodeHeaderValue(this.subject, this.subjectEncoding, MimeBasePart.ShouldUseBase64Encoding(this.subjectEncoding), MailHeaderID.Subject.ToString().Length));
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Subject));
     }
     this.EncodeHeaders(this.headers);
 }