Skip to content
January 9, 2008 / dranaxum

Sending e-mails with C# .NET 2.0

I ran over this problem a couple of days ago when I was writing a plug-in for one of my software projects. The Internet is full of examples of sending e-mail algorithms. The problem is none of them worked for me. After studying this problem I came up with a viable solution. In the following rows I will explain how it works (I’m proud that my solution was marked as an answer to this problem in the MSDN forum):

First step: Setting-up your SMTP (Simple Mail Transfer Protocol)

I tried to use NetworkCredentials Class but it seemed that G-mail nor Yahoo SMTP worked. It gives the Failure sending mail exception. A drastic solution was to setup my own SMTP. It came up to be really easy in-fact. If you are a .NET developer you must have installed IIS (internet information services), so you can setup your SMTP Virtual Server from there.

Control Panel->Administrative Tools->Internet Information Services , then you select the SMTP Virtual Server Properties and grant permission to localhost (127.0.0.1) (Access->Relay).

Second Step: Writing the basic C# Code

private void SendEmail(string to, string from, string sub, string body)
{
SmtpClient n = new SmtpClient();
n.Host = “localhost”;
n.Send(from, to, sub, body);
}

You can also use the MailMessage Class like so:

private void SendEmail(string to, string from, string sub, string body)
{
MailMessage msg = new MailMessage(from, to, sub, body);
SmtpClient n = new SmtpClient();
n.Host = “localhost”;
n.Send(msg);
}

Be sure that the recipient checks his spam messages because many of the e-mails using this algorithm go there. Also don’t forget to include System.Net.Mail namespace (using System.Net.Mail;).

Extra:

In .NET 1.1 you can use SmtpMail class (.NET1.1 does not support SmtpClient class) and be sure to include System.WEB.Mail .

7 Comments

Leave a Comment
  1. JohnJones / Jan 12 2008 11:38 pm

    your just going to get black listed really really fast !!

    (unless you use this on a real smtp gateway or you setup your relay preferances in ISS)

    would it not be better to auth ?

    you can see this here I think
    http://support.microsoft.com/kb/555287

    regards

    John Jones
    smtp testing http://www.johnjones.me.uk

  2. dranaxum / Jan 13 2008 7:20 pm

    I tested the code found on support.microsoft.com, and others like that one. It just doesn’t work if i want to send an e-mail from a yahoo or gmail account. I don’t know why but it gives, like I said, the Failure sending mail exception!🙂
    What I do know is that you must pay to have access to yahoo smtp.

    I don’t know any solution which doesn’t include making a smtp server. If you checkout the thread on the MSDN forums you will see that many people face the same issue like i do.

  3. Agkelos / Mar 21 2008 7:52 am

    dranaxum, there are two reasons why you get that error on gmail. One is that gmail requires authentication for sending e-mail messages. Another one is that gmail requires email clients to use TLS.

    About yahoo, you’re right. They require authentication.

    Another reason for that error, if you tried some other SMTP servers, can be your ISP. In Romania RDS has blocked access on ports 25 and 26 as a method to fight spam. If you are trying to connect an email message you’ll get an error after a few seconds beacause you won’t be able to connect to the SMTP server. One solution is to use the SMTP offered by RDS, smtp.rdslink.ro

  4. dranaxum / Mar 21 2008 4:16 pm

    Thanks very much for this feedback!

  5. Learned / Jun 19 2008 7:42 am

    Somehow i missed the point. Probably lost in translation🙂 Anyway … nice blog to visit.

    cheers, Learned.

  6. fernandojhoel / Sep 16 2008 8:15 pm

    This has been very very useful to me, THANKS a lot!

  7. nightmaar / Jun 3 2009 8:09 pm

    Salut, am si eu o problema mare, ma tot chinui sa trimit mail printr-un password recovery control dintr-un website asp.net, visual studio 2008 – C#. Sunt in spatele unui proxy(complex studentesc), am incercat mai multe, am tot cautat pe net dar vad ca nu merge nimic.Ai vreo idee, se poate oare?
    proxy-ul e 10.9.1.1, portul 3128, am pus asta in web.config la system.net, default proxy, la fel autodetect si tot degeaba . Am mail settings definite asa

    Am incercat si cu 587 la port, nu vrea, foloseam o functie luata de aici http://www.aspcode.net/Send-mail-from-ASPNET-using-your-gmail-account.aspx .

    As fi recunoscator daca m-ai putea ajuta…Merci anticipat.

    condacsergiu@yahoo.com – mailul meu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: