コード例 #1
0
        public static void SendEmail(string smtpServerAddress, string smtpUser, string smtpPassword, int smtpPort, string toEmailAddress, string fromEmailAddress, string subject, string body, string fileName, bool deleteFile, bool bypassSslErrors)
        {
            var mail       = new MailMessage();
            var smtpServer = new SmtpClient(smtpServerAddress);

            try
            {
                mail.From = new MailAddress(fromEmailAddress);
                mail.To.Add(toEmailAddress);
                mail.Subject = subject;
                mail.Body    = body;

                if (!string.IsNullOrEmpty(fileName))
                {
                    fileName.Split(',').ToList().ForEach(x => mail.Attachments.Add(new Attachment(x)));
                }

                smtpServer.Port        = smtpPort;
                smtpServer.Credentials = new NetworkCredential(smtpUser, smtpPassword);
                smtpServer.EnableSsl   = true;
                if (bypassSslErrors)
                {
                    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); }
                }
                ;
                smtpServer.Send(mail);
            }
            catch (Exception e)
            {
                AppServices.LogError(e);
            }
            finally
            {
                if (deleteFile && !string.IsNullOrEmpty(fileName))
                {
                    fileName.Split(',').ToList().ForEach(
                        x =>
                    {
                        if (File.Exists(x))
                        {
                            try
                            {
                                File.Delete(x);
                            }
                            catch (Exception) { }
                        }
                    });
                }
            }
        }