通过上一篇配置本地邮件服务器,已经实现了手动发送,接收邮件的功能。
接下来,我们就试试用代码实现发邮件。
我们都知道,邮件一般包括,发件人,收件人,邮件服务器,邮件。那么我们就按照这个思路来写代码
1.发送一封简单的邮件
private static void SendSimpleMail() { //1.创建一封邮件对象 MailMessage mail=new MailMessage(); mail.Subject = "我是邮件的主题"; mail.SubjectEncoding = Encoding.UTF8; mail.Body = "我是邮件的正文"; mail.BodyEncoding = Encoding.UTF8; //发件人 mail.From=new MailAddress(lisi@163.com,"李小四"); //收件人 mail.To.Add(new MailAddress(wangwu@163.com,"王小五")); //2.创建一个发送邮件的类 SmtpClient client=new SmtpClient("127.0.0.1"); client.Credentials=new NetworkCredential("lisi@163.com","lisi"); client.Send(mail); Console.WriteLine("ok"); }
在Foxmail中如下图
查看邮件源码
2.发送一封带html的邮件
private static void SendHtmlMail() { //1.创建一封邮件对象 MailMessage mail = new MailMessage(); mail.Subject = "我是邮件的主题"; mail.SubjectEncoding = Encoding.UTF8; mail.Body = "我是邮件的正文"; mail.BodyEncoding = Encoding.UTF8; //发件人 mail.From = new MailAddress("lisi@163.com", "李四"); //收件人 mail.To.Add(new MailAddress("wangwu@163.com", "王五")); //----------------------------------------------------------- //设置该邮件的格式为html AlternateView htmlBody = AlternateView.CreateAlternateViewFromString( "你好<font colof='red' size=7>中国</font><hr/>", null, "text/html"); //将该html内容添加 到邮件中 mail.AlternateViews.Add(htmlBody); //----------------------------------------------------------- //2.创建一个发送邮件的类 SmtpClient client = new SmtpClient("127.0.0.1"); client.Credentials = new NetworkCredential("lisi@163.com", "lisi"); client.Send(mail); Console.WriteLine("ok"); }
在Foxmail中如下图
3.发送一封带显示图片的邮件
private static void SendMailWithImg() { //1.创建一封邮件对象 MailMessage mail = new MailMessage(); mail.Subject = "我是邮件的主题"; mail.SubjectEncoding = Encoding.UTF8; mail.Body = "我是邮件的正文"; mail.BodyEncoding = Encoding.UTF8; //发件人 mail.From = new MailAddress("lisi@163.com", "李四"); //收件人 mail.To.Add(new MailAddress("wangwu@163.com", "王五")); //----------------------------------------------------------- //设置邮件格式为html,将img标签设置到html中 AlternateView htmlBody = AlternateView.CreateAlternateViewFromString("<img src='cid:aa' width='100px' height='100px'/>", null, "text/html"); //创建一个链接资源, LinkedResource lr=new LinkedResource("h:/1.jpg","image/gif"); //设置此链接资源MIME内容ID lr.ContentId = "aa"; //将该链接资源添加到html中 htmlBody.LinkedResources.Add(lr); //将html内容添加到邮件中 mail.AlternateViews.Add(htmlBody); //----------------------------------------------------------- //2.创建一个发送邮件的类 SmtpClient client = new SmtpClient("127.0.0.1"); client.Credentials = new NetworkCredential("lisi@163.com", "lisi"); client.Send(mail); Console.WriteLine("ok"); }
在Foxmail中如下图
4.发送一封带附件的邮件
private static void SendMailWithAttachment() { //1.创建一封邮件对象 MailMessage mail = new MailMessage(); mail.Subject = "我是邮件的主题"; mail.SubjectEncoding = Encoding.UTF8; mail.Body = "我是邮件的正文"; mail.BodyEncoding = Encoding.UTF8; //发件人 mail.From = new MailAddress("lisi@163.com", "李四"); //收件人 mail.To.Add(new MailAddress("wangwu@163.com", "王五")); //----------------------------------------------------------- //添加附件 Attachment a1=new Attachment(@"h:/1.jpg"); Attachment a2=new Attachment(@"h:/1.rar"); Attachment a3=new Attachment(@"h:/1.docx"); mail.Attachments.Add(a1); mail.Attachments.Add(a2); mail.Attachments.Add(a3); //----------------------------------------------------------- //2.创建一个发送邮件的类 SmtpClient client = new SmtpClient("127.0.0.1"); client.Credentials = new NetworkCredential("lisi@163.com", "lisi"); client.Send(mail); Console.WriteLine("ok"); }
在Foxmail中如下图