明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

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());
            }
        }
 
==================================================================
//************************接收邮件代码: