Foreword
Data in this chapter record: VS2019 WinFrm desktop applications for mail sent via SMTP way. As a key step in Delphi turn C #, followed by the progressive realization of Delphi to commonly used functions.
Ready to work
Build WinFrm front interface
Add the necessary controls, this is mainly applied to GroupBox, Label, TextBox and the Button, as FIG.
Outgoing mail account configuration
Prepare a mailbox, open the SMTP service, and turn on the client authorization code ( note here ).
Core code class configuration SendEMail
code show as below:
. 1 the using the System; 2 the using the System.Collections.Generic; . 3 the using the System.Linq; . 4 the using the System.Net.Mail; . 5 the using the System.Text; . 6 the using System.Threading.Tasks; . 7 . 8 namespace the SendMail . 9 { 10 /// <Summary> . 11 /// the SMTP send mail 12 /// particular attention: this password authorization code for the client, instead of sending mail password 13 is /// </ Summary> 14 class the SendEMail 15 { 16 /// <Summary> . 17 /// E-mail 18 is /// </ Summary> . 19 /// <param name = "userEmailAddress"> from address </ param> 20 is /// <param name = "the userName"> fat member name (may be null) </ param> 21 is /// <param name = "password"> password </ param> 22 is /// <param name = "Host"> mail server address </ param> 23 is / // <param name = "Port"> </ param> 24 /// <param name = "sendToList"> (multiple email addresses must be comma character ( ",") separated) recipient </ param> 25 /// <param name="sendCCList">(You must use a comma between multiple e-mail addresses character ( ",") separated) Cc </ param> 26 /// <param name = "Subject"> theme </ param> 27 /// <param name = "body"> SUMMARY </ param> 28 /// <param name = "AttachmentsPath"> attachments path </ param> 29 /// <param name = "errorMessage"> error </ param> 30 public static BOOL SendMessage ( String userEmailAddress, String userName, String password, String Host, int Port, 31 string[] sendToList, String [] sendCCList, String Subject, String body, String [] AttachmentsPath, OUT String errorMessage) 32 { 33 is errorMessage = String .Empty; 34 is the SmtpClient Client = new new the SmtpClient (); 35 client.UseDefaultCredentials = to true ; 36 client.Credentials = new new System.Net.NetworkCredential (userEmailAddress, password); // username, client authorization code ******* special Note: the need here is the client authorization code is not mail password ****** * 37 = SmtpDeliveryMethod.Network client.DeliveryMethod; // specified Email embodiment 38 is client.Host = Host; // mail server 39 client.Port = Port; // port number of the non-SSL embodiment, the default port number is: 25 40 the MailMessage MSG = new new the MailMessage (); 41 is // add the sender 42 is the foreach ( String Send in sendToList) 43 is { 44 is msg.To.Add (Send); 45 } 46 is // plus CC 47 the foreach (String CC in sendCCList) 48 { 49 msg.CC.Add (CC); 50 } 51 is // add an attachment at an attachment case 52 is IF ! (AttachmentsPath = null && attachmentsPath.Length> 0 ) 53 is { 54 is the foreach ( String path in AttachmentsPath) 55 { 56 is var AttachFile = new new Attachment (path); 57 is msg.Attachments.Add (AttachFile); 58 } 59 } 60 msg.From = new new the MailAddress (userEmailAddress, the userName); // sender address 61 is msg.Subject = Subject; // message header 62 is msg.Body = body; // message content 63 is msg.BodyEncoding = the System .Text.Encoding.UTF8; // message content encoder 64 msg.IsBodyHtml = to true ; // whether HTML mail 65 msg.Priority = MailPriority.High; // message priority 66 the try 67 { 68 client.Send(msg); 69 return true; 70 } 71 catch (System.Net.Mail.SmtpException ex) 72 { 73 errorMessage = ex.Message; 74 return false; 75 } 76 } 77 } 78 }