C # Sending Email Using SMTP Email

I am new to SMTP and IIS settings, but according to the documentation read on the Internet, this should work.

What I'm trying to achieve: To send an email from the server to the users email address using the existing SMTP relay server.

What I did: In my IIS for my site (ASP.NET), I set up SMTP email. I entered:

  • A random email address (it doesn't have to be an existing one, right?)
  • IP address of the SMTP server (in this case, the IP address of the external SMTP relay server)
  • Port Number (25).
  • Authentication settings to "Not required."

My email sending method is as follows:

public static void SendEmail()
{
    var message = new MailMessage()
    {
        Subject = "Heading",
        Body = "Body",

        message.From = new MailAddress("test@test.com");
        message.To.Add("A valid email address"); //My own email address
    }
    var smtpClient = new SmtpClient("SMTP-Relay-Server-IP", 25); //Same IP as the one in SMTP E-mail configuration in IIS for the site.         
    smtpClient.Send(message);
}
}

Facts / questions:

  • ? IP- SmtpClient?
  • , . ( 100% , " " .
  • ? ?
+4
2

smtpClient.Send(message); try/catch .

( , ?)

SMTP .

SMTP- , - .

+2
MailMessage mail = new MailMessage("sendTo", "from");
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential("user", "pass");
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
mail.Body = "this my message";
smtp.Send(mail);

-2

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


All Articles