Example #1
0
        public string Send()
        {
            if (!Enabled)
            {
                return("Рассылка отключена");
            }

            if (TargetMail.IsNullOrEmpty())
            {
                return(!IsForAdmin ? "Не указан адрес рассылки." : "");
            }

            _replacements.AddRange(DefaultReplacements);
            var mail = new Email();

            if (Attachments != null)
            {
                mail = mail.WithAttachments(Attachments);
            }
            if (MemoryAttachments != null)
            {
                mail = mail.WithAttachments(MemoryAttachments);
            }
            return
                (mail.WithSubject(FillWithReplacements(Header))
                 .WithBody(FillWithReplacements(Letter))
                 .From(SiteSetting.Get <string>("SMTPLogin"))
                 .To(TargetMail)
                 .Send());
        }
Example #2
0
        public static bool SendEmail(
            string pGmailEmail,
            string pGmailPassword,
            string pTo,
            string pSubject,
            string pBody,
            System.Web.Mail.MailFormat pFormat,
            string pAttachmentPath)
        {
            try
            {
                System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                    SiteSetting.Get <string>("SMTP"));
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                    SiteSetting.Get <Int32>("SMTPPort"));
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                    "2");
                //sendusing: cdoSendUsingPort, value 2, for sending the message using
                //the network.

                //smtpauthenticate: Specifies the mechanism used when authenticating
                //to an SMTP
                //service over the network. Possible values are:
                //- cdoAnonymous, value 0. Do not authenticate.
                //- cdoBasic, value 1. Use basic clear-text authentication.
                //When using this option you have to provide the user name and password
                //through the sendusername and sendpassword fields.
                //- cdoNTLM, value 2. The current process security context is used to
                // authenticate with the service.
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                //Use 0 for anonymous
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                    pGmailEmail);
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                    pGmailPassword);
                myMail.Fields.Add
                    ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                    SiteSetting.Get <bool>("SMTPSSL") ? "true" : "false");
                myMail.From       = pGmailEmail;
                myMail.To         = pTo;
                myMail.Subject    = pSubject;
                myMail.BodyFormat = pFormat;
                myMail.Body       = pBody;
                if (pAttachmentPath.Trim() != "")
                {
                    MailAttachment MyAttachment =
                        new MailAttachment(pAttachmentPath);
                    myMail.Attachments.Add(MyAttachment);
                    myMail.Priority = System.Web.Mail.MailPriority.Normal;
                }

                System.Web.Mail.SmtpMail.SmtpServer = SiteSetting.Get <string>("SMTP") + ":" + SiteSetting.Get <Int32>("SMTPPort");
                System.Web.Mail.SmtpMail.Send(myMail);
                return(true);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Example #3
0
        protected static bool Send(string sendTo, string sendFrom, string sendSubject, string sendMessage, List <string> attachments, List <KeyValuePair <string, MemoryStream> > memAttaches, out string result)
        {
            try
            {
                /*   MailSender.SendEmail(SiteSetting.Get<string>("SMTPLogin"), SiteSetting.Get<string>("SMTPPass"), sendTo,
                 *     sendSubject, sendMessage, MailFormat.Html, "");
                 * result = "";
                 * return true;*/

                bool bTest = sendTo.IsMailAdress();
                if (bTest == false)
                {
                    result = "Неправильно указан адрес: " + sendTo;
                    return(false);
                }

                var message = new MailMessage(
                    sendFrom,
                    sendTo,
                    sendSubject,
                    sendMessage);


                message.IsBodyHtml   = true;
                message.BodyEncoding = Encoding.UTF8;
                foreach (string attach in attachments)
                {
                    var attached = new Attachment(attach, MediaTypeNames.Application.Octet);
                    //attached.NameEncoding = Encoding.UTF8;
                    message.Attachments.Add(attached);
                }

                foreach (var pair in memAttaches)
                {
                    pair.Value.Position = 0;
                    var attached = new Attachment(pair.Value, pair.Key, MIMETypeWrapper.GetMIME(Path.GetExtension(pair.Key).Substring(1)));
                    //attached.NameEncoding = Encoding.UTF8;
                    message.Attachments.Add(attached);
                }


                // create smtp client at mail server location

                var client = new SmtpClient(SiteSetting.Get <string>("SMTP"));

                if (SiteSetting.Get <Int32>("SMTPPort") > 0)
                {
                    client.Port = SiteSetting.Get <Int32>("SMTPPort");
                }

                if (SiteSetting.Get <string>("SMTPLogin").IsNullOrEmpty() || SiteSetting.Get <string>("SMTPPass").IsNullOrEmpty())
                {
                    client.UseDefaultCredentials = true;
                }
                else
                {
                    client.UseDefaultCredentials = false;
                    client.Credentials           = new NetworkCredential(SiteSetting.Get <string>("SMTPLogin"), SiteSetting.Get <string>("SMTPPass"));
                }
                client.EnableSsl = SiteSetting.Get <bool>("SMTPSSL");
                /*client.DeliveryMethod = SmtpDeliveryMethod.Network;*/

                client.Send(message);
                result = "";
                return(true);
            }

            catch (Exception ex)
            {
                result = ex.Message;
                return(false);
            }
        }