AnanasPie
AnanasPie

Reputation: 25

ASP.Net MVC sending mail server side

I am developing a .net MVC application. I have read a lot about sending mail in the server side, but i am confused. If someone can tell me please how this works?

Upvotes: 0

Views: 1222

Answers (1)

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22094

This is just too broad. In short:

You upload the mail to SMTP server via .NET API. The SMTP server then redirect email to destination server (directly or through one or more servers). The process of delivery is out off your control - you can get error message from first SMTP server (if any), but there is no way how to determine if the email was delivered - its up to implementation of every single server in chain.

If you want to use sending messages you have to setup your SMTP server or use public one (such as gmail.com). You can in theory use in memory SMTP server, but those are usually rejected by other SMTP servers in chain (thus lowering odds of delivery).

Reference: http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol

C# example of sending email via gmail:

web.config (or app.config):

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network" from="[email protected]">
      <network userName="gmail_account_name" defaultCredentials="false" 
        password="gmail_account_password" port="587" 
        host="smtp.gmail.com" enableSsl="true"/>
    </smtp>
  </mailSettings>
</system.net>

Code:

var client = new SmtpClient();
var email = new MailMessage("[email protected]", "[email protected]") {
  IsBodyHtml = false,
  Subject = "this is subject",
  Body = "this is body",
  BodyEncoding = Encoding.UTF8
};

try {
  client.Send(email);
} catch (SmtpException exception) {
  // log exception
}

Upvotes: 1

Related Questions