26

I am trying to setup SMTP server on IIS for sending mails. The SMTP server is intended to be used by the ASP.NET code in C#.

I was previously using gmail smtp wherein i provided the smtp.gmail.com as host with secure port and my gmail uid/pwd. That worked fine. Here is the code used to do that.

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential(uname,pwd);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);

Now i am planning to use the localhost SMTP server on IIS, what values should i be giving for the parameters UseDefaultCredentials and Credentials. I will be assigning false to EnableSsl as it's over port 25.

Also, what could be the most simple SMTP virtual server configuration.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
pencilslate
  • 12,958
  • 18
  • 58
  • 73

6 Answers6

30

When you are using the local IIS SMTP service, set the DeliveryMethod to PickupDirectoryFromIis. For example:

  smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

This totally bypasses the network layer, and writes the messages directly to disk. Its much faster than going through the chatty SMTP protocol.

When you using the above code, it means you can get rid of this part of your code:

smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential(uname,pwd);
smtpClient.EnableSsl = true;
dave wanta
  • 7,164
  • 3
  • 29
  • 29
  • 4
    This is the correct answer for using the local IIS pickup directory. Since it just writes a file, it won't use any credentials at all and IIS will just send it for you. – Jeff Tucker Oct 13 '09 at 21:23
21

I think in localhost you can use :

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true;
smtpClient.Send(mailMessage);
Natim
  • 17,274
  • 23
  • 92
  • 150
3

It depends on how you configure the smtp server. You might not need to use any credentials at all, and just configure the server to only accept local connections.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • @Joel: My SMTP server setup: Authentication: Anonymous access IP address: All Unassigned Outbound security: Anonymous access Curious, if this is a correct configuration for sending mails without credentials. – pencilslate Oct 13 '09 at 03:07
  • That's a question for serverfault. – Joel Coehoorn Oct 13 '09 at 03:52
2

Have you tried enabling relay?

Find IIS6 manager (I have found that searching for IIS may return 2 results) go to the SMTP server properties then 'Access' then press the relay button.

Then you can either select all or only allow certain ip's like 127.0.0.1

SMTP Relay

Lee Englestone
  • 4,545
  • 13
  • 51
  • 85
  • I am using CDOSYS and from server where this virtual server is setup emails are sending smoothly, but when I try my utility from workstation get 'Transport fail to connect to server' message. any idea? what I need to change in my code so workstation will also able to use this virtual SMTP server and can send emails. – Almas Mahfooz Sep 23 '16 at 10:11
2

If you want to test emails in localhost, just download install the papercut tool https://papercut.codeplex.com/

and change hostname to localhost as below. Papercut captures all the emails sending using localhost.

  smtpClient.UseDefaultCredentials = false;
    smtpClient.Host = "localhost";
    smtpClient.Port = 587;
    smtpClient.Credentials = new NetworkCredential(uname,pwd);
    smtpClient.EnableSsl = true;
mutanic
  • 2,448
  • 1
  • 15
  • 17
DinP
  • 59
  • 7
  • also smtp4dev is a local smtp server for developers. – Birey Nov 28 '17 at 18:28
  • also TestMailServerTool: original (download seems to be broken): https://toolheap.com/test-mail-server-tool/ backup: https://github.com/Serjster/TestMailServerTool – Serj Sagan Jul 11 '19 at 04:09
  • I already had Host and Credentials but for me (using Sendgrid) the fix was adding EnableSsl and the 587 port. – Christopher Dec 17 '21 at 11:40
1

Tx Natim, what you say worked for me. Have our intranet app using integrated auth with our exchange 2007 server now:

Dim msg As New MailMessage()
Dim smtp As SmtpClient

msg.From = New MailAddress(strFrom)
msg.To.Add(strTo)
msg.Subject = strSubject
msg.Body = strBody

smtp = New SmtpClient("ServerName")
smtp.UseDefaultCredentials = True
smtp.Send(msg) 
mahalie
  • 2,647
  • 20
  • 21