Beispiel #1
0
 public void Send(DefaultMailTemplate template)
 {
     template.Process();
     lock (syncObject)
     {
         Send(template.mailConfig.From, template.mailConfig.FromFriendlyName, template.ToList, template.Subject, template.tempBodyString, template.resourceIdDic);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Send mail to user.
        /// </summary>
        /// <param name="template"></param>
        public void SendMailAsync(DefaultMailTemplate template, UserTask taskItem, Action <Exception, UserTask> callback)
        {
            List <LinkedResource> resList      = new List <LinkedResource>();
            List <IDisposable>    resourceList = new List <IDisposable>();

            try
            {
                taskItem.beginTicks = DateTime.Now.Ticks;
                template.Process();

                SmtpClient client = new SmtpClient(template.mailConfig.Host, template.mailConfig.Port);
                resourceList.Add(client);
                client.Timeout = EDMMSConfigSection.Current.MailClientTimeout;

                client.UseDefaultCredentials = false;
                client.Credentials           = new NetworkCredential(template.mailConfig.UserName, template.mailConfig.Password);

                MailMessage message = new MailMessage();
                resourceList.Add(message);
                message.From = new MailAddress(template.mailConfig.From, template.mailConfig.FromFriendlyName);

                foreach (string to in template.ToList)
                {
                    message.To.Add(to);
                }

                message.Subject = template.Subject;

                string bodyString = template.tempBodyString;

                foreach (string stub in template.resourceIdDic.Keys)
                {
                    MemoryStream ms = new MemoryStream(template.resourceIdDic[stub]);
                    resourceList.Add(ms);
                    LinkedResource imageResource = new LinkedResource(ms, "image/jpeg");
                    resourceList.Add(imageResource);
                    resList.Add(imageResource);
                    bodyString = bodyString.Replace(stub, imageResource.ContentId);
                }

                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(bodyString, null, "text/html");
                resourceList.Add(htmlView);
                foreach (LinkedResource res in resList)
                {
                    htmlView.LinkedResources.Add(res);
                }

                //
                message.AlternateViews.Add(htmlView);

                client.SendCompleted += (s, e) =>
                {
                    try
                    {
                        MailMessage msg = e.UserState as MailMessage;
                        if (msg != null)
                        {
                            msg.Dispose();
                        }

                        SmtpClient mailClient = s as SmtpClient;
                        if (mailClient != null)
                        {
                            mailClient.Dispose();
                        }

                        if (callback != null)
                        {
                            callback(e.Error, taskItem);
                        }
                    }
                    catch (Exception inEx)
                    {
                        tracing.ErrorFmt(inEx, "client.SendCompleted failed with unexpected error.");
                    }
                };

                client.SendAsync(message, message);
            }
            catch (Exception)
            {
                foreach (IDisposable item in resourceList)
                {
                    item.Dispose();
                }

                throw;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Send mail to user.
        /// </summary>
        /// <param name="template"></param>
        public void SendMail(DefaultMailTemplate template)
        {
            template.Process();

            using (SmtpClient client = new SmtpClient(template.mailConfig.Host, template.mailConfig.Port))
            {
                client.Timeout = EDMMSConfigSection.Current.MailClientTimeout;
                client.UseDefaultCredentials = false;
                client.Credentials           = new NetworkCredential(template.mailConfig.UserName, template.mailConfig.Password);

                using (MailMessage message = new MailMessage())
                {
                    message.From = new MailAddress(template.mailConfig.From, template.mailConfig.FromFriendlyName);
                    List <LinkedResource> resList = new List <LinkedResource>();
                    List <MemoryStream>   msList  = new List <MemoryStream>();
                    foreach (string to in template.ToList)
                    {
                        message.To.Add(to);
                    }

                    message.Subject = template.Subject;

                    string bodyString = template.tempBodyString;
                    try
                    {
                        foreach (string stub in template.resourceIdDic.Keys)
                        {
                            MemoryStream ms = new MemoryStream(template.resourceIdDic[stub]);
                            msList.Add(ms);
                            LinkedResource imageResource = new LinkedResource(ms, "image/jpeg");

                            resList.Add(imageResource);
                            bodyString = bodyString.Replace(stub, imageResource.ContentId);
                        }

                        using (AlternateView htmlView = AlternateView.CreateAlternateViewFromString(bodyString, null, "text/html"))
                        {
                            foreach (LinkedResource res in resList)
                            {
                                htmlView.LinkedResources.Add(res);
                            }

                            message.AlternateViews.Add(htmlView);
                            //
                            client.Send(message);
                        }
                    }
                    catch (Exception)
                    {
                        foreach (LinkedResource res in resList)
                        {
                            res.Dispose();
                        }

                        foreach (MemoryStream ms in msList)
                        {
                            ms.Dispose();
                        }

                        throw;
                    }
                }
            }
        }