System.Net.Mail
Use Simple Mail Transfer Protocol SMTP to send mail asynchronously
To implement SMTP to send mail, you need to understand these classes
SmtpClient: use the profile settings to initialize a new instance of the SmtpClient class.
It contains the following properties:
Host: set the host name or host IP used for SMTP service;
Port: set the port used for SMTP service (generally set to 25);
Credentials: authentication;
Send: send mail directly;
SendAsync: send messages asynchronously (calling threads are not blocked).
MailMessage: indicates an email.
It contains the following properties:
Attachment: indicates a file attachment;
CC: CC;
Subject: subject;
From: from
Priority: priority;
Body: body;
BodyEncoding:Content-type.
In addition, the SmtpClient class does not have a Finalize method, so the application must call Dispose To explicitly release resources.
1 static bool mailsend = true; 2 public async Task Send(object sender, EventArgs e) 3 { 4 5 SmtpClient smtp = new SmtpClient();//item base SMPTClient object 6 smtp.EnableSsl = false;//Not enabled SSL encryption 7 smtp.Host = "00.000.00.000";//Server here IP 8 smtp.Port = 25;//Port fixed to 25 9 //smtp.Credentials = new NetworkCredential("user@.com","password");//Authenticate users 10 MailMessage msg = new MailMessage();//item base Message object 11 msg.Priority = MailPriority.High;//Message priority 12 msg.To.Add("user@foxmail.com");//Addressee 13 // msg.CC.Add("user@163.com");//CC 14 msg.Bcc.Add("user@qq.com");//bcc 15 string fileName = ""; 16 var na = Request.Files.AllKeys;//Front end HTTP Requested documents 17 foreach (var item in na) 18 { 19 HttpPostedFileBase file = Request.Files[item] as HttpPostedFileBase; 20 fileName = Path.GetFullPath(file.FileName); 21 } 22 Attachment attach = new Attachment(fileName);//Pay file path to Attachment Instanced objects for 23 ContentDisposition dispo = attach.ContentDisposition;//Get information and read and write attachments 24 dispo.CreationDate = System.IO.File.GetCreationTime(fileName); 25 dispo.ModificationDate = System.IO.File.GetLastWriteTime(fileName); 26 dispo.ReadDate = System.IO.File.GetLastAccessTime(fileName); 27 msg.Attachments.Add(attach);//Add attachment to message 28 msg.From = new MailAddress("SpadesQ@sea.com", "It's you.");//Sender alias 29 msg.Subject = "I learned. How about you?"; 30 msg.SubjectEncoding = System.Text.Encoding.UTF8; 31 msg.Body = "Wait for the next one"; 32 msg.BodyEncoding = System.Text.Encoding.UTF8; 33 smtp.SendCompleted += new SendCompletedEventHandler(client_SendCompleted); 34 string userState = "Task one"; 35 smtp.SendAsync(msg, userState);//Send asynchronously, otherwise the thread will block 36 if (mailsend==false) //Modifiable on callback mailsend Value to cancel 37 { 38 smtp.SendAsyncCancel(); 39 } 40 Response.Write("Already issued"); 41 } 42 43 void client_SendCompleted(object sender, AsyncCompletedEventArgs e) 44 { 45 string token =(string)e.UserState; 46 if (e.Cancelled) 47 { 48 Response.Write(string.Format("{0} Send canceled.",token)); 49 } 50 if (e.Error !=null) 51 { 52 Response.Write(string.Format("[{0}] {1}", token, e.Error.ToString())); 53 } 54 else 55 { 56 Response.Write("Message Send."); 57 } 58 mailsend = true; 59 }
Thank you for watching. Your support is my greatest!