4
votes

I'm rather new to SMTP and IIS settings but according to the documentation on I've read the web this should be working.

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

What I have done: In my IIS, for my site (ASP.NET), I have configured the SMTP E-mail. I have entered:

  1. A random E-mail address (it doesn't have to be an existing, right?)
  2. A SMTP Server IP (in this case an IP to an external SMTP Relay Server)
  3. A port number (25).
  4. Autentication Settings to "Not required".

My method for sending an email looks like this:

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

        message.From = new MailAddress("[email protected]");
        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:

  1. Is this correct? Is it correct to also put the Relay Server IP and Port number in the code as parameters in the new SmtpClient?
  2. I don't get an error but I don't receive an email. (I am 100% sure that the "to-email" is correct.
  3. What can be the reason for this not working? What am I missing or misunderstanding?
2

2 Answers

2
votes

Wrap your smtpClient.Send(message); in a try/catch block and log any exceptions that are thrown.

A random E-mail address (it doesn't have to be an existing, right?)

That depends on your SMTP provider and configuration.

Without more information on your SMTP provider or an error message I doubt there's anything we can do for you.

-2
votes
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);

should be set out like this