public async Task <ActionResult> Emails(ViewModels.EmailViewModel model)
        {
            if (ModelState.IsValid)
            {
                var body    = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                var message = new MailMessage();
                message.To.Add(new MailAddress("*****@*****.**")); // replace with valid value
                message.From       = new MailAddress(model.Email);           // replace with valid value
                message.Subject    = model.Subject.ToString();
                message.Body       = string.Format(body, model.Name, model.Email, model.Message);
                message.IsBodyHtml = true;

                using (var smtp = new SmtpClient())
                {
                    var credential = new System.Net.NetworkCredential
                    {                                       //Website's official e-mail username
                        UserName = "******", // replace with valid value
                        Password = ""                       // replace with valid value
                                                            //website's official e-mail password
                    };
                    smtp.Credentials = credential;
                    smtp.Host        = "smtp.gmail.com";
                    smtp.Port        = 587;
                    smtp.EnableSsl   = true;
                    await smtp.SendMailAsync(message);

                    return(RedirectToAction("Sent"));
                }
            }
            return(View(model));
        }
Esempio n. 2
0
        private static void OBackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            ViewModels.EmailViewModel oEmailViewModel = e.Argument as ViewModels.EmailViewModel;

            string smtpAddress = FindSTMPAddress(oEmailViewModel.SenderEmail);
            int    portNumber  = 587;
            bool   enableSSL   = true;

            System.Net.Mail.MailAddress oFromMailAddress = new System.Net.Mail.MailAddress(oEmailViewModel.SenderEmail, oEmailViewModel.DisplayName, System.Text.Encoding.UTF8);

            System.Net.Mail.MailAddress oToMailAddress = new System.Net.Mail.MailAddress(oEmailViewModel.ReceiverEmail);

            System.Net.Mail.MailMessage oMailMessage = new System.Net.Mail.MailMessage(oFromMailAddress, oToMailAddress);

            oMailMessage.Subject = oEmailViewModel.Subject;

            oMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;

            oMailMessage.Body = oEmailViewModel.Body;

            oMailMessage.BodyEncoding = System.Text.Encoding.UTF8;

            if (oEmailViewModel.Attachment != null)
            {
                oMailMessage.Attachments.Add(new System.Net.Mail.Attachment(contentStream: oEmailViewModel.Attachment, name: oEmailViewModel.AttachmentFileName + ".pdf", mediaType: "application/pdf"));
            }

            System.Net.Mail.SmtpClient oSmtpClient = new System.Net.Mail.SmtpClient(smtpAddress, portNumber);

            oSmtpClient.Credentials = new System.Net.NetworkCredential(oEmailViewModel.SenderEmail, oEmailViewModel.SenderPassword);

            oSmtpClient.EnableSsl = enableSSL;

            try
            {
                oSmtpClient.Send(oMailMessage);

                Utility.EmailSentSuccessfully = true;
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Utility.Exception = ex;

                Utility.EmailSentSuccessfully = false;
            }
        }
Esempio n. 3
0
        public static void SendEmail(string senderEmail, string senderPassword, string displayName, string receiverEmail, string subject, string body, System.IO.Stream attachment, string attachmentName)
        {
            ViewModels.EmailViewModel oEmailViewModel = new ViewModels.EmailViewModel();

            oEmailViewModel.Attachment         = attachment;
            oEmailViewModel.SenderEmail        = senderEmail;
            oEmailViewModel.SenderPassword     = senderPassword;
            oEmailViewModel.DisplayName        = displayName;
            oEmailViewModel.ReceiverEmail      = receiverEmail;
            oEmailViewModel.Subject            = subject;
            oEmailViewModel.Body               = body;
            oEmailViewModel.AttachmentFileName = attachmentName;

            System.ComponentModel.BackgroundWorker oBackgroundWorker = new System.ComponentModel.BackgroundWorker();

            oBackgroundWorker.DoWork             += OBackgroundWorker_DoWork;
            oBackgroundWorker.RunWorkerCompleted += OBackgroundWorker_RunWorkerCompleted;

            oBackgroundWorker.RunWorkerAsync(oEmailViewModel);
        }