private void LoadMailConfig() { MailInfo info = mailUtil.DeserializeMailInfo(mailConfigPath); eSender.Text = info.Sender; eReceiver.Text = info.Receiver; eSmtp.Text = info.Smtp; ePort.Value = info.Port; if (info.SSL) { rbSSLOn.Checked = true; } else { rbSSLOff.Checked = true; } }
public void SendMail(MailInfo mailInfo, Order order) { if (String.IsNullOrWhiteSpace(mailInfo.Sender)) { throw new ArgumentNullException("sender", "MailUtil.SendMail: sender cannot be empty or null."); } if (String.IsNullOrWhiteSpace(mailInfo.Receiver)) { throw new ArgumentNullException("receiver", "MailUtil.SendMail: receiver cannot be empty or null."); } if (String.IsNullOrWhiteSpace(mailInfo.Smtp)) { throw new ArgumentNullException("smtp", "MailUtil.SendMail: smtp cannot be empty or null."); } if (order == null) { throw new ArgumentNullException("order", "MailUtil.SendMail: order cannot be null."); } try { SmtpClient client = new SmtpClient(mailInfo.Smtp, mailInfo.Port); client.EnableSsl = mailInfo.SSL; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(mailInfo.Sender, mailInfo.Password); client.DeliveryMethod = SmtpDeliveryMethod.Network; using (MailMessage mail = new MailMessage(mailInfo.Sender, mailInfo.Receiver)) { mail.Subject = "Restaurant order"; mail.Body = order.ToString(); client.Send(mail); } } catch (Exception exc) { throw new Exception(String.Format("MailUtil.SendMail: {0}", exc.Message), exc); } }