开发者

Send mail using localhost SMTP

I am trying to setup SMTP server on IIS for sending mails. The SMTP server 开发者_JAVA技巧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.


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;


I think in localhost you can use :

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true;
smtpClient.Send(mailMessage);


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.


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

Send mail using localhost SMTP


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;


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) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜