C#发送email

 1 using System;
2 using System.Net;
3 using System.Net.Mail;
4 using System.Net.Mime;
5 namespace Fish
6 {
7 /**/
8 /// <summary>
9 /// 发送邮件[you jian]的类
10 /// </summary>
11 public class SendMail
12 {
13 private MailMessage mailMessage;
14 private SmtpClient smtpClient;
15 private string password;//发件人密码[mi ma]
16 /**/
17 /// <summary>
18 /// 处审核后类的实例
19 /// </summary>
20 /// <param name="To">收件人地址[di zhi]</param>
21 /// <param name="From">发件人地址[di zhi]</param>
22 /// <param name="Body">邮件[you jian]正文[zheng wen]</param>
23 /// <param name="Title">邮件[you jian]的主题</param>
24 /// <param name="Password">发件人密码[mi ma]</param>
25 public SendMail(string To, string From, string Body, string Title, string Password)
26 {
27 mailMessage = new MailMessage();
28 mailMessage.To.Add(To);
29 mailMessage.From = new System.Net.Mail.MailAddress(From);
30 mailMessage.Subject = Title;
31 mailMessage.Body = Body;
32 mailMessage.IsBodyHtml = true;
33 mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
34 mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
35 this.password = Password;
36 Console.WriteLine("send mail sucssesful");
37 }
38 /**/
39 /// <summary>
40 /// 添加附件
41 /// </summary>
42 public void Attachments(string Path)
43 {
44 string[] path = Path.Split(',');
45 Attachment data;
46 ContentDisposition disposition;
47 for (int i = 0; i < path.Length; i++)
48 {
49 data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化[shi li hua]附件
50 disposition = data.ContentDisposition;
51 disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期[chuang jian ri qi]
52 disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改[xiu gai]日期
53 disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取[du qu]日期
54 mailMessage.Attachments.Add(data);//添加到附件中
55 }
56 }
57 /**/
58 /// <summary>
59 /// 异步[yi bu]发送邮件[you jian]
60 /// </summary>
61 /// <param name="CompletedMethod"></param>
62 public void SendAsync(SendCompletedEventHandler CompletedMethod)
63 {
64 if (mailMessage != null)
65 {
66 smtpClient = new SmtpClient();
67 smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置[she zhi]发件人身份[shen fen]的票据
68 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
69 smtpClient.Host = "smtp." + mailMessage.From.Host;
70 smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册[zhu ce]异步[yi bu]发送邮件[you jian]完成时的事件[shi jian]
71 smtpClient.SendAsync(mailMessage, mailMessage.Body);
72 }
73 }
74 /**/
75 /// <summary>
76 /// 发送邮件[you jian]
77 /// </summary>
78 public void Send()
79 {
80 if (mailMessage != null)
81 {
82 smtpClient = new SmtpClient();
83 smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置[she zhi]发件人身份[shen fen]的票据
84 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
85 smtpClient.Host = "smtp." + mailMessage.From.Host;
86 smtpClient.Send(mailMessage);
87 }
88 }
89 }
90 }

 

你可能感兴趣的:(email)