• C#写的 电子邮件客户端(winform窗体)


    如果用QQ邮箱测试需要在邮箱里面设置里面开启POPSMTP协议。并用获取授权码输入到密码框。

    添加引用

    using System.Net.Mail;
    using System.Net;

     实例化:MailMessage mmsg = new MailMessage();

            /// <summary>
            /// 发送电子邮件
            /// </summary>
            /// <param name="sendEmailAddress">发件人邮箱地址</param>
            /// <param name="sendEmailPwd">发件人邮箱密码授权码</param>
            /// <param name="msgToEmail">收件人邮箱地址</param>
            /// <param name="title">邮箱标题</param>
            /// <param name="content">邮件内容</param>
            /// <param name="host">邮箱服务器</param>
            /// <returns></returns>
            public int SendEmail(string sendEmailAddress, string sendEmailPwd, string msgToEmail, string title, string content, string host)
            {
                //发件者邮箱地址
                string fjrtxt = sendEmailAddress;
                //发件者邮箱密码
                string mmtxt = sendEmailPwd;
                //主题
                string zttxt = title;
                //内容
                string nrtxt = content;
                string[] fasong = fjrtxt.Split('@');
                //设置邮件协议
                SmtpClient client = new SmtpClient(host);
    
                client.EnableSsl = true;
    
                client.UseDefaultCredentials = false;
                //通过网络发送到Smtp服务器
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                //通过用户名和密码 认证
                client.Credentials = new NetworkCredential(fasong[0].ToString(), mmtxt);
                //发件人和收件人的邮箱地址
                //   MailMessage mmsg = new MailMessage();        //表示电子邮件: MailMessage
    
                mmsg.From = new MailAddress(fjrtxt);         //邮件地址 :MailAddress
    
                mmsg.To.Add(new MailAddress(msgToEmail));
                //邮件主题
                mmsg.Subject = zttxt;
                //主题编码
                mmsg.SubjectEncoding = Encoding.UTF8;
                //邮件正文
                mmsg.Body = nrtxt;
                //正文编码
                mmsg.BodyEncoding = Encoding.UTF8;
                //设置为HTML格式
                mmsg.IsBodyHtml = true;
                //优先级
                mmsg.Priority = MailPriority.High;
                try
                {
                    client.Send(mmsg);
                    return 1;
                }
                catch
                {
                    return 0;
                }
            }
            
    View Code

    上传附件

            private void 上传附件button1_Click(object sender, EventArgs e)
            {
                // MailMessage mm = new MailMessage();
                OpenFileDialog openFile = new OpenFileDialog();
                if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    mmsg.Attachments.Add(new Attachment(openFile.FileName));
                    this.fujian.Text = openFile.FileName;
                }
            }

    发送按钮

     private void 发送button2_Click(object sender, EventArgs e)
            {
                int mesAge = SendEmail(fajianren.Text, mima.Text, shoujianren.Text, biaoti.Text, neirong.Text, "smtp.qq.com");//mima就是授权码
                if (mesAge == 1)
                    MessageBox.Show("发送成功!");
                else
                    MessageBox.Show("发送失败!");
            }

    有问题可以留言

  • 相关阅读:
    CentOS-6.8 最详细安装教程(贴镜像文件+多图)
    SSM 框架整合完整流程讲解(IDEA + Maven)
    【万字长文】Spring MVC 层层递进轻松入门 !
    Spring AOP 由浅入深学习教程【新手向】
    Spring 注解和XML两种方式配置IOC
    【万字长文】Spring框架 层层递进轻松入门 (IOC和DI)
    用idea搭建SSM项目,原来这么简单
    Java并发编程:什么是CAS?这回总算知道了
    Java并发编程:Java的四种线程池的使用,以及自定义线程工厂
    Java并发编程:Java线程池核心ThreadPoolExecutor的使用和原理分析
  • 原文地址:https://www.cnblogs.com/xmy-007/p/6428667.html
Copyright © 2020-2023  润新知