• C# 2.0 利用自身的类发送与接收邮件。。。。。。。。。。。。。。。。


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


    //************************发送邮件代码:
      System.Net.Mail.SmtpClient client = new SmtpClient();
                client.Host = "smtp.qq.com";
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("账号", "密码");
                //星号改成自己邮箱的密码
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage message = new MailMessage("uuu@qq.com", "uu@qq.com");
                message.Subject = "测试";
                message.Body = "用自己写的软件发的邮件!";
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;

                //添加附件
                //Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
                //message.Attachments.Add(data);

                try
                {
                    client.Send(message);
                    MessageBox.Show("Email 成功 send.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Send Email 失败." + ex.ToString());
                }

    //*****邮件是否发送成功,要看目标邮箱是否允许或已打开pop3或smtp功能??请检查。

    //-----------------------------------------发送给gmail邮箱的代码:
     //***************------------------------------------------------------------------------

     System.Net.Mail.SmtpClient client = new SmtpClient();
                client.Host = "smtp.gmail.com";
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("账号", "密码");
                //星号改成自己邮箱的密码
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage message = new MailMessage("hh00@gmail.com", "hh00@gmail.com");
                message.Subject = "测试";
                message.Body = "用自己写的软件发的邮件!";
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;

                client.EnableSsl = true;   //**安全认证,gmial邮箱设置开启pop3功能后,这个属性同样要设置为true
                
                //添加附件
                //Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
                //message.Attachments.Add(data);

                try
                {
                    client.Send(message);
                    MessageBox.Show("Email 成功 send.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Send Email 失败." + ex.ToString());
                }

      //-----------------------------------------------------------

    ==================================================================

    //********线程异步发送邮件
     
    private void Form2_Load(object sender, EventArgs e)
            {
                Thread thread = new Thread(new ThreadStart(Updating));
                thread.Start();
            }

            private void Updating()
            {
                System.Net.Mail.SmtpClient client = new SmtpClient();
                client.Host = "smtp.gmail.com";
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("ddddd", "密码");
                //星号改成自己邮箱的密码
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage message = new MailMessage("ddddd@gmail.com", "ddddd@gmail.com");
                message.Subject = "测试9999999999999999";
                message.Body = "用自己写的软件发的邮件!";
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;

                client.EnableSsl = true;

                //添加附件
                //Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
                //message.Attachments.Add(data);

                try
                {
                    client.Send(message);
                    //MessageBox.Show("Email 成功 send.");
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("Send Email 失败." + ex.ToString());
                }
            }
     
    ==================================================================
    //************************接收邮件代码:

  • 相关阅读:
    了解一些常用的牛逼编译器(不限制编程语言, 不限制平台)
    Linux下的常用文本编辑器
    linux下一些重要命令的了解
    linux学习笔记(二:权限)
    liunx学习笔记(一:常用命令)
    文件操作相关的函数总结
    关于动态内存malloc和realloc
    实现一个简易的通讯录
    qsort函数排序各种类型的数据。
    结构体总结
  • 原文地址:https://www.cnblogs.com/Fooo/p/1230471.html
Copyright © 2020-2023  润新知