Tag Archives: System.Net.Mail

Scripting: Send email using Powershell and System.Net.Mail API


You can send email using System.Net.Mail API & PowerShell scripting. It’s easy, faster!! Lets try…

Why Powershell?
PowerShell is the automation platform and scripting language for Windows and Windows Server that allows you to simplify the management of your systems. Unlike other text-based shells, PowerShell harnesses the power of the .NET Framework, providing rich objects and a massive set of built-in functionality for taking control of your Windows environments. For more info, https://msdn.microsoft.com/en-us/powershell/mt173057.aspx

Why Powershell & System.Net.Mail API?
I chose this combo to send email, as it’s easy, faster, make use of the powerful platform so that you can make use of it in your Windows and Windows Server. Again, System.Net.Mail API contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery. For more info, https://msdn.microsoft.com/en-us/library/system.net.mail(v=vs.110).aspx

Lets have a look at the Powershell:

$smtpServer = “ex13.contoso13.com”
$smtpFrom = “t1@contoso13.com”
$smtpTo = “t2@contoso13.com”
$messageSubject = “Hello world”
$messageBody = “Hello world from PowerShell”
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)

Here is the Powershell and the related explanation:

$smtpServer = “ex13.contoso13.com” => Specify the remote SMTP server info here
$smtpFrom = “t1@contoso13.com” => Specify the From address of the recipient
$smtpTo = “t2@contoso13.com” => Specify the To address of the recipient
$messageSubject = “Hello world” => Specify the Subject of the email
$messageBody = “Hello world from PowerShell” => Specify the MessageBody
$smtp = New-Object Net.Mail.SmtpClient($smtpServer) => Create a newobject, call the system.net.mail.api and pass the smtpserver info
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody) => use .send command to send email

In action,

Le Café Central de DeVa - Deva blogs

Le Café Central de DeVa - Deva blogs

Here is the output:

Le Café Central de DeVa - Deva blogs

Note: Related code sample can be downloaded from here.

Hope this helps.

Diagnostics: How to enable debug tracing for System.Net.Mail API?


You can enable the following trace switches in your application config file, so that you can enable tracing for The System.Net.Mail library/API. This will log the contents of the SMTP session to a file. To send traces to a log file, add the following node to the <system.diagnostics> node of the appropriate configuration file (application or machine). You can change the name of the file (trace.log) to suit your needs, like what i did below as “System.Net.trace.log”.

So the below example will log the SMTP session in a file, “System.Net.trace.log”. You can have a look it, which will be helpful for your detailed debugging/troubleshooting purpose.

<configuration>
   <system.diagnostics>     <trace autoflush=”true” />
    <sources>
      <source name=”System.Net” >
         <listeners>
           <add name=”MyTraceFile”/>
         </listeners>
       </source>
      <source name=”System.Net.Sockets”>
         <listeners>
           <add name=”MyTraceFile”/>
         </listeners>
       </source>
    </sources>
    <sharedListeners>
       <add  name=”MyTraceFile” type=”System.Diagnostics.TextWriterTraceListener” initializeData=”System.Net.trace.log”   />
     </sharedListeners>
    <switches>
       <add name=”System.Net” value=”Verbose” />
       <add name=”System.Net.Sockets” value=”Verbose” />
     </switches>
  </configuration>

Hope this helps.

Hotfix: Failure Sending mail error with System.Net.Mail (.Net Framework 4.0) while sending large attachment


Recently one of my customer ran into an issue with his application used to send email messages using System.Net.Mail.SmtpClient (SNM) API. He used Visual Studio 2010 and .NET Framework 4.0. It’s a pretty simple code,

//Sample C# code snippet using System.Net.Mail API
SmtpClient MyClient = new SmtpClient("mail_server");
MyClient.Credentials = new System.Net.NetworkCredential("User", "Password", "Domain");
MailMessage MyMsg = new MailMessage("fromuser", "touser", "Testing Attachment", "Testing");
Attachment MyAttachment = new Attachment(@"attachmentfile");
MyMsg .Attachments.Add(MyAttachment );
MyClient.Send(MyMsg );

Using the above code, whenever he adds the attachment size is larger than 3 MB and tries to send the message. It fails with the following a exception System.Net.Mail.SmtpException and error message “Failure sending mail”. Also during troubleshooting i noticed that i generates a inner exception System.IndexOutOfRangeException with the error message “Index was outside the bounds of array”. In the application he also failed to specify the TransferEncoding property of the attachment.

In this customer scenario, recommended him to try with TransferEncoding property with QuotedPrintable worked for him. We tried it like,
MyAttachment.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;

You can find the following support knowledgebase article which talks about the issue and provides resolution to overcome the issue. 

Also there is a hotfix available to overcome this issue. Please make sure that you must have .NET Framework 4.0 installed to apply this hotfix. To download this hotfix, you can visit following Microsoft website: https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=30226.

System.Net.Mail: Mail sent via Network delivery throws exception “Command not implemented/supported”


One of the customer reported that whenever he tries to send e-mail by using System.Net.Mail API they get the following error “System.Net.Mail.SmtpException error message: Command not implemented. The server response was: Command not Supported

I had a look at the application. They built a Microsoft Visual Studio 2005 based application and tried to send e-mail by using the System.Net.Mail.SmtpClient class. They use the DeliveryMethod.Network method from the Microsoft .NET Framework 2.0. The application receive the following System.Net.Mail.SmtpException error message: Command not implemented. The server response was: Command not Supported.

During the investigation, we found that this problem occurs because the remote SMTP server does not support the SMTP extended Hello (EHLO) command. The exception occurs when the remote SMTP server returns the 502 reply code and the System.Net.Mail.SmtpClient class expects the 500 reply code.

To overcome this issue, Microsoft released a hot fix downloaded from http://code.msdn.microsoft.com/KB913616/Release/ProjectReleases.aspx?ReleaseId=789. Also you can refer the detailed Knowledge base article regarding this issue and hot fix.

CDONTS, CDOSYS, System.Web.Mail & System.Net.Mail


Microsoft Windows Server 2003 does not install Collaboration Data Objects (CDO) for NTS (CDONTS). Therefore, applications that use CDONTS do not function on a Windows Server 2003-based computer.

Windows Server 2003 provides improved alternatives to CDONTS. To make applications that use CDONTS function on a Windows Server 2003-based computer, update existing CDONTS applications to use one of the following technologies:

  • CDO for Windows 2000
    CDO for Windows 2000 provides a broader functionality set than CDONTS. This functionality includes the ability to send messages through a remote Simple Mail Transfer Protocol (SMTP) server. You can use CDO for Windows 2000 (CDOSYS) in applications that are not Microsoft .NET Framework-based.

    For example, Microsoft Visual Basic 6.0 and Microsoft ASP are not based in the .NET Framework. You can also use CDOSYS in .NET Framework applications.
  • System.Web.Mail
    The System.Web.Mail namespace provides a managed wrapper for CDOSYS. The System.Web.Mail namespace is only available in .NET Framework applications.
  • System.Net.Mail
    When we talk about .Net framework, we can use the System.Net.Mail namespace. It provides way to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.

FYI: System.Net.Mail Vs System.Web.Mail

In .NET 1.1, only the System.Web.Mail was available. This implementation needs to be done in quite different way. For example, attachment could only be added from files, not from Streams. So, this can be still used under .Net Framework 1.0 and 1.1, but going forward it’s “deprecated” or “Obsolete”; so it’s recommended that to make use of System.Net.Mail.

The .NET 2.0 implementation is System.Net.Mail and is much more flexible and has a richer feature set.