Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending mail using SmtpClient in .net

Tags:

I am unable to send the mail using smtp client. here is the code:

SmtpClient client=new SmtpClient("Host");
client.Credentials=new NetworkCredential("username", "password");
MailMessage mailMessage = new MailMessage();
mailMessage.from="[email protected]";
mailMessage.To.Add("[email protected]");
mailMessage.body="body";
mailMessage.subject="subject";
client.Send(mailMessage);

The problem is that when I use this code in ASP.NET application, I do not receive any mails. When in asp.net I change the from mail address to username given in NetworkCredential, I receive mails.

But in C# windows application, I can get emails, even if sender's email address is not valid.

like image 246
Manish Gupta Avatar asked Mar 18 '10 14:03

Manish Gupta


People also ask

What is Net mail SmtpClient?

Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient type is obsolete on some platforms and not recommended on others; for more information, see the Remarks section.


2 Answers

I figured out that setting the SmtpClient Credentials property before setting the ' UseDefaultCredentials = false ' UseDefaultCredentials = false causes the credentials to be ignored.

Fails:

SmtpClient smtp = new SmtpClient;
smtp.Credentials = new NetworkCredential("user","pass");
smtp.UseDefaultCredentials = false;

Works:

SmtpClient smtp = new SmtpClient;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("user","pass");

Go figure.

** UPDATE **

The reason the order is important here is that the setter on the UseDefaultCredentials property actually sets the Credentials to null via the decompiled code:

/// <summary>Gets or sets a <see cref="T:System.Boolean" /> value that controls whether the <see cref="P:System.Net.CredentialCache.DefaultCredentials" /> are sent with requests.</summary>
/// <returns>
/// <see langword="true" /> if the default credentials are used; otherwise <see langword="false" />. The default value is <see langword="false" />.</returns>
/// <exception cref="T:System.InvalidOperationException">You cannot change the value of this property when an e-mail is being sent.</exception>
public bool UseDefaultCredentials
{
  get
  {
    return this.transport.Credentials is SystemNetworkCredential;
  }
  set
  {
    if (this.InCall)
      throw new InvalidOperationException(SR.GetString("SmtpInvalidOperationDuringSend"));
    this.transport.Credentials = value ? (ICredentialsByHost) CredentialCache.DefaultNetworkCredentials : (ICredentialsByHost) null;
  }
}
like image 94
user3524983 Avatar answered Oct 06 '22 16:10

user3524983


The short answer : Do not use .net the internal SMTP client class - use MailKit.

The long answer :

  1. It is marked as obsolete in MS docs

[System.Obsolete("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")] public class SmtpClient : IDisposable

  1. Use the oss lib MailKit.
  2. Elaboration : Because you will face its deficiencies sooner rather than later. Read here or read around the internet.
  3. Here is an example for sending plain messages and multipart messages.

I intentionally did not copy-paste the examples from the docs here because the documentation may change over time and it is better to read .

like image 28
Ognyan Dimitrov Avatar answered Oct 06 '22 18:10

Ognyan Dimitrov