コード例 #1
0
        public static bool SendMail(string fromAddress, string toAddress, string subject, string body, string cc, string sectionName, string attachmentFilename = "", string pathImageEmbedded = "", string bcc = "")
        {
            string       sectionPath = "mailSettings/" + sectionName;
            SmtpSection  mailSetting = (SmtpSection)ConfigurationManager.GetSection(sectionPath);
            string       HOST        = mailSetting.Network.Host;
            int          PORT        = mailSetting.Network.Port;
            string       USERNAME    = mailSetting.Network.UserName;
            var          parent      = Directory.GetParent(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).FullName).FullName;
            StreamReader sr          = new StreamReader(parent + "\\abc.txt");

            sr.ReadLine();
            sr.ReadLine();
            sr.ReadLine();
            sr.ReadLine();
            sr.ReadLine();
            sr.ReadLine();
            sr.ReadLine();
            string mailPassword    = sr.ReadLine();
            string PASSWORD        = Sercurity.Decrypt(mailPassword);
            string ADMIN_MAIL      = mailSetting.From;
            string ADMIN_MAIL_NAME = "AnonymousBidder Administrator";
            string mailFrom        = !string.IsNullOrEmpty(fromAddress) ? fromAddress : ADMIN_MAIL;

            try
            {
                MailMessage mailMessage = BuildMailMessage(ADMIN_MAIL_NAME, mailFrom, toAddress, subject, body, pathImageEmbedded);

                if (!string.IsNullOrEmpty(cc))
                {
                    var toCcList = cc.Split(',');

                    if (toCcList != null && toCcList.Any())
                    {
                        foreach (var toCc in toCcList)
                        {
                            mailMessage.CC.Add(new MailAddress(toCc));
                        }
                    }
                }

                if (!string.IsNullOrEmpty(bcc))
                {
                    var toBccList = bcc.Split(',');

                    if (toBccList != null && toBccList.Any())
                    {
                        foreach (var toBcc in toBccList)
                        {
                            mailMessage.Bcc.Add(new MailAddress(toBcc));
                        }
                    }
                }


                if (!string.IsNullOrEmpty(attachmentFilename))
                {
                    Byte[]     bytes = Convert.FromBase64String(attachmentFilename);
                    Attachment att   = new Attachment(new MemoryStream(bytes), "test.png");
                    mailMessage.Attachments.Add(att);
                }


                using (var smtpClient = new SmtpClient(HOST, PORT))
                {
                    var credential = new NetworkCredential(USERNAME, PASSWORD);
                    smtpClient.Credentials = credential;
                    smtpClient.EnableSsl   = true;
                    smtpClient.Send(mailMessage);
                    mailMessage.Dispose();
                }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);

                throw ex;
            }
        }
コード例 #2
0
ファイル: Utilities.cs プロジェクト: wynn987/AnonymousBidder
        public static bool SendMail(string fromAddress, string toAddress, string subject,
                                    string body, string cc, string sectionName,
                                    string pathImageEmbedded = "", List <Attachment> attachment = null, string fromName = "", string bcc = "")
        {
            bool        flag;
            string      str       = string.Concat("mailSettings/", sectionName);
            SmtpSection section   = (SmtpSection)ConfigurationManager.GetSection(str);
            string      host      = section.Network.Host;
            int         port      = section.Network.Port;
            string      userName  = section.Network.UserName;
            string      str1      = Sercurity.Decrypt(section.Network.Password);
            string      from      = section.From;
            bool        enableSsl = section.Network.EnableSsl;
            string      str2      = string.IsNullOrEmpty(fromName) ? "Administrator" : fromName;
            string      str3      = (!string.IsNullOrEmpty(fromAddress) ? fromAddress : from);

            try
            {
                MailMessage mailMessage = Utilities.BuildMailMessage(str2, str3, toAddress, subject, body, pathImageEmbedded, attachment);
                if (!string.IsNullOrEmpty(cc))
                {
                    string[] strArrays = cc.Split(new char[] { ',' });
                    if ((strArrays == null ? false : strArrays.Any <string>()))
                    {
                        string[] strArrays1 = strArrays;
                        for (int i = 0; i < (int)strArrays1.Length; i++)
                        {
                            string str4 = strArrays1[i];
                            mailMessage.CC.Add(new MailAddress(str4));
                        }
                    }
                }
                if (!string.IsNullOrEmpty(bcc))
                {
                    string[] bccArrays = bcc.Split(new char[] { ',' });
                    if ((bccArrays == null ? false : bccArrays.Any <string>()))
                    {
                        string[] bccArrays1 = bccArrays;
                        for (int i = 0; i < (int)bccArrays1.Length; i++)
                        {
                            string bcc4 = bccArrays1[i];
                            mailMessage.Bcc.Add(new MailAddress(bcc4));
                        }
                    }
                }
                SmtpClient smtpClient = new SmtpClient(host, port);
                try
                {
                    smtpClient.Credentials = new NetworkCredential(userName, str1);
                    smtpClient.EnableSsl   = enableSsl;
                    smtpClient.Send(mailMessage);
                    mailMessage.Dispose();
                }
                finally
                {
                    if (smtpClient != null)
                    {
                        ((IDisposable)smtpClient).Dispose();
                    }
                }
                flag = true;
            }
            catch (Exception exception)
            {
                flag = false;
            }
            return(flag);
        }