Beispiel #1
0
        private void mnuProcess_Mail_Click(object sender, EventArgs e)
        {
            string sBody =
                "Hello Yolanda  Xie,\r\n" +
                "\r\n" +
                "Thank you for purchasing Naurtech software. Here are the registration keys for your software licenses. Please note that both the License ID and Registration keys are case sensitive. You can find instructions to manually register your license at:\r\n" +
                "http://www.naurtech.com/wiki/wiki.php?n=Main.TrainingVideoManualRegistration\r\n" +
                "\r\n" +
                "     Order Number:     15109\r\n" +
                "     Order Date:       4/10/2014\r\n" +
                "     Your PO Number:   PO93504\r\n" +
                "     End Customer:     Honeywell\r\n" +
                "     Product:          [NAU-1504] CETerm for Windows CE 6.0 / 5.0 / CE .NET\r\n" +
                "     Quantity:         46\r\n" +
                "\r\n" +
                "     Qty Ordered...............: 46\r\n" +
                "     Qty Shipped To Date.......: 46\r\n" +
                "\r\n" +
                "     Qty Shipped in this email.: 46\r\n" +
                "\r\n" +
                "\r\n" +
                "**** Registration Keys for Version 5.7 ***** \r\n" +
                "\r\n" +
                "\r\n" +
                "Version 5.1 and above support AUTOMATED LICENSE REGISTRATION. Please use the attached license file. This prevents you from having to type each key to register your copy of the software. Please refer to support wiki article http://www.naurtech.com/wiki/wiki.php?n=Main.AutoRegistration\r\n" +
                "";

            string testFile = utils.helpers.getAppPath() + "LicenseXMLFileSample.xml";
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            //read file into memorystream
            using (System.IO.FileStream file = new System.IO.FileStream(testFile, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                byte[] bytes = new byte[file.Length];
                file.Read(bytes, 0, (int)file.Length);
                ms.Write(bytes, 0, (int)file.Length);
                ms.Flush();
            }

            Attachement[] atts = new Attachement[] { new Attachement(ms, "test.xml") };
            MailMessage msg = new MailMessage("E841719", sBody, "License Keys - Order: 15476: [NAU-1504] CETerm for Windows CE 6.0 / 5.0 / CE .NET", atts, DateTime.Now);

            this.Cursor = Cursors.WaitCursor;
            this.Enabled = false;
            Application.DoEvents();
            int i = _licenseMail.processMail(msg);
            this.Enabled = true;
            this.Cursor = Cursors.Default;
            Application.DoEvents();

            //setLblStatus("Updated data. Use refresh";
            doRefresh();
        }
Beispiel #2
0
        public static void SendMailSmtp(string subject, string body, bool isBodyHtml, MailAddress[] mailTo, MailAddress[] hiddenMailTo, AttachmentFile file, bool isTest)
        {
            if (!mailTo.Any() && (hiddenMailTo == null || !hiddenMailTo.Any()))
            {
                throw new Exception("Не указаны получатели письма!");
            }

            var mail = new MailMessage();

            string host     = ConfigurationManager.AppSettings["SmtpHost"];
            int    port     = String.IsNullOrEmpty(ConfigurationManager.AppSettings["Host"]) ? 587 :Convert.ToInt32(ConfigurationManager.AppSettings["Host"]);
            string login    = ConfigurationManager.AppSettings["SmtpLogin"];
            string pass     = ConfigurationManager.AppSettings["SmtpPass"];
            string mailFrom = ConfigurationManager.AppSettings["SmtpMailFrom"];

            var client = new SmtpClient(host, port);

            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl      = true;
            client.Credentials    = new NetworkCredential(login, pass);

            mail.From = new MailAddress(mailFrom, string.Empty, System.Text.Encoding.UTF8);

            //client.EnableSsl = false;

            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["Environment"]) && !isTest)
            {
                if (mailTo != null)
                {
                    foreach (var mailAddress in mailTo)
                    {
                        if (string.IsNullOrEmpty(mailAddress.Address))
                        {
                            continue;
                        }
                        mail.To.Add(mailAddress);
                    }
                }
                if (hiddenMailTo != null)
                {
                    foreach (var mailAddress in hiddenMailTo)
                    {
                        if (string.IsNullOrEmpty(mailAddress.Address))
                        {
                            continue;
                        }
                        mail.CC.Add(mailAddress);
                    }
                }
            }
            else
            {
                var testMails = ConfigurationManager.AppSettings["Emails4Test"]?.Split('|');
                foreach (var email in testMails)
                {
                    if (string.IsNullOrEmpty(email))
                    {
                        continue;
                    }
                    mail.To.Add(email);
                }

                body += "\r\n";
                if (mailTo != null)
                {
                    foreach (var mailAddress in mailTo)
                    {
                        body += "\r\n" + mailAddress.Address;
                    }
                }
                //Hidden recipients
                if (hiddenMailTo != null)
                {
                    foreach (var mailAddress in hiddenMailTo)
                    {
                        body += "\r\n" + mailAddress.Address;
                    }
                }
            }

            mail.Subject    = subject;
            mail.Body       = body;
            mail.IsBodyHtml = isBodyHtml;
            //client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
            //client.SendCompleted += (s, e) => {
            //    SendCompletedCallback(s, e);
            //    client.Dispose();
            //    mail.Dispose();
            //};

            if (file != null && file.Data.Length > 0)
            {
                var stream     = new MemoryStream(file.Data);
                var attachment = new Attachment(stream, file.FileName, file.DataMimeType);
                mail.Attachments.Add(attachment);
            }

            try
            {
                client.Send(mail);
            }
            catch (Exception ex)
            {
                throw new Exception($"Сообщение не было отправлено. Текст ошибки - {ex.Message}");
            }
        }