Home >Backend Development >C#.Net Tutorial >C# Supplementary SmtpClient Class
SmtpClient Class
Allows applications to send email using Simple Mail Transfer Protocol (SMTP).
Namespace: system.net.mail
Properties
ClientCertificates: Specifies which certificate should be used to establish a Secure Sockets Layer (SSL) connection
Credentials: Gets or sets the credentials used to authenticate the sender
DeliveryFormat: Gets or sets the delivery format used by SmtpClient to send emails
DeliveryMethod: Specifies how to send emails Mail will handle the message
EnableSsl: Specifies whether SmtpClient uses Secure Socket Layer (SSL) encrypted connections
Host: Gets or sets the IP of the host used to record one or more SMTP transactions Address
PickupDirectoryLocation: Gets or sets the folder in which the application saves mail for processing by the local SMTP server
Port: Gets or sets the port used for SMTP transactions
ServicePoint: Gets the network connection used to transmit email
TargetName: Gets or sets the service provider name (SPN) when using extended protection for authentication
Timeout: Gets or sets A value that specifies the timeout for Send calls
UseDefaultCredentials: Gets or sets a Boolean value that controls whether DefaultCredentials are sent with the request
Method
Dispose()
Send a QUIT message to the SMTP server, terminate the TCP connection normally, and release all resources of the SmtpClient class used by the current instance
Dispose(Boolean)
Send a QUIT message When sent to the SMTP server and the TCP connection is terminated normally, all resources of the SmtpClient class used by the current instance are released, and managed resources can be released as needed
Equals(Object)
Determine the specified object Whether it is equal to the current object
Finalize()
Allows an object to try to release resources and perform other cleanup operations before the garbage collection mechanism will recycle it
GetHashCode()
As the default hash function
GetType()
Get the Type of the current instance
MemberwiseClone()
Create the current Object Shallow copy
OnSendCompleted(AsyncCompletedEventArgs)
Raises the SendComplete event
Send(MailMessage)
Sends the specified message to the SMTP server for delivery
Send(String, String, String, String)
Sends the specified email to the SMTP server for delivery. The email sender, recipient, subject, and message body are sent to the SMTP server for delivery using the specified String object
SendAsync(MailMessage, Object)
. This method does not block the calling thread and allows the caller to pass the object to the method that is called when the operation completes
SendAsync(String, String, String, String, Object)
Will send a The email is sent to the SMTP server for delivery. The email sender, recipients, subject, and message body are specified using String objects. This method does not block the calling thread and allows the caller to pass the object to the method that is called when the operation completes.
SendAsyncCancel()
Cancel an asynchronous operation to send an email
SendMailAsync(MailMessage)
Sends the specified message to the SMTP server for an asynchronous operation transmitted in the form.
SendMailAsync(String, String, String, String)
Sends the specified message to the SMTP server for delivery as an asynchronous operation. . The email sender, recipients, subject, and message body are specified using String objects.
ToString()
Returns a string representing the current object. (Inherited from Object.)
Event
SendCompleted
Occurs when an asynchronous email send operation is completed
Remarks
Table below The class shown in is used to build emails that can be sent using the SmtpClient.
Attachment class
Represents a file attachment, this class allows you to attach a file, stream, or text to an email
MailAddress class
Represents a sending message The email address of the person and recipient
MailMessage class
Represents an email
To construct and send an email using SmtpClient, you must specify the following information :
The SMTP host server used to send emails.
For authentication, if SMTP server requires credentials.
Sender email address.
Email address or recipient's address.
Message content.
To include an attachment in an email, first create the attachment using the Attachment class, and then add it to the message via the MailMessage.Attachments property. Depending on the email reader used and the file type of the attachment, some recipients may not be able to read the attachment. For clients that cannot maintain attachments displayed in their original format, you can specify an alternate view by specifying the MailMessage.AlternateViews property.
You can use the application or computer configuration file to specify default host, port, and credential values for all SmtpClient objects.
To send emails and chunks while waiting for the email to be transmitted to the SMTP server, use a synchronous Send method. To allow the program's main thread to continue executing while transmitting emails, use one of the asynchronous SendAsync methods. The SendCompleted event is raised when the SendAsync operation completes. To receive this event, you must add a SendCompletedEventHandler delegate to SendCompleted. The callback method that the SendCompletedEventHandler delegate must reference to handle the notification's SendCompleted event. To cancel asynchronous email transmission, use the SendAsyncCancel method.
Main code for the email sending interface:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;using System.Windows.Forms; using System.IO;using System.Net;using System.Net.Mail; namespace SendEmail{ public partial class Form3 : Form{ string severaddress;string mailuser;string userpwd;public Form3(){ InitializeComponent(); } private void button1_Click(object sender, EventArgs e){ Form6 form = new Form6(); form.SendParaHandler +=new Form6.SendPara(reload); //事件的挂接form.Show(); } public void reload(){ StreamReader read = new StreamReader(@"fajianren.asdf"); severaddress = read.ReadLine(); mailuser = read.ReadLine(); userpwd = read.ReadLine(); read.Close();} private void Form3_Load(object sender, EventArgs e){ reload();} public bool sendmail(string mailfrom,string mailto,string mailsubject,string mailbody){ MailAddress from = new MailAddress(mailfrom); MailMessage message = new MailMessage(); try{message.From = from; message.To.Add(mailto); message.Subject = mailsubject; message.Body = mailbody; message.Priority = MailPriority.Normal; SmtpClient smtp = new SmtpClient(); smtp.Host = severaddress; smtp.UseDefaultCredentials = false; smtp.EnableSsl = true; smtp.Credentials = new NetworkCredential(mailuser,userpwd); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(message); } catch(Exception e){ return false; } return true; } private void button2_Click(object sender, EventArgs e){ string mailfrom = mailuser; string mailto = textBox1.Text; string mailsubject = textBox2.Text; string mailbody = textBox3.Text; if (sendmail(mailfrom, mailto, mailsubject, mailbody)){ MessageBox.Show("邮件发送成功"); } else{ MessageBox.Show("邮件发送失败"); } } } }
Main code for setting the sender information interface:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace SendEmail { public partial class Form 6 : Form {public Form6(){InitializeComponent(); } private void button1_Click(object sender, EventArgs e){ Write(); } //加载信息 private void Form6_Load(object sender, EventArgs e){ StreamReader read = new StreamReader(@"fajianren.asdf"); textBox1.Text = read.ReadLine(); textBox2.Text = read.ReadLine(); textBox3.Text = read.ReadLine(); read.Close(); } //写入信息 public void Write(){ StreamWriter write = new StreamWriter(@"fajianren.asdf"); write.WriteLine(textBox1.Text); write.WriteLine(textBox2.Text); write.WriteLine(textBox3.Text); write.Close(); } public delegate void SendPara(); //定义委托 public event SendPara SendParaHandler; //定义事件 private void button2_Click(object sender, EventArgs e){ SendParaHandler.Invoke(); Write(); this.Close(); } } }
The above is the content of the SmtpClient class in C#. For more related information, please Follow the PHP Chinese website (www.php.cn)!