示例#1
0
        private void SendEmail(string btncommand)
        {
            try
            {
                var attachments = new List <FileAttachment>();
                if (fileAttachment.HasFile)
                {
                    var file = new FileAttachment
                    {
                        Name    = fileAttachment.FileName,
                        Content = Convert.ToBase64String(fileAttachment.FileBytes)
                    };
                    attachments.Add(file);
                }


                var request = new SendEmailRequest
                {
                    Body             = this.txtBody.Text,
                    FromEmailAddress = String.IsNullOrEmpty(this.txtFromEmail.Text) ? null : this.txtFromEmail.Text,
                    Recipients       = String.IsNullOrEmpty(this.txtTo.Text) ? null : this.txtTo.Text.Split(';'),
                    CarbonCopyList   = String.IsNullOrEmpty(this.txtCC.Text) ? null : this.txtCC.Text.Split(';'),
                    SessionId        = "6C06251C-377E-4803-A96F-5CC10490748B",
                    Subject          = this.txtSubject.Text,
                    Attachments      = attachments.Count > 0 ? attachments.ToArray() : null
                };
                System.Net.ServicePointManager.ServerCertificateValidationCallback =
                    ((sender, certificate, chain, sslPolicyErrors) => true);

                if (btncommand == "Sendmail")
                {
                    using (var proxy = new EmailServiceClient())
                    {
                        var response = proxy.SendEmail(request);
                        if (response != null && response.IsSuccess)
                        {
                            this.errorText.Visible = true;
                            this.errorText.Text    = "Email succeeded.";
                            return;
                        }
                        this.errorText.Visible = true;
                        this.errorText.Text    = "Email failed.";
                    }
                }

                else
                {
                    using (var proxy = new EmailServiceClient())
                    {
                        var response = proxy.SendEmailWithBadAddressCheck(request);
                        if (response != null && response.IsSuccess)
                        {
                            this.errorText.Visible = true;
                            this.errorText.Text    = "Email succeeded.";
                        }
                        else
                        {
                            this.errorText.Visible = true;
                            this.errorText.Text    = "Email failed.";
                        }
                        if (response.InvalidRecipients.BadDomains.Count() != 0)
                        {
                            this.errorText.Text += String.Format("\t Email address with bad domains list:-\t {0}", String.Join(",", response.InvalidRecipients.BadDomains));
                        }
                    }
                }
            }
            catch (FaultException <EmailServiceFault> fxException)
            {
                this.errorText.Visible = true;
                this.errorText.Text    = fxException.Detail.ToString();
            }
            catch (Exception ex)
            {
                this.errorText.Visible = true;
                this.errorText.Text    = ex.Message + ex.StackTrace;
            }
        }