Ejemplo n.º 1
0
        // creates comma separated address list from to: and cc:
        private string CreateAddressList(ArrayList msgList)
        {
            StringBuilder sb = new StringBuilder();

            int index    = 1;
            int msgCount = msgList.Count;

            for (IEnumerator i = msgList.GetEnumerator(); i.MoveNext(); index++)
            {
                EmailAddress a = (EmailAddress)i.Current;

                // if the personal name exists, add it to the address sent. IE: "Ian Stallings" <*****@*****.**>
                if (a.FriendlyName != null && a.FriendlyName.Length > 0)
                {
                    sb.Append("\"" + MailEncoder.ConvertHeaderToQP(a.FriendlyName, charset) + "\" <" + a.Address + ">");
                }
                else
                {
                    sb.Append("<" + a.Address + ">");
                }

                // if it's not the last address add a semi-colon:
                if (msgCount != index)
                {
                    sb.Append(",");
                }
            }

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        // end added by mb

        /// <summary>Constructor using a file path</summary>
        /// <example>
        /// <code>Attachment a = new Attachment("C:\\file.jpg");</code>
        /// </example>
        public Attachment(string filePath)
        {
            this.filePath = filePath;

            if (filePath.Length > 0)
            {
                try
                {
                    FileInfo fileInfo = new FileInfo(filePath);
                    if (fileInfo.Exists)
                    {
                        this.mimeType = getMimeType(fileInfo.Extension);
                        this.name     = fileInfo.Name;
                        this.size     = (int)fileInfo.Length;

                        string encodedTempFile = Path.GetTempFileName();
                        MailEncoder.ConvertToBase64(filePath, encodedTempFile);
                        this.encodedFilePath = encodedTempFile;
                    }
                }
                catch (ArgumentNullException)
                {
                    throw new MailException("Attachment file does not exist or path is incorrect.");
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Encode the file for inclusion as a mime attachment.
        /// The boundaries are not provided.
        /// </summary>
        /// <returns></returns>

        public String ToMime()
        {
            StringBuilder sb = new StringBuilder();

            if (ContentId != null)
            {
                sb.Append("Content-ID: <" + ContentId + ">\r\n");
            }
            sb.Append("Content-Type: " + mimeType + ";\r\n");
            sb.Append(" name=\"" + MailEncoder.ConvertToQP(name, null) + "\"\r\n");
            sb.Append("Content-Transfer-Encoding: " + encoding + "\r\n");
            sb.Append("Content-Disposition: attachment;\r\n");
            sb.Append(" filename=\"" + MailEncoder.ConvertToQP(name, null) + "\"\r\n\r\n");

            FileStream fin = new FileStream(encodedFilePath, FileMode.Open, FileAccess.Read);

            byte[] bin;

            while (fin.Position != fin.Length)
            {
                bin = new byte[76];
                int len = fin.Read(bin, 0, 76);
                sb.Append(System.Text.Encoding.UTF8.GetString(bin, 0, len) + "\r\n");
            }

            fin.Close();
            return(sb.ToString());
        }
Ejemplo n.º 4
0
        private string GetTextMessageBody(string messageBody, string textType)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Content-Type: " + textType + ";\r\n");
            sb.Append(" charset=\"" + charset + "\"\r\n");
            sb.Append("Content-Transfer-Encoding: quoted-printable\r\n\r\n");
            sb.Append(MailEncoder.ConvertToQP(messageBody, charset));

            return(sb.ToString());
        }
Ejemplo n.º 5
0
        /// <summary>Constructor using a provided Stream</summary>
        /// <example>
        /// <code>Attachment a = new Attachment(new FileStrema(@"C:\file.jpg", FileMode.Open, FileAccess.Read), "file.jpg");</code>
        /// </example>
        public Attachment(Stream stream, string fileName)
        {
            try
            {
                this.mimeType = "unknown/application";
                this.name     = fileName;
                this.size     = (int)stream.Length;

                string encodedTempFile = Path.GetTempFileName();
                MailEncoder.ConvertToBase64(stream, encodedTempFile);
                this.encodedFilePath = encodedTempFile;
            }
            catch (ArgumentNullException)
            {
                throw new MailException("Attachment file does not exist or path is incorrect.");
            }
        }
Ejemplo n.º 6
0
        /// <summary>Returns the MailMessage as a RFC 822 formatted message</summary>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder(60000);

            sb.Append("Reply-To: " + ReplyTo.ToMessageHeaderString(charset));
            sb.Append(SmtpConstants.CrLf);
            sb.Append("From: " + From.ToMessageHeaderString(charset));
            sb.Append(SmtpConstants.CrLf);

            if (1 == 2)
            {
                if (Stringer.IsNotEmpty(ReplyTo.FriendlyName))
                {
                    string rt = MessageHeaderString.EncodeAsRequired(
                        Stringer.Enquote(ReplyTo.FriendlyName, '"', QuoteEncapsulation.Escape),
                        new QuotedPrintableTraits( ));
                    sb.Append("Reply-To: ");
                    sb.Append(rt);
                    sb.Append(" <" + ReplyTo.Address + ">" + SmtpConstants.CrLf);
                    string debug = sb.ToString( );
                }
                else
                {
                    sb.Append("Reply-To: <" + ReplyTo.Address + ">" + SmtpConstants.CrLf);
                }

                // from email address.
                if (Stringer.IsNotEmpty(From.FriendlyName))
                {
                    string rt = MessageHeaderString.EncodeAsRequired(
                        Stringer.Enquote(From.FriendlyName, '"', QuoteEncapsulation.Escape),
                        new QuotedPrintableTraits( ));
                    sb.Append("From: ");
                    sb.Append(rt);
                    sb.Append(" <" + From.Address + ">" + SmtpConstants.CrLf);
                }
                else
                {
                    sb.Append("From: <" + From.Address + ">" + SmtpConstants.CrLf);
                }
            }

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

            if (CCRecipients.Count != 0)
            {
                sb.Append("CC: " + CreateAddressList(CCRecipients) + "\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.FriendlyName != null && ReplyTo.FriendlyName.Length != 0)
                {
                    sb.Append("Disposition-Notification-To: " + MailEncoder.ConvertHeaderToQP(ReplyTo.FriendlyName, 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());
        }