A *really* common and popular thing to do with web pages it to send email. That email might be a feedback form, or an order confirmation, or a variety of other functions. Below is the complete contents of a page I named SendEmail.cshtml. I’ll throw it out there right at the start in case you want to copy/paste it and start playing around. After the code I’ll break things down a bit to explain.
**NOTE: You need a working SMTP Server to send the email. In my code sample I used GMail’s SMTP service, which requires SMTP-AUTH of an existing username/password. If you have a GMail account and want to use this same SMTP server, just be sure to put your username (email address) and password in the appropriate places. If you want to use a different SMTP server, then you’ll need the name of that server and security requirements (SMTP-AUTH, IP-AUTH, etc.) and adjust your code as needed.
Oh, let me also say up-front here – DO NOT just use this code sample and put it out on a public web page. It won’t take a spammer very long to find the page (I don’t know how they do – but they do!) and start abusing it. As soon as that happens you are going to be on the hook for a lot of trouble since your specific identity is known via the authentication.
Okay, here’s the code:
@{ var errorMessage = ""; if (IsPost) { var toEmail = Request["toEmail"]; var fromEmail = Request["fromEmail"]; var subject = Request["subject"]; var body = Request["body"]; try { WebMail.SmtpServer = "smtp.gmail.com"; WebMail.SmtpPort = 25; WebMail.EnableSsl = true; WebMail.UserName = "*YOU*@gmail.com"; WebMail.Password = "*YourGMailPassword*"; WebMail.From = fromEmail; WebMail.Send(to: toEmail, subject: subject, body: body ); } catch (Exception ex ) { errorMessage = ex.Message; } } } @if (errorMessage !="") { <div>Error: @errorMessage } <form method="post"> From Address: <input type="text" name="fromEmail" /> To Address: <input type="text" name="toEmail" /> Subject: <input type="text" name="subject" /> Body: <textarea id="body" cols="40" rows="10"></textarea> <input id="Submit1" type="submit" value="submit" /> </form>
Okay, let’s break this down a bit. In line two I’m defining a variable that can be used to capture any error message that’s returned. I actually added that after I started working on this sample because I was throwing a number of different errors every time I fixed one. :)
Starting at line 4 I check to see if this is a postback to the page or a first load. If it’s a first load then I ignore the code and show the form. If it’s a postback then I define some variables and load them with values posted from the form.
Then on line 11 we start the try block that wraps the email-sending code. I find that it’s pretty easy to have errors working with email code (a previous post I wrote years ago had over 100 comments from people with various challenges) so wrapping the code in a Try-Catch block and showing the error in a user-friendly format is helpful.
Starting with line 12 we define everything we need to send the email. First I define the SMTP server. This can be “localhost” if you have an SMTP service running on your machine (like Smtp4dev or even the Microsoft SMTP service (if you are running Windows Server 2008 R2 or earlier – Windows 8.1 and Server 2012 no longer support the Microsoft SMTP service). In my sample code I’m using my GMail account so using GMail’s SMTP server. Then I clarify the port number (some ISPs use a non-standard port). GMail’s SMTP server requires an encrypted connection so I specifically set EnableSsl to true (this is ISP dependent – check with whoever manages your SMTP server). The new two lines set the username and password if SMTP-AUTH is requires by your SMTP server (GMail does require it to track spammers). Lastly I set the From address and then execute the Send method.
If there is an error it will be noticed by the Catch statement on line 23 and assigned to the errorMessage variable for display on line 29.
That’s about it. The rest is a really basic HTML form so I won’t go through that specifically.