.net开发之邮件发送
的有关信息介绍如下:在软件开发的过程中,可能会要用到邮件发送的情况,今天通过一个小例子,为大家展示,纯文本,html,图片,附件等情况的邮件发送。
新建项目,命名为通过程序来发邮件,如下图所示:
邮件发送需要引入两个头文件,如下图所示:
演示一封纯文本邮件发送例子,如下图所示,代码如下。
#region 发一封普通的邮件(纯文本)
Console.WriteLine("开始发送邮件....");
//1.MailMessage
//1.创建一封邮件
MailMessage msg = new MailMessage();
msg.From = new MailAddress("gy@cheshi.net");
msg.To.Add(new MailAddress("lb@cheshi.net"));
msg.Subject = "去给我带份饭。";
msg.SubjectEncoding = Encoding.GetEncoding("gb2312");
msg.Body = "在中午12:00之前把饭送过来。";
//2.SmtpClient
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
smtp.Credentials = new NetworkCredential("sk", "123");
smtp.Send(msg);
Console.WriteLine("发送完毕!");
Console.ReadKey();
#endregion
在邮件的发送过程中,经常会用到需要发送html格式的情况,下面演示这种情况:
#region 发html格式邮件,第一种方式
Console.WriteLine("开始发送邮件....");
//1.MailMessage
//1.创建一封邮件
MailMessage msg = new MailMessage();
msg.From = new MailAddress("gy@cheshi.net");
msg.To.Add(new MailAddress("liubei@cheshi.net"));
msg.Subject = "去给我带份饭。";
msg.SubjectEncoding = Encoding.GetEncoding("gb2312");
msg.Body = "在中午12:00
之前把饭送过来。";
//设置文本正文是否是html格式的。
msg.IsBodyHtml = true;
//2.SmtpClient
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
smtp.Credentials = new NetworkCredential("sk", "123");
smtp.Send(msg);
Console.WriteLine("发送完毕!");
Console.ReadKey();
#endregion
下面演示html格式邮件发送的另一种方式:
#region 发送html格式的邮件 ,第二种方式
Console.WriteLine("开始发送邮件....");
//1.MailMessage
//1.创建一封邮件
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sk@cheshi.net");
msg.To.Add(new MailAddress("yzk@cheshi.net"));
msg.Subject = "======去给我带份饭。=======";
msg.SubjectEncoding = Encoding.GetEncoding("gb2312");
msg.Body = "==================在中午12:00之前把饭送过来。================";
//创建一个html格式的文档
AlternateView aview = AlternateView.CreateAlternateViewFromString("在中午
12:00
之前把饭送过来。", Encoding.UTF8, "text/html");msg.AlternateViews.Add(aview);
//2.SmtpClient
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
smtp.Credentials = new NetworkCredential("sk", "123");
smtp.Send(msg);
Console.WriteLine("发送完毕!");
Console.ReadKey();
#endregion
在邮件在插入图片发送,这个是非常常见的:
#region 在邮件中带图片
Console.WriteLine("开始发送邮件....");
//1.MailMessage
//1.创建一封邮件
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sk@cheshi.net");
msg.To.Add(new MailAddress("yzk@cheshi.net"));
msg.Subject = "======去给我带份饭。=======";
msg.SubjectEncoding = Encoding.GetEncoding("gb2312");
msg.Body = "==================在中午12:00之前把饭送过来。================";
//创建一个html格式的文档
AlternateView aview = AlternateView.CreateAlternateViewFromString("在中午
12:00
之前把饭送过来。", Encoding.UTF8, "text/html");
//为邮件中增加一张图片
LinkedResource resource = new LinkedResource(@"c:\toYou.gif");
resource.ContentId = "img001";
aview.LinkedResources.Add(resource);
msg.AlternateViews.Add(aview);
//2.SmtpClient
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
smtp.Credentials = new NetworkCredential("sk", "123");
smtp.Send(msg);
Console.WriteLine("发送完毕!");
Console.ReadKey();
#endregion
最后引入一种,在邮件中加入附件发送:
#region 在邮件中增加附件
Console.WriteLine("开始发送邮件....");
//1.MailMessage
//1.创建一封邮件
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sk@ceshi.net");
msg.To.Add(new MailAddress("yzk@ceshi.net"));
msg.Subject = "======去给我带份饭。=======";
msg.SubjectEncoding = Encoding.GetEncoding("gb2312");
msg.Body = "==================在中午12:00之前把饭送过来。================";
//创建一个html格式的文档
AlternateView aview = AlternateView.CreateAlternateViewFromString("在中午
12:00
之前把饭送过来。", Encoding.UTF8, "text/html");
//为邮件中增加一张图片
LinkedResource resource = new LinkedResource(@"c:\toYou.gif");
resource.ContentId = "img001";
aview.LinkedResources.Add(resource);
msg.AlternateViews.Add(aview);
//为邮件增加一些附件
Attachment attchFile1 = new Attachment(@"c:\1.xls");
Attachment attchFile2 = new Attachment(@"c:\a.gif");
Attachment attchFile3 = new Attachment(@"c:\a.jpg");
msg.Attachments.Add(attchFile1);
msg.Attachments.Add(attchFile2);
msg.Attachments.Add(attchFile3);
//2.SmtpClient
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
//smtp.qq.com
smtp.Credentials = new NetworkCredential("sk", "123");
smtp.Send(msg);
Console.WriteLine("发送完毕!");
Console.ReadKey();
#endregion