C# System.Net.Mail.MailMessage 发邮件
上篇文化在哪个可以看到使用 System.Web.Mail.MailMessage 发邮件时会提示
,提供用于构造电子邮件的属性和方法。建议使用的替代项:System.Net.Mail,The recommended alternative is System.Net.Mail.MailMessage ,因此,我们新建控制台Console项目,然后添加 System.Net引用
代码如下:
using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; //using System.IO.Pipes; using System.Net; using System.Net.Mail; using System.Reflection; using System.Text; using System.Text.RegularExpressions; namespace LongtengSupremeConsole { class Program { static void Main(string[] args) { System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(); //mm.Sender = new MailAddress("mail@aliyun.com", "linjie"); mm.From = new MailAddress("test@aliyun.com", "123");//发送方 mm.To.Add(new MailAddress("test@qq.com", "456"));//接受方 mm.CC.Add(new MailAddress("test@163.com", "123789"));//抄送方,CC就是carbon copy,副本,及抄送的意思 mm.Subject = "Hello!";//主题 mm.Body = "Hello. Here's the myphoto!";//内容 mm.IsBodyHtml = false;//是否使用html格式 mm.Priority = MailPriority.High;//优先级 Attachment a = new Attachment("myphoto.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg);//附件 mm.Attachments.Add(a); SmtpClient client = new SmtpClient();//smtp客户端 client.Host = "smtp.aliyun.com";//服务器主机 client.DeliveryMethod = SmtpDeliveryMethod.Network;//发送方式 client.Port = 25;//端口 client.Credentials = new NetworkCredential("test@aliyun.com", "***");//用户名和密码 client.Send(mm); Console.WriteLine("邮件发送完成!!"); Console.ReadKey(); } } }