Home Backend Development C#.Net Tutorial C# Supplementary SmtpClient Class

C# Supplementary SmtpClient Class

Feb 06, 2017 pm 05:23 PM

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("邮件发送失败");
}
}
}
}
Copy after login

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();
}
}
}
Copy after login

The above is the content of the SmtpClient class in C#. For more related information, please Follow the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

See all articles