C#发邮件

private void SendEmail(object sender, RoutedEventArgs e)
        {
            if (!PingIpOrDomainName("www.google.com"))
            {
                mainViewModel.MessageText = "Network impassability!";return;
            }
            string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            if (!string.IsNullOrEmpty(txtTo.Text))
            {
                var list = from s in txtTo.Text.Split(';') where s != "" select s;
                foreach (var item in list)
                {
                    Match m = Regex.Match(item, pattern);
                    if (!m.Success)
                    {
                        mainViewModel.MessageText = "The format of email address is incorrect!";return;
                    }
                }
            }
            else
            {
                mainViewModel.MessageText = "The recipient is required!";return;
            }
            if (!string.IsNullOrEmpty(txtCC.Text))
            {
                var list = from s in txtCC.Text.Split(';') where s != "" select s;
                foreach (var item in list)
                {
                    Match m = Regex.Match(item, pattern);
                    if (!m.Success)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("The format of email address is incorrect.");
                        return;
                    }
                }
            }

            PatientPanelViewModel dataContext = this.DataContext as PatientPanelViewModel;

            SmtpClient SmtpClient = new SmtpClient();   //设置SMTP协议
            SmtpClient.Host = "smtp.163.com";//指定SMTP服务名  例如QQ邮箱为 smtp.qq.com 新浪cn邮箱为 smtp.sina.cn等
            SmtpClient.Port = 25; //指定端口号
            SmtpClient.Timeout = 0;  //超时时间

            //创建服务器认证
            SmtpClient.Credentials = new NetworkCredential("xxxx@163.com", "xxxx");

            MailMessage MailMessage_Mai = new MailMessage();
            //清空历史发送信息 以防发送时收件人收到的错误信息(收件人列表会不断重复)
            MailMessage_Mai.To.Clear();
            var listTo = from s in txtTo.Text.Split(';') where s != "" select s;
            foreach (var item in listTo)
            {
                MailMessage_Mai.To.Add(item);
            }
            //抄送
            MailMessage_Mai.CC.Clear();
            var listCC = from s in txtCC.Text.Split(';') where s != "" select s;
            foreach (var item in listCC)
            {
                MailMessage_Mai.CC.Add(item);
            }
            //发件人邮箱
            MailMessage_Mai.From = new MailAddress("xxxx@163.com", "xxxx"); ;
            //邮件主题
            MailMessage_Mai.Subject = txtSubject.Text;
            MailMessage_Mai.SubjectEncoding = System.Text.Encoding.Unicode;
            //邮件正文
            MailMessage_Mai.Body = txtBody.Text;
            MailMessage_Mai.BodyEncoding = System.Text.Encoding.Unicode;
            //清空历史附件  以防附件重复发送
            MailMessage_Mai.Attachments.Clear();
            //添加附件
            // Read byte[] from png file
            FileInfo fileInfo = new FileInfo(dataContext.SelectedImage);
            BinaryReader binReader = new BinaryReader(File.Open(fileInfo.FullName, FileMode.Open));
            byte[] bytes = binReader.ReadBytes((int)fileInfo.Length);
            binReader.Close();
            MailMessage_Mai.Attachments.Add(new Attachment(new MemoryStream(bytes), fileInfo.Name));
            //开始发送邮件
            SmtpClient.SendAsync(MailMessage_Mai, "message");
            Messenger.Default.Send(new NotificationMessage("CloseSubWindow"), "ShareChildWindow");
        }

 

 private bool PingIpOrDomainName(string strIpOrDName)
        {
            try
            {
                Ping objPingSender = new Ping();
                PingOptions objPinOptions = new PingOptions();
                objPinOptions.DontFragment = true;
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes("");
                PingReply objPinReply = objPingSender.Send(strIpOrDName, 1000, buffer, objPinOptions);
                string strInfo = objPinReply.Status.ToString();
                if (strInfo == "Success")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

  

posted @ 2013-07-22 10:55  wxing67  阅读(272)  评论(0编辑  收藏  举报