首先要添加引用语句:using System.Net;using System.Net.Mail;
实现代码如下:
string mailtoaddress = "2727***@qq.com";
string mailfrom = "scg***@gmail.com";//会被后面验证的邮箱地址覆盖
string displayName = "yuzhongheng";
string smtp = "smtp.gmail.com"; //指定发送服务器的地址
string fileName = "Agreenment.txt";
string strbody = "aaaaaaaaaa"; //附件内容
MailAddress to = new MailAddress(mailtoaddress);
MailAddress from = new MailAddress(mailfrom, displayName);
MailMessage message = new MailMessage(from, to);
//message.CC.Add(ccmail);//抄送人
message.Subject = "主题 " + DateTime.Now.ToString("MM-dd-yyyy");
message.Body = "内容";
message.IsBodyHtml = true; //邮件显示的格式,以HTML格式显示
message.BodyEncoding = System.Text.Encoding.UTF8;
Stream s = new MemoryStream(System.Text.Encoding.Default.GetBytes(strbody));
Attachment a = new Attachment(s, fileName); //添加附件
message.Attachments.Add(a);
SmtpClient smtpClient = new SmtpClient(smtp, 587);
smtpClient.EnableSsl = true;//非常重要,不设置成true的话,可能无法发送成功
smtpClient.Credentials = new NetworkCredential("y******@gmail.com", "******"); //您的邮箱账号和密码,现在很多邮箱都要验证,没有的话,不能发送成功
smtpClient.Send(message);
原创的免费软件 - LoopcVPN,DnsAcc,DNS加速器,百度相册助手