コード例 #1
0
 public static bool Send(MailModel model)
 {
     try
     {
         var message = new MailMessage();
         message.From = new MailAddress(model.FromAddress);
         model.ToAddress.ForEach(m => message.To.Add(new MailAddress(m)));
         message.Subject = model.Subject;
         message.Body    = model.Body;
         var smtp = new SmtpClient("smtp.gmail.com");                // to app.config
         smtp.UseDefaultCredentials = false;                         // to app.config
         smtp.Credentials           = new NetworkCredential("", ""); // to app.config
         smtp.EnableSsl             = true;
         smtp.Port = 587;
         smtp.Send(message);
         return(true);
     }
     catch (Exception ex)
     {
         throw;
     }
 }
コード例 #2
0
        private void SendButton_Click(object sender, EventArgs e)
        {
            try
            {
                MailModel model = new MailModel(tbTo.Text, tbFrom.Text, tbTitle.Text, rtbBody.Text);

                if (!MailService.Send(model))
                {
                    MessageBox.Show("Mail sent successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (IncorrectRecipientException exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (IncorrectSenderException exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (MailServiceException exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }