To use the WebMail helper, you need access to an SMTP server. SMTP is the "output" part of email. If you use a web host, you probably already know the name of the SMTP server. If you work in a corporate network, your IT department can give you the name. If you are working at home, you might be able to use your ordinary email provider.
In order to send an email you will need:
- The name of the SMTP server
- The port number (most often 25)
- An email user name
- An email password
In the root of your web, create a page (or edit the page ) named _AppStart.cshtml.
Put the following code inside the file:
_AppStart.cshtml
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "support@example.com";
WebMail.Password = "password";
WebMail.From = "john@example.com"
}
The code above will run each time the web site (application) starts. It feeds your WebMail Object with initial values.
Please substitute:
smtp.example.com with the name the SMTP server that will be used to send the emails.
25 with the port number the server will use to send SMTP transactions (emails).
false with true, if the server should use SSL (Secure Socket Layer) encryption.
support@example.com with the name of the SMTP email account used to send emails.
password with the password of the SMTP email account.
john@example with the email to appear in the from address.
You don't have to initiate the WebMail object in your AppStart file, but you must set these properties before you call the WebMail.Send() method
Practice Excercise Practice now