Set Up SMTP Client Activity Logging in .NET Core
Activity logging is used for debugging, as well as for collecting and analyzing working information about the SMTP client.
Enable Activity Logging
Use appsettings.json File to Enable Activity Logging
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);
}
Use App.config File to Enable Activity Logging
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:
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Build message | |
MailMessage message = new MailMessage(); | |
// Set email address for From and TO | |
message.From = "userFrom@gmail.com"; | |
message.To = "userTo@gmail.com"; | |
// Set Subject and Body | |
message.Subject = "Appointment Request"; | |
message.Body = "Test Body"; | |
// Initialize SmtpClient and Set valid user name and password, Port and SecurityOptions | |
SmtpClient client = new SmtpClient(); | |
client.Host = "smtp.gmail.com"; | |
client.Username = "userFrom"; | |
client.Password = "***********"; | |
client.Port = 587; | |
client.SecurityOptions = SecurityOptions.SSLExplicit; | |
try | |
{ | |
client.Send(message); | |
} | |
catch(Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} |
- Add reference to Aspose.Email.
![]() |
---|
- Add the App.Config file and modify it in such a way that file contents are as follows
For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
<configuration> | |
<configSections> | |
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > | |
<section name="Aspose.Email.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> | |
</sectionGroup> | |
</configSections> | |
<applicationSettings> | |
<Aspose.Email.Properties.Settings> | |
<setting name="SmtpDiagnosticLog" serializeAs="String"> | |
<value>Aspose.Email.SMTP.log</value> | |
</setting> | |
</Aspose.Email.Properties.Settings> | |
</applicationSettings> | |
</configuration> |
- 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.
![]() |
---|