예제 #1
0
        private void buttonSend_Click(object sender, EventArgs e)
        {
            SMTPComposer composer = new SMTPComposer();
            SMTPBuilder  builder  = new SMTPBuilder();

            builder.FormEmailManager = this;
            composer.ComposeEmail(builder);
            this.Email = builder.GetComplexProduct();

            try
            {
                var    fromAddress  = new MailAddress(this.Email.SenderAddress.Address, this.Email.SenderAddress.User);
                var    toAddress    = new MailAddress(this.Email.RecieverAddress.Address, this.Email.RecieverAddress.User);
                string fromPassword = this.Email.SenderPassword;
                string subject      = this.Email.MailSubject;
                string body         = this.Email.MailBody;

                var smtp = new SmtpClient
                {
                    Host                  = this.Email.Host,
                    Port                  = this.Email.Port,
                    EnableSsl             = true,
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(fromAddress.Address, fromPassword)
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body
                })
                {
                    smtp.Send(message);
                    MessageBox.Show("Email sent successfully", string.Empty, MessageBoxButtons.OK);
                    this.Close();
                }
            }
            catch (Exception excption)
            {
                DialogResult dialogResult = MessageBox.Show("Invalid sender credentials. try again?", string.Empty, MessageBoxButtons.OKCancel);
                if (dialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    this.Close();
                }
                else
                {
                    dialogResult = MessageBox.Show("Enter again email sender and email password", string.Empty, MessageBoxButtons.OK);
                    this.textBoxSenderEmailAddress.Text  = string.Empty;
                    this.textBoxSenderEmailPassword.Text = string.Empty;
                }
            }
        }
예제 #2
0
 public FormEmailManager()
 {
     this.InitializeComponent();
     this.Email = new MyEmail();
 }
예제 #3
0
 public SMTPBuilder()
 {
     Email = new MyEmail();
 }