コード例 #1
0
 public static void Start()
 {
     try
     {
         ExtensionLogger.Info("Mail Job started.");
         thread = new Thread(MailJob.Start);
         thread.IsBackground = true;
         thread.Start();
     }
     catch (Exception ex)
     {
         ExtensionLogger.Error("An unhandled exception occured on start mail job", ex);
     }
 }
コード例 #2
0
        public void SendMailNonStatic()
        {
            IEnumerable <Product> newProducts = sendProduct.GetProductForSend();

            foreach (Product product in newProducts)
            {
                string      tagsList = GetNameTagList(product.Tags);
                MailMessage message  = new MailMessage();
                message.To.Add(new MailAddress("*****@*****.**")); //replace with valid value
                message.Subject = "Details of the created or edited product";
                string body = "<p>Product Details:<p> Name : {0}</p> <p> Quantity : {1}</p>  <p> Category : {2}</p> <p> Tags : {3}</p> Price : {4} EUR</p>";
                message.Body       = string.Format(body, product.Name, product.Quantity.ToString(), product.CategoryId, tagsList, product.Price.ToString());
                message.IsBodyHtml = true;

                try {
                    using (var smtp = new SmtpClient())
                    {
                        if (product.Image != null && product.Image.Count() > 0)
                        {
                            using (MemoryStream ms = new System.IO.MemoryStream(product.Image))
                            {
                                ms.Write(product.Image, 0, product.Image.Length);
                                ms.Position = 0;
                                Attachment attachment = new Attachment(ms, "image.jpg", "image/jpeg");
                                message.Attachments.Add(attachment);
                                message.Attachments[0].ContentId = "image.jpg";
                                message.Attachments[0].ContentDisposition.Inline   = true;
                                message.Attachments[0].ContentDisposition.FileName = "image.jpg";
                                message.Body += string.Format("Show Image in List of Products: {0}", YesNoShowInList(product.ShowInList));
                                message.Body += "<img src='cid:image'>" + Environment.NewLine;
                                smtp.Send(message);
                            }
                        }
                        else
                        {
                            smtp.Send(message);
                        }
                    }
                    product.IsSendByEmail = true;
                    sendProduct.UpdateProduct(product);
                }
                catch (Exception ex)
                {
                    ExtensionLogger.Error("Error at sending  mail", ex);
                }
            }
        }