示例#1
0
        /// <summary>
        /// Create message.
        /// </summary>
        /// <param name="subject">Message subject.</param>
        /// <param name="body">Message body.</param>
        /// <param name="bodyType">Type of body (MediaTypeNames.Text)</param>
        /// <returns>System.Net.Mail.MailMessage</returns>
        public static MailMessage GetMessage(string subject, string body, MailTextType bodyType = MailTextType.Auto)
        {
            if (bodyType == MailTextType.Auto)
            {
                bodyType = IsHtml(body) ? MailTextType.Html : MailTextType.Plain;
            }
            var message = new MailMessage()
            {
                Subject         = subject,
                SubjectEncoding = Encoding.UTF8,
                BodyEncoding    = Encoding.UTF8,
            };
            // Framework selects a TransferEncoding of Base64 if you set the BodyEncoding property to UTF8, Unicode or UTF32.
            // So we must add alternate view for some old crappy browsers which doesn't support Base64 TransferEncoding.
            var           plainBody = string.Empty;
            AlternateView htmlView  = null;

            switch (bodyType)
            {
            case MailTextType.Html:
                // Add HTML view.
                htmlView = AlternateView.CreateAlternateViewFromString(body, Encoding.UTF8, MediaTypeNames.Text.Html);
                // Very important
                htmlView.TransferEncoding = TransferEncoding.SevenBit;
                // Mark message as HTML.
                message.IsBodyHtml = true;
                // Create alternative plain body.
                plainBody = HtmlToText(body);
                break;

            case MailTextType.Plain:
                plainBody = body;
                break;

            case MailTextType.RichText:
                break;

            case MailTextType.Xml:
                break;

            default:
                break;
            }
            // Add Plain View.
            var plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, MediaTypeNames.Text.Plain);

            // Very important
            plainView.TransferEncoding = TransferEncoding.SevenBit;
            // Add views.
            message.AlternateViews.Add(plainView);
            // "text/html" part must be last in order for gmail to display it.
            if (htmlView != null)
            {
                message.AlternateViews.Add(htmlView);
            }
            return(message);
        }
示例#2
0
        public string Send(string toAddress, string toName, string subject, string body, MailTextType bodyType)
        {
            MailMessage message = GetMessage(subject, body, bodyType);

            message.From = new MailAddress(SmtpSettings.From);
            message.To.Add(new MailAddress(toAddress, toName));
            var token = new UserToken {
                Message = message
            };

            //smtp.SendAsync(message, new UserToken());
            //smtp.Send(message);
            //MailClient.SendAsync(message, token);
            MailClient.Send(message);
            return(string.Empty);
        }
示例#3
0
        public MailText getItemByType(MailTextType type)
        {
            var mailTexts = _list.Where(mailText => mailText.ID == (int)type);

            return (mailTexts.Count() == 0) ? null : mailTexts.First() as MailText;
        }
示例#4
0
        public MailText getItemByType(MailTextType type)
        {
            var mailTexts = _list.Where(mailText => mailText.Id == (int)type);

            return((mailTexts.Count() == 0) ? null : mailTexts.First() as MailText);
        }
示例#5
0
 public string Send(string toAddress, string toName, string subject, string body, MailTextType bodyType)
 {
     MailMessage message = GetMessage(subject, body, bodyType);
     message.From = new MailAddress(SmtpSettings.From);
     message.To.Add(new MailAddress(toAddress, toName));
     var token = new UserToken { Message = message };
     //smtp.SendAsync(message, new UserToken());
     //smtp.Send(message);
     //MailClient.SendAsync(message, token);
     MailClient.Send(message);
     return string.Empty;
 }
示例#6
0
 /// <summary>
 /// Create message.
 /// </summary>
 /// <param name="subject">Message subject.</param>
 /// <param name="body">Message body.</param>
 /// <param name="bodyType">Type of body (MediaTypeNames.Text)</param>
 /// <returns>System.Net.Mail.MailMessage</returns>
 public MailMessage GetMessage(string subject, string body, MailTextType bodyType)
 {
     var message = new MailMessage { SubjectEncoding = Encoding.UTF8, Subject = subject, BodyEncoding = Encoding.UTF8 };
     // Framework selects a TransferEncoding of Base64 if you set the BodyEncoding property to UTF8, Unicode or UTF32.
     // So we must add alternate view for some old crappy browsers which doesn't support Base64 TransferEncoding.
     string plainBody = string.Empty;
     AlternateView htmlView = null;
     switch (bodyType)
     {
         case MailTextType.Html:
             // Add HTML view.
             htmlView = AlternateView.CreateAlternateViewFromString(body, Encoding.UTF8, MediaTypeNames.Text.Html);
             htmlView.TransferEncoding = TransferEncoding.SevenBit; // Very important
                                                                    // Mark message as HTML.
             message.IsBodyHtml = true;
             // Create alternative plain body.
             plainBody = ConvertHtmlToPlain(body);
             break;
         case MailTextType.Plain:
             plainBody = body;
             break;
         case MailTextType.RichText:
             break;
         case MailTextType.Xml:
             break;
         default:
             break;
     }
     // Add Plain View.
     AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, MediaTypeNames.Text.Plain);
     plainView.TransferEncoding = TransferEncoding.SevenBit; // Very important
                                                             // Add views.
     message.AlternateViews.Add(plainView);
     // "text/html" part must be last in order for gmail to display it.
     if (htmlView != null) message.AlternateViews.Add(htmlView);
     return message;
 }