Exemple #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++)
            {
                MailAddress a = (MailAddress)i.Current;

                // if the personal name exists, add it to the address sent. IE: "Ian Stallings" <*****@*****.**>
                if (a != null)
                {
                    if (!string.IsNullOrEmpty(a.DisplayName))
                    {
                        sb.AppendFormat("\"{0}\" <{1}>", MailEncoder.ConvertHeaderToQP(a.DisplayName, _charset), a.Address);
                    }
                    else
                    {
                        sb.AppendFormat("<{0}>", a.Address);
                    }

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

            return(sb.ToString());
        }
Exemple #2
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.AppendFormat(Line("Content-ID: <{0}>"), ContentId);
            }
            sb.AppendFormat(Line("Content-Type: {0};"), MimeType);
            sb.AppendFormat(Line(" name=\"{0}\""), MailEncoder.ConvertToQP(Name));
            sb.AppendFormat(Line("Content-Transfer-Encoding: {0}"), Encoding);
            sb.AppendFormat(Line("Content-Disposition: attachment;"));
            sb.AppendFormat(Line(Line(" filename=\"{0}\"")), MailEncoder.ConvertToQP(Name));

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

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

            fin.Close();
            return(sb.ToString());
        }
Exemple #3
0
        private string GetTextMessageBody(string messageBody, string textType)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("Content-Type: {0};\r\n", textType);
            sb.AppendFormat(" charset=\"{0}\"\r\n", Charset);
            sb.AppendFormat("Content-Transfer-Encoding: quoted-printable\r\n\r\n");
            sb.AppendFormat(MailEncoder.ConvertToQP(messageBody, Charset));

            return(sb.ToString());
        }
        public void TestQuotedPrintable()
        {
            StringBuilder s = new StringBuilder();

            s.Append("space before crlf: \r\n");
            s.Append("tab before crlf:	\r\n");
            s.Append("no spaces before this crlf:\r\n");
            s.Append("above 126:ËÇÅÃ\r\n");
            s.Append("equal sign:=\r\n");
            s.Append("null, form feed, backspace (before 32):\0\f\b\r\n");
            s.Append("over 76 chars: 12345678901234567890123456789012345678901234567890123456789012345678901234567890");

            Console.WriteLine("\r\n----- QP Encoded ------\r\n" + MailEncoder.ConvertToQP(s.ToString(), "ISO-8859-1"));
        }
Exemple #5
0
        private void InitAttachment(Stream stream, string fileName)
        {
            try
            {
                this.MimeType = "unknown/application";
                this.Name     = fileName;
                Size          = (int)stream.Length;

                string encodedTempFile = Path.GetTempFileName();
                MailEncoder.ConvertToBase64(stream, encodedTempFile);
                EncodedFilePath = encodedTempFile;
            }
            catch (Exception exception)
            {
                throw new SmtpException("Attachment file does not exist or path is incorrect.", exception);
            }
        }
        public void TestBase64File()
        {
            string imagePath         = @"..\lib\test attachments\test.jpg";
            string encodedImagePath  = @"..\lib\test attachments\base64_EncodedImage.txt";
            string returnedImagePath = @"..\lib\test attachments\base64_returned.jpg";

            Bitmap image = new Bitmap(imagePath);

            MailEncoder.ConvertToBase64(imagePath, encodedImagePath);

            MailEncoder.ConvertFromBase64(encodedImagePath, returnedImagePath);
            Bitmap returnedImage = new Bitmap(returnedImagePath);

            Assertion.AssertEquals("Returned image from Base64 test has incorrect height.", image.Height, returnedImage.Height);
            Assertion.AssertEquals("Returned image from Base64 test has incorrect width.", image.Width, returnedImage.Width);

            image.Dispose();
            returnedImage.Dispose();
            GC.Collect();
        }
Exemple #7
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());
        }