Пример #1
0
        public bool sendItemEmail(string item, string lab, int quantity)
        {
            DBOps db = new DBOps();

            try
            {
                string      from         = db.GetConfig("senderEmail").Tables[0].Rows[0]["configval1"].ToString();
                MailAddress fromAddress  = new MailAddress(from, "ETS_Inventory");
                string      fromPassword = db.GetConfig("senderPassword").Tables[0].Rows[0]["configval1"].ToString();
                string      body         = prepItemEmail(item, lab, quantity);
                string      subject      = "ETS Inventory: Low Item Quantity Alert";


                //multiple senders
                DataTable toTable = db.GetConfig("receiverEmail").Tables[0];


                var smtp = new SmtpClient
                {
                    Host                  = "smtp.gmail.com",
                    Port                  = 587,
                    EnableSsl             = true,
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(fromAddress.Address, fromPassword)
                };
                using (var message = new MailMessage() //new MailMessage(fromAddress, toAddress)
                {
                    From = fromAddress,
                    Subject = subject,
                    Body = body,
                    IsBodyHtml = false, //true for multiple row table i think.
                    BodyEncoding = System.Text.Encoding.UTF8,
                })
                {
                    foreach (DataRow dr in toTable.Rows)
                    {
                        message.To.Add(new MailAddress(dr["configval1"].ToString()));
                    }
                    //MailAddress toAddress = new MailAddress(to, "ETS Team");
                    smtp.Send(message); //trySend() in case of failure.
                }

                return(true);
            }
            catch (Exception ex)
            {
                log.Error("Error in sendItem", ex);
                return(false);
            }
        }
Пример #2
0
        public bool sendEmail()
        {
            DBOps db = new DBOps();

            try
            {
                DataSet from = db.GetConfig("senderEmail");
                DataSet to   = db.GetConfig("receiverEmail");


                var          fromAddress  = new MailAddress("*****@*****.**", "ETS_Inventory_Alerts");
                var          toAddress    = new MailAddress("*****@*****.**", "Recievers");
                const string fromPassword = "******";
                const string subject      = "Subject";
                const string body         = "Body";

                var smtp = new SmtpClient
                {
                    Host                  = "smtp.gmail.com",
                    Port                  = 587,
                    EnableSsl             = true,
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(fromAddress.Address, fromPassword)
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body
                })
                {
                    smtp.Send(message);
                }

                return(true);
            } catch (Exception ex)
            {
                log.Error("An error happened", ex);
                return(false);
            }
        }