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;
            }
        }

  

转载于:https://www.cnblogs.com/wxjing67/p/3204889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值