Пример #1
0
            // http://stackoverflow.com/questions/7276375/what-are-best-practices-for-using-smtpclient-sendasync-and-dispose-under-net-4/7276819#7276819

            /// <summary>
            /// Call using:
            /// using System.Threading.Tasks;
            /// var t = Task.Run( () => Global.Utils.Emails.SendEmailAsync( "*****@*****.**", asunto, "enviado desde helpdesk", false ) );
            /// t.Wait();
            /// </summary>
            /// <param name="toEmailAddress"></param>
            /// <param name="emailSubject"></param>
            /// <param name="emailMessage"></param>
            /// <param name="isBodyHtml"></param>
            /// <returns></returns>
            public static async Task SendEmailAsync(string[] to, string from, string[] CC, string[] BCC, string emailSubject, string emailMessage, bool isBodyHtml)
            {
                var message = new MailMessage();

                //message.To.Add( toEmailAddress );

                // from?
                message.From = new MailAddress(from);

                // to?
                if (to != null)
                {
                    foreach (string s in to)
                    {
                        if (s != null)
                        {
                            message.To.Add(s);
                        }
                    }
                }

                // CC?
                if (CC != null)
                {
                    foreach (string s in CC)
                    {
                        if (s != null)
                        {
                            message.To.Add(s);
                        }
                    }
                }

                // BCC?
                if (BCC != null)
                {
                    foreach (string s in BCC)
                    {
                        if (s != null)
                        {
                            message.To.Add(s);
                        }
                    }
                }


                message.Subject    = emailSubject;
                message.Body       = emailMessage;
                message.IsBodyHtml = isBodyHtml;

                message.From = new MailAddress(Configuration.Mail.GetMailServerLogin());


                //Proper Authentication Details need to be passed when sending email from gmail
                NetworkCredential mailAuthentication = new NetworkCredential(Configuration.Mail.GetMailServerLogin(), Configuration.Mail.GetMailServerPassword());

                using (var smtpClient = new SmtpClient())
                {
                    // server
                    smtpClient.Host                  = Configuration.Mail.GetMailServer();
                    smtpClient.Port                  = Configuration.Mail.GetMailServerPort();
                    smtpClient.EnableSsl             = Configuration.Mail.GetMailServerIsEnableSSL();
                    smtpClient.UseDefaultCredentials = false;
                    smtpClient.Credentials           = mailAuthentication;


                    if (Global.Configuration.Development.GetIsEnabledDeveloperMode())
                    {
                        smtpClient.Timeout = 5000;
                    }
                    else
                    {
                        smtpClient.Timeout = 180000;  //An Int32 that specifies the time-out value in milliseconds. The default value is 100,000 (100 seconds).
                    }
                    // Set the method that is called back when the send operation ends.
                    // smtpClient.SendCompleted += new SendCompletedEventHandler( SendCompletedCallback );

                    // The userState can be any object that allows your callback
                    // method to identify this send operation.
                    // For this example, the userToken is a string constant.
                    string userState = emailSubject.Replace(" ", "") + "_" + DateTime.Now.Ticks.ToString();


                    // send
                    try
                    {
                        await smtpClient.SendMailAsync(message);

                        // smtpClient.Send( message ); // only works with this...
                        // smtpClient.SendAsync( message, userState );
                    }
                    catch (Exception e)
                    {
                        Global.LogError(HttpContext.Current, EnumLogCategories.EMAIL, e.Message + Environment.NewLine + e.InnerException);
                    }
                }
            }