예제 #1
0
        public async Task <bool> SendEmail(int fromEmpID, String msgFrom, string msgTo, string Subject, string msgBody)
        {
            MailMessage mailMessage     = new MailMessage();
            string      strPassword     = ServiceEmployee.GetEmailPasswordForEmployee(fromEmpID);
            string      strEmailAddress = ServiceEmployee.GetEmployeeEmailByID(fromEmpID);
            string      strEmpName      = ServiceEmployee.GetEmployeeNameByID(fromEmpID);

            if (strPassword == string.Empty)
            {
                MessageBox.Show("You cannot send Emails, unless you set your valid Password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }


            if (!new EmailAddressAttribute().IsValid(strEmailAddress))
            {
                MessageBox.Show("It appears that you have not set your Email Address in the Employee database or is Invalid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            mailMessage.From = new MailAddress(strEmailAddress, strEmpName);
            foreach (var address in msgTo.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (new EmailAddressAttribute().IsValid(address))
                {
                    mailMessage.To.Add(address);
                }
            }
            if (mailMessage.To.Count == 0)
            {
                MessageBox.Show("Invalid number of Recipients or Invalid Email Address", "Terminating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            mailMessage.Subject    = Subject;
            mailMessage.Body       = msgBody;
            mailMessage.IsBodyHtml = false;

            //SmtpClient smtp = new SmtpClient(Properties.Settings.Default.EMAIL_SERVER.ToString());
            //smtp.Port = int.Parse(Properties.Settings.Default.EMAIL_PORT.ToString());
            //smtp.Credentials = new NetworkCredential(strEmailAddress, strPassword);
            //smtp.EnableSsl = Properties.Settings.Default.EMAIL_ENABLE_SSL;
            SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["EMAIL_SERVER"].ToString());

            smtp.Port        = int.Parse(ConfigurationManager.AppSettings["EMAIL_PORT"].ToString());
            smtp.Credentials = new NetworkCredential(strEmailAddress, strPassword);
            smtp.EnableSsl   = (ConfigurationManager.AppSettings["EMAIL_ENABLE_SSL"].ToString() == "True") ? true : false;



            await smtp.SendMailAsync(mailMessage);

            return(true);
        }
예제 #2
0
        private void btnSendMail_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ValidateChildren())
                {
                    MailMessage mailMessage = new MailMessage();
                    string      strPassword = ServiceEmployee.GetEmailPasswordForEmployee(Program.CURR_USER.EmployeeID);
                    if (strPassword == string.Empty)
                    {
                        frmSetPassword frm = new frmSetPassword(PASSWORD_TYPE.EMAIL_PASSWORD);
                        if (frm.ShowDialog() == DialogResult.OK)
                        {
                            strPassword = frm.txtPassword.Text.Trim();
                        }
                        else
                        {
                            MessageBox.Show("You cannot send Emails, unless you set your valid Password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (!new EmailAddressAttribute().IsValid(Program.CURR_USER.EmailAddress))
                    {
                        MessageBox.Show("It appears that you have not set your Email Address in the Employee database or is Invalid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    mailMessage.From = new MailAddress(Program.CURR_USER.EmailAddress);
                    foreach (var address in txtMailTo.Text.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (new EmailAddressAttribute().IsValid(address))
                        {
                            mailMessage.To.Add(address);
                        }
                    }
                    if (mailMessage.To.Count == 0)
                    {
                        MessageBox.Show("Invalid number of Recipients found. orInvalid Email Address", "Terminating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    mailMessage.Subject    = txtSubject.Text;
                    mailMessage.Body       = txtMessage.Text;
                    mailMessage.IsBodyHtml = false;
                    if (this.AttachedFiles.Count > 0)
                    {
                        foreach (KeyValuePair <string, string> pair in AttachedFiles)
                        {
                            Attachment attachment = new Attachment(pair.Value);
                            if (attachment != null)
                            {
                                mailMessage.Attachments.Add(attachment);
                            }
                        }
                    }

                    //SmtpClient smtp = new SmtpClient(Properties.Settings.Default.EMAIL_SERVER.ToString());
                    //smtp.Port =int.Parse(Properties.Settings.Default.EMAIL_PORT.ToString());
                    //smtp.Credentials = new NetworkCredential(Program.CURR_USER.EmailAddress, strPassword);
                    //smtp.EnableSsl = Properties.Settings.Default.EMAIL_ENABLE_SSL;

                    SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["EMAIL_SERVER"].ToString());
                    smtp.Port        = int.Parse(ConfigurationManager.AppSettings["EMAIL_PORT"].ToString());
                    smtp.Credentials = new NetworkCredential(Program.CURR_USER.EmailAddress, strPassword);
                    smtp.EnableSsl   = (ConfigurationManager.AppSettings["EMAIL_ENABLE_SSL"].ToString() == "True") ? true : false;


                    smtp.SendAsync(mailMessage, mailMessage.Subject);
                    smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "frmSendMail::btnSendMail_Click", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }