使用MailMessage和SmtpClient发送邮件

 Console.WriteLine("开始发送邮件....");
            //1.MailMessage
            //1.创建一封邮件
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("szyuxueliang@163.com");
            msg.To.Add(new MailAddress("1027206433@qq.com"));
            msg.Subject = "======去给我带份饭。=======";
            msg.SubjectEncoding = Encoding.GetEncoding("gb2312");
            msg.Body = "==================在中午12:00之前把饭送过来。================";

            //创建一个html格式的文档
            AlternateView aview = AlternateView.CreateAlternateViewFromString("在中午<h1><font color=\"red\">12:00</font></h1>之前把饭送过来。<br/><img src=\"cid:img001\"/>", Encoding.UTF8, "text/html");

            //为邮件中增加一张图片
            LinkedResource resource = new LinkedResource(@"E:\ASP.NET WorkPlace\MyArticle\MyArticle.WebSite\Content\images\loading.gif");
            resource.ContentId = "img001";

            aview.LinkedResources.Add(resource);
            msg.AlternateViews.Add(aview);

            //为邮件增加一些附件
            Attachment attchFile1 = new Attachment(@"E:\ASP.NET WorkPlace\MyArticle\MyArticle.WebSite\Content\images\addarticle.gif");
           // Attachment attchFile2 = new Attachment(@"E:\HTML.5与CSS.3权威指南.pdf");
            //Attachment attchFile3 = new Attachment(@"E:\传智播客视频\【ASP.NET≠拖控件!】-传智播客ASP.NET高手之路视频教程01_单线程缺点和简单多线程操作(1).zip");

            msg.Attachments.Add(attchFile1);
           // msg.Attachments.Add(attchFile2);
            //msg.Attachments.Add(attchFile3);

 

            //2.SmtpClient

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.163.com";
            smtp.Credentials = new NetworkCredential("szyuxueliang", "哈哈不告诉你!");
            smtp.Send(msg);
            Console.WriteLine("发送完毕!");
            Console.ReadKey();

原文地址:https://www.cnblogs.com/yxlblogs/p/3038247.html