예제 #1
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 && EmitSenderHeader)
            {
                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));

            if (AutomaticallyBuildAndApplyRecipientHeaders)
            {
                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);
            }

            if (!DisplayBccHeader)
            {
                Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Bcc));
            }

            if (AutomaticallyBuildAndApplyRecipientHeaders && DisplayBccHeader && Bcc.Count > 0)
            {
                Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Bcc));
                headerName = MailHeaderInfo.GetString(MailHeaderID.Bcc);
                Headers.InternalAdd(headerName, Bcc.Encode(headerName.Length, allowUnicode));
            }

            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(Date, 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) && !PreferQEncodeForHeaders,
                                                                       headerName.Length));
                }
            }
            else
            {
                Headers.Remove(headerName);
            }

            EncodeHeaders(_headers, allowUnicode);
        }