What is up fellow programmers!? I’m back by popular demand and, boy, do I have a lot to share. I know its been a while and you might have some abandonment issues but I’m going to make it up to you by blowing your mind with some great blog posts in the future starting…right…now.
I’m going to ease back into the blogosphere with something relatively simple: Sending an Email Notification via the C# Code Behind of an ASP.NET Web Page. If that sounds like a long title, it’s because it is.
Lets get started by getting the easy stuff out of the way. First, lets make a simple web form with an email address field and a send button. We might even use some Bootstrap because I’m a trendy programmer now and I like my forms to be cute like Twitter.
I’m also going to toss in some RequredFieldValidators (from the AJAX Control Toolkit) for good measure. I wouldn’t expect such a novice programmer like yourself to understand why I put them there so you can just skip that part.
<div id="divemail" class="panel panel-default"> <div class="panel-body"> <h2>Send Email <h2> <label for="inputEmail">Sender Email</label> <span style="color:Red;"> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="inputEmail" ErrorMessage="Cannot be left blank." Display="Dynamic" SetFocusOnError="true"> </asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid email address." ControlToValidate="inputEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" SetFocusOnError="true"> </asp:RegularExpressionValidator> </span> <asp:TextBox ID="inputEmail" runat="server" class="form-control" placeholder="janedoe@email.com"></asp:TextBox> <div style="text-align:right;"> <asp:Button ID="btnSendEmail" class="btn btn-primary" runat="server" Text="Submit" OnClick="SendEmail"/> </div> </div> </div>
As you can see, the concept is pretty simple. The user will enter an email address then click the ‘Submit’ button which will send that email address a nice (or maybe not-so-nice) email notification.
Moving on to the code behind, lets toss these two lines at the top because they are required and we don’t have a choice.
using System.Net; using System.Net.Mail;
Lastly, let’s add this tidbit to our code behind so we can actually send the email…you know…because that is the whole point.
protected void SendEmail(object sender, EventArgs e) { //gather email from form textbox string remail = inputEmail.Text; MailAddress from = new MailAddress("whatever@gmail.com"); MailAddress to = new MailAddress(remail); MailMessage message = new MailMessage(from, to); message.Subject = "Yo! I'm an email!"; string note = "Hello ! "; note += "I am an email sent from an ASP.NET Web Page! "; note += "See you later! "; message.Body = note; message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true; SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.UseDefaultCredentials = false; client.EnableSsl = true; client.Credentials = new NetworkCredential("whatever@gmail.com", "p@ssw0rd"); try { client.Send(message); } catch { //error message? } finally { } }
And that’s it!
Obviously, I am using Gmail but you can use whatever email service you want as long as they allow you to send emails via smtp. Sometimes Gmail can be a pain in the butt with their security regarding Smtp and you will have to whitelist your ASP.NET server but once you get the security stuff out of the way you are golden.
If you find yourself wondering why you would ever need to send an email notification from your code behind then look no further than the new app I have been working on: DeadDrop.mobi
DeadDrop is a project I started working on because I was bored one day and I was tired of having to send important passwords to our help desk via email (they can’t be trusted). Its not a new idea and I made it in less than a day, so don’t expect to see it at the top of the iTunes app charts anytime soon. However, it should give you an idea as to how sending emails in this fashion might be helpful.
That concludes the blog post for today, I hope to see you again next time! Remember kids, follow your dreams and make them come true.