SmtpClient Activity Logging
Activity logging is used for debugging, as well as for collecting and analyzing working information about the SMTP client.
Enable Activity Logging using appsettings.json File
NOTE: This option is preferred for .NET Core applications.
Logging in SmtpClient can be enabled with the following steps and code samples:
-
Add an appsettings.json configuration file to a C# project, if it has not been added before.
-
Make sure that the project file contains the following lines in the ItemGroup section.
<Content Include="appsettings.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content>
-
Then, add the following content to the appsettings.json file.
{ "SmtpDiagnosticLog": "smtp.log", "SmtpDiagnosticLog_UseDate": true }
The two properties mentioned above are:
-
SmtpDiagnosticLog - specifies the relative or absolute path to the log file.
-
SmtpDiagnosticLog_UseDate - specifies whether to add a string representation of the current date to the log file name.
Enable Activity Logging in Programm Code
You can also enable logging immediately in the code.
NOTE: even if you have already enabled logging by using configuration files, this option will be applied.
Logging in SmtpClient can be enabled with the following steps and code samples:
- Create an SmtpClient.
- Set the path to the log file using the LogFileName property.
- Set the UseDateInLogFileName property if it is necessary.
using (var client = new SmtpClient("your smtp server"))
{
// Set username, password, port, and security options
client.Username = "your username";
client.Password = "your password";
client.Port = 465;
client.SecurityOptions = SecurityOptions.SSLImplicit;
// Set the path to the log file using the LogFileName property.
client.LogFileName = @"C:\Aspose.Email.Smtp.log";
// Set the UseDateInLogFileName property if it is necessary.
client.UseDateInLogFileName = false;
var eml = new MailMessage("from address", "to address", "this is a test subject", "this is a test body");
client.Send(eml);
}
Enable Activity Logging using App.config File
SMTP client activity can be logged by modifying the configSections in the config file. Diagnostics logging can be performed with the following steps:
- Add a sectionGroup called “applicationSettings”.
- Add a section called “Aspose.Email.Properties.Settings”.
- Include the setting with the name SmtpDiagonosticLog where the file name is defined in the applicationSettings/Aspose.Email.Properties.Settings
Here is a sample form based application which uses SmtpClient to send an email. This whole activity is logged by modifying the App.config file. Create a form application with a single button on it. Add the following code for button’s click:
- Add reference to Aspose.Email.
- Add the App.Config file and modify it in such a way that file contents are as follows
- For C# .NET use the following option
- For VB .NET use the following option
- Run the code and then observe the debug folder. The following file will be generated.