Example #1
0
        /// <summary>
        /// Creates a new SMTP email message.
        /// </summary>
        /// <param name="from">From Mail Box object.</param>
        /// <param name="to">To Mail Box object.</param>
        /// <param name="cc">CC Mailbox object.</param>
        /// <param name="subject">Subject line.</param>
        /// <param name="body">Message body.</param>
        /// <param name="filesToAttach">Files that has to be attached</param>
        public SmtpMessage(MailBox from, MailBox to, MailBox cc, string subject, string body, List<string> filesToAttach=null)
        {
            Initialize();

            From = from;
            
            if (!String.IsNullOrEmpty(to.EmailAddress))
            {
                To.Add(to);
            }

            if (cc != null && !string.IsNullOrEmpty(cc.EmailAddress))
            {
                Cc.Add(cc);
            }

            Subject = subject;
            Body = body;
            if (filesToAttach != null)
            {
                HasAttachments = true;
                AttachedFiles = filesToAttach;
            }
        }
Example #2
0
        /// <summary>
        /// Creates the SMTP message body.
        /// </summary>
        /// <returns>The string representation of the message.</returns>
        public async Task <string> CreateMessageBody()
        {
            if (From == null)
            {
                throw new Exception("From field is missing.");
            }

            if (To.Count == 0)
            {
                throw new Exception("To field is missing.");
            }

            StringBuilder message = new StringBuilder();

            message.AppendFormat("Date: {0}{1}", DateTime.Now.ToString(DATE_FORMAT), System.Environment.NewLine);
            message.AppendFormat("X-Priority: {0}{1}", ((byte)Priority).ToString(), System.Environment.NewLine);
            message.AppendFormat("From: \"{0}\"<{1}>{2}", From.Name, From.EmailAddress, System.Environment.NewLine);

            message.Append("To: ");

            for (int i = 0; i < To.Count; i++)
            {
                MailBox to        = To[i];
                string  separator = i == To.Count - 1 ? Environment.NewLine : ", ";

                message.AppendFormat("\"{0}\"<{1}>{2}", to.Name, to.EmailAddress, separator);
            }
            if (Cc.Count > 0)
            {
                message.Append("Cc: ");
            }
            for (int i = 0; i < Cc.Count; i++)
            {
                MailBox cc        = Cc[i];
                string  separator = i == Cc.Count - 1 ? Environment.NewLine : ", ";

                message.AppendFormat("\"{0}\"<{1}>{2}", cc.Name, cc.EmailAddress, separator);
            }
            if (Cc.Count > 0)
            {
                message.AppendFormat("{0}", Environment.NewLine);
            }
            message.AppendFormat("Subject: {0}{1}", Subject, Environment.NewLine);
            message.AppendFormat("MIME-Version: 1.0{0}", Environment.NewLine);
            if (HasAttachments)
            {
                message.AppendFormat("Content-Type: multipart/mixed; boundary=\"{0}\"{1}", Boundary, Environment.NewLine);
                message.AppendFormat("{0}", Environment.NewLine);
                message.AppendFormat("--{0}{1}", Boundary, Environment.NewLine);
            }
            if (IsHtml)
            {
                message.AppendFormat("Content-Type: text/html; {0}", System.Environment.NewLine);
            }
            else
            {
                message.AppendFormat("Content-Type: text/plain; charset=\"{0}\"{1}", Encoding.WebName, System.Environment.NewLine);
            }

            message.Append(Environment.NewLine);
            message.Append(Body);
            message.Append(Environment.NewLine);
            message.Append(Environment.NewLine);
            if (HasAttachments)
            {
                foreach (string file in AttachedFiles)
                {
                    message.AppendFormat("--{0}{1}", Boundary, Environment.NewLine);
                    message.AppendFormat("Content-Type: application/octet-stream;{0}", Environment.NewLine);
                    message.AppendFormat("Content-Transfer-Encoding: base64{0}", Environment.NewLine);
                    string filename = _findFileName(file);
                    message.AppendFormat("Content-Disposition: attachment; filename= \"" + filename + "\" {0}", Environment.NewLine);
                    string fileEncoded = await _encodeToBase64(file);

                    message.Append(Environment.NewLine);
                    message.Append(fileEncoded);
                    message.Append(Environment.NewLine);
                }
                message.AppendFormat("--{0}--{1}", Boundary, Environment.NewLine);
            }
            message.Append(".");

            return(message.ToString());
        }
Example #3
0
        /// <summary>
        /// Sends the specified email message.
        /// </summary>
        /// <param name="from">The sender's email address.</param>
        /// <param name="to">The receiver's email address.</param>
        /// <param name="subject">The subject line of the email.</param>
        /// <param name="message">The message body of the email.</param>
        /// <returns>True if the message was sent successfully. False otherwise.</returns>
        public async Task<bool> SendAsync(MailBox from, MailBox to, string subject, string message)
        {
            From = from;
            To = to;
            Subject = subject;
            Message = message;

            bool result = await SendAsync();

            return result;
        }