使用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/archive/2013/04/23/3038247.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值