ASP MVC: sending email

I am new to the .NET platform. And I'm currently learning ASP.NET MVC.

I want to send an email from my program, and I have the following code:

public void sendVerrificationEmail() { //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress(""); mail.To.Add(""); //set the content mail.Subject = "This is an email"; mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>"; mail.IsBodyHtml = true; //send the message SmtpClient smtp = new SmtpClient("127.0.0.1"); smtp.Send(mail); } 

Now, when I execute this code, I get the following exception:

 System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25 

Now I am very new to IIS Manager and more. therefore, maybe something is wrong.

Do I need to install a virtual SMTP server or something like that? I currently have the following settings:

http://img153.imageshack.us/img153/695/capture2p.png http://img153.imageshack.us/img153/695/capture2p.png

I searched for a few hours, but I can not find a working solution.

Help will be appreciated!

+4
source share
3 answers

when you call

 SmtpClient smtp = new SmtpClient("127.0.0.1"); 

There must be an SMTP server on the local host. If not, you can use MailServer on your network.

for testing purposes you can use

 <system.net> <mailSettings> <smtp from=" Test@test.com " deliveryMethod="SpecifiedPickupDirectory"> <network host="127.0.0.1" port="25" userName="userID" password="*****" defaultCredentials="true" /> <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/> </smtp> </mailSettings> </system.net> 

This will save your letters in the C: \ temp \ mail folder without sending.

+6
source

Well, when you try to send an email to an SMTP service running on 127.0.0.1, it really should be MANDATORY one to start there to receive the email or;)?

Nothing about the IIS manager - just plain common sense.

Basically:

  • DO NOT install smtp relay service there. Just don't do it.

  • Configure the smtp service in the IIS configuration - this way it enters your web.config and is not hardcoded to as many places as possible in your application.

0
source

Usually I send mail through the GMail SMTP service with localhost. I have a different configuration when the project is uploaded to my web host.

Here is an example: http://www.shabdar.org/send-email-using-gmail-account-asp-net-csharp.html

0
source

Source: https://habr.com/ru/post/1308464/


All Articles