Ejemplo n.º 1
0
        public void TestMethodSend()
        {
            string s = "1234567890";

            Assert.IsTrue(mail.Send(s));
            info.Pass = "******";
            mail      = new ToMail(info);
            Assert.IsFalse(mail.Send(s));
        }
Ejemplo n.º 2
0
        public static void SendExceptionEmail(Exception exmail, string processName, int departmentId = 0, string userName = "")
        {
            String ErrorlineNo, Errormsg, ErrorLocation, InnerException, extype, Frommail, ToMail, Sub, HostAdd, EmailHead, EmailSing;

            try
            {
                var newline = "<br/>";
                ErrorlineNo   = exmail.StackTrace.Substring(exmail.StackTrace.Length - 7, 7);
                Errormsg      = exmail.GetType().Name.ToString();
                extype        = exmail.GetType().ToString();
                ErrorLocation = exmail.Message.ToString();

                if (exmail.InnerException != null)
                {
                    InnerException = exmail.InnerException.ToString();
                }
                else
                {
                    InnerException = "No Inner Exception";
                }

                EmailHead = "<b>Dear Team,</b>" + "<br/>" + "An exception occurred in [" + processName + "] " + "With following Details" + "<br/>" + "<br/>";
                EmailSing = newline + "Thanks and Regards" + newline + "    " + "     " + "<b>Application Admin </b>" + "</br>";
                Sub       = "Exception occurred" + " " + "in [" + processName + "]";

                string errortomail = EmailHead + "<b>Log Written Date: </b>" + " " + DateTime.Now.ToString() + newline + "<b>Error Line No :</b>" + " " + ErrorlineNo + "\t\n" + " " + newline + "<b>Error Message:</b>" + " " + Errormsg + newline + "<b>Exception Type:</b>" + " " + extype + newline + "<b> Error Details :</b>" + " " + ErrorLocation + newline + "<b> Inner Details :</b>" + " " + InnerException + newline + "<b>System:</b>" + " " + processName + newline + $"<b>DepartmentId: {departmentId}</b>" + newline + $"<b>Username: {userName}</b>" + newline + newline + exmail.StackTrace + newline + newline + EmailSing;

                using (MailMessage mailMessage = new MailMessage())
                {
                    Frommail               = OutboundEmailServerConfig.FromMail;
                    ToMail                 = OutboundEmailServerConfig.ToMail;
                    mailMessage.From       = new MailAddress(Frommail);
                    mailMessage.Subject    = Sub;
                    mailMessage.Body       = errortomail;
                    mailMessage.IsBodyHtml = true;

                    string[] MultiEmailId = ToMail.Split(',');
                    foreach (string userEmails in MultiEmailId)
                    {
                        mailMessage.To.Add(new MailAddress(userEmails));
                    }

                    SmtpClient smtp = new SmtpClient();
                    smtp.Host      = OutboundEmailServerConfig.Host;
                    smtp.EnableSsl = OutboundEmailServerConfig.EnableSsl;
                    NetworkCredential NetworkCred = new NetworkCredential();
                    NetworkCred.UserName       = OutboundEmailServerConfig.UserName;
                    NetworkCred.Password       = OutboundEmailServerConfig.Password;
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials           = NetworkCred;
                    smtp.Port = OutboundEmailServerConfig.Port;
                    smtp.Send(mailMessage);                     //sending Email
                }
            }
            catch { }
        }
Ejemplo n.º 3
0
        public bool SendMail()
        {
            //基础验证
            if (SendMailAddr.IsNullOrEmpty())
            {
                return(false);
            }
            if (!ToMail.IsEmail())
            {
                return(false);
            }
            if (SmtpHost.IsNullOrEmpty())
            {
                return(false);
            }
            MailMessage message     = new MailMessage();
            SmtpClient  mSmtpClient = new SmtpClient();

            mSmtpClient.Host = SmtpHost;
            mSmtpClient.Port = Port;
            mSmtpClient.UseDefaultCredentials = false;
            mSmtpClient.EnableSsl             = IsSSL;

            if (IsAuth)
            {
                NetworkCredential nc = new NetworkCredential(SendMailAddr, Password);
                mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
            }
            else
            {
                mSmtpClient.Credentials = new NetworkCredential(SendMailAddr, Password);
            }
            message.DeliveryNotificationOptions = DeliveryNotificationOptions.None;
            mSmtpClient.DeliveryMethod          = SmtpDeliveryMethod.Network;
            try
            {
                //// 加载发件人地址
                if (!SendUserNickName.IsNullOrEmpty())
                {
                    MailAddress from = new MailAddress(SendMailAddr, SendUserNickName);
                    message.From   = from;
                    message.Sender = from;
                }
                else
                {
                    MailAddress from = new MailAddress(SendMailAddr);
                    message.From   = from;
                    message.Sender = from;
                }

                // 加载收件人地址
                message.To.Add(new MailAddress(ToMail));

                // 加载标题
                message.Subject         = Subject;
                message.SubjectEncoding = Encoding.UTF8;
                // 设置发送时间
                //message.setSentDate(new Date());

                // 设置抄送
                if (null != CcList)
                {
                    foreach (String ccMail in CcList)
                    {
                        if (ccMail.IsEmail())
                        {
                            message.CC.Add(new MailAddress(ccMail));
                        }
                    }
                }
                // 设置抄送
                if (null != BccList)
                {
                    foreach (String bccMail in BccList)
                    {
                        if (bccMail.IsEmail())
                        {
                            message.Bcc.Add(new MailAddress(bccMail));
                        }
                    }
                }
                //添加正文内容,判断是否为html格式

                message.Body         = Content;
                message.IsBodyHtml   = IsHtml;
                message.BodyEncoding = Encoding.UTF8;
                message.Priority     = MailPriority.Normal;

                // 添加附件
                if (null != AttrList)
                {
                    ContentDisposition disposition;
                    Attachment         data;
                    foreach (String filePath in AttrList)
                    {
                        if (!File.Exists(filePath))
                        {
                            continue;
                        }
                        data        = new Attachment(filePath, MediaTypeNames.Application.Octet);
                        disposition = data.ContentDisposition;
                        disposition.CreationDate     = File.GetCreationTime(filePath);
                        disposition.ModificationDate = File.GetLastWriteTime(filePath);
                        disposition.ReadDate         = File.GetLastAccessTime(filePath);
                        message.Attachments.Add(data);
                    }
                }
                // 把邮件发送出去
                mSmtpClient.Send(message);
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }