Exemple #1
0
        // creates comma seperated address list from to: and cc:
        private string CreateAddressList(List <EmailAddress> 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;

                // EmailAddress a = new EmailAddress(i.Current.ToString());

                // if the personal name exists, add it to the address sent. IE: "Ian Stallings" <*****@*****.**>
                if (a.name != null && a.name.Length > 0)
                {
                    sb.Append("=?" + charset + "?Q?" + MailEncoder.ConvertToQP(a.name) + "?= <" + 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());
        }
Exemple #2
0
        /// <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 SmtpException("Attachment file does not exist or path is incorrect.");
                }
            }
        }
Exemple #3
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("Content-Transfer-Encoding: Quoted-Printable\r\n\r\n");
            sb.Append("Content-Transfer-Encoding: base64\r\n\r\n");
            sb.Append(MailEncoder.ConvertToQP(messageBody));

            return(sb.ToString());
        }
Exemple #4
0
        // adds the contents of a encoded temp file to the message
        private string GetAttachments()
        {
            StringBuilder sb = new StringBuilder();

            if (attachments.Count != 0)
            {
                StringBuilder attSection = new StringBuilder();
                for (IEnumerator i = attachments.GetEnumerator(); i.MoveNext();)
                {
                    Attachment att = (Attachment)i.Current;

                    attSection.Append("\r\n--" + mixedBoundary + "\r\n");
                    attSection.Append("Content-Type: " + att.mimeType + ";\r\n");
                    attSection.Append(" name=\"" + att.name + "\"\r\n");
                    attSection.Append("Content-Transfer-Encoding: " + att.encoding + "\r\n");
                    attSection.Append("Content-Disposition: attachment;\r\n");
                    attSection.Append(" filename=\"" + MailEncoder.ConvertSubjectToQP(att.name, charset) + "\"\r\n\r\n");

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

                    byte[] bin;

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

                    fin.Close();
                }

                sb.Append(attSection);
            }

            return(sb.ToString());
        }
Exemple #5
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)
            {
//                sb.Append("Subject: " + MailEncoder.ConvertHeaderToQP(subject, charset) + "\r\n");
                sb.Append("Subject: " + MailEncoder.ConvertSubjectToQP(subject, charset) + "\r\n");
            }

            sb.Append("Date: " + DateTime.Now.ToUniversalTime().ToString("R") + "\r\n");
            sb.Append(SmtpConfig.Current.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(htmlBody!=null)
//			{
//				sb.Append(MailEncoder.ConvertHeaderToQP(htmlBody, charset) + "\r\n");
//			}
            if (priority != null)
            {
                sb.Append("X-Priority: " + priority + "\r\n");
            }

            if (customHeaders.Count > 0)
            {
                for (int i = 0; i < customHeaders.Count; i++)
                {
                    sb.Append(customHeaders.Keys[i]);
                    sb.Append(": ");
                    sb.Append(MailEncoder.ConvertHeaderToQP(customHeaders[customHeaders.Keys[i]], charset));
                    sb.Append("\r\n");
                }
            }

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

            return(sb.ToString());
        }