【C#学习笔记】smtp发邮件

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("***@gmail.com");
            mail.To.Add(new MailAddress("***@sina.cn"));
            mail.Subject = "邮件标题";
            mail.SubjectEncoding = Encoding.UTF8;
            mail.Body = "邮件内容";
            mail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient(); 
            smtp.Host = "smtp.gmail.com";       //不同的邮箱这个不一样
            smtp.Port = 587;                    //大部分邮箱为25,gmail为587
            smtp.Credentials = new NetworkCredential("账号","密码");    //gmail的账号和密码
            smtp.EnableSsl = true;               //其他邮箱不需要ssl
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;   //其他邮箱也不需要这个

            smtp.Send(mail);

            Console.Write("this work");
            Console.Read();
        }      
    }
}

 

posted @ 2017-08-27 15:11  Dsp Tian  阅读(500)  评论(0编辑  收藏  举报