示例#1
0
 private void btnSendMail_Click(object sender, EventArgs e)
 {
     try
     {
         string mailHost   = configurationManager.GetKey("SMTPHost");
         int    mailPort   = Int32.Parse(configurationManager.GetKey("SMTPPort"));
         string userName   = configurationManager.GetKey("EmailUsername");
         string password   = configurationManager.GetKey("EmailPassword");
         var    mail       = new MailMessage();
         var    mailClient = new SmtpClient(mailHost, mailPort);
         mail.From = new MailAddress(txtMailFrom.Text);
         string[] mails = txtMailTo.Text.Split(',');
         foreach (var m in mails)
         {
             mail.To.Add(m);
         }
         mail.Subject = txtMailSubject.Text;
         mail.Body    = txtMailMessage.Text;
         if (attachmentFileName != null)
         {
             mail.Attachments.Add(new Attachment(attachmentFileName));
         }
         mailClient.Port        = mailPort;
         mailClient.Credentials = new System.Net.NetworkCredential(userName, password);
         mailClient.EnableSsl   = (configurationManager.GetKey("SMTPSSL") == "yes");
         mailClient.Send(mail);
         MessageBox.Show(String.Format("Successfully sent mail to {0}!", mailTo));
         Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(String.Format("Failed to send e-mail, {0}", ex.Message));
     }
 }
示例#2
0
 private void frmMailSettings_Load(object sender, EventArgs e)
 {
     txtSMTPHost.Text      = configurationManager.GetKey("SMTPHost");
     txtSMTPPort.Text      = configurationManager.GetKey("SMTPPort");
     txtEmailUsername.Text = configurationManager.GetKey("EmailUsername");
     txtEmailPassword.Text = configurationManager.GetKey("EmailPassword");
     chkMailSSL.Checked    = (configurationManager.GetKey("SMTPSSL") == "yes");
 }