Sending email notifications from C# ASP .NET web applications can often become a troublesome process. Automatic emails from software may be marked as junk or spam by many ISP mail servers. This can make it difficult for users to properly receive email notifications from software products and the messages themselves can be very important to the user. While one solution may be to ask users to add your web applications email address to their junk mail filter’s safe-list, this process can be difficult to control and unreliable. A possibly better solution is to use a 3rd-party email service to route messages through, such as Gmail.
In this article, we’ll create a basic C# ASP .NET web service which sends emails using Gmail’s SMTP server and your own Gmail account. The web service that we’ll create can by used by any range of applications, written in any programming language, simply by communicating with the web service in XML to send emails.
Gmail is one of the most popular web-based email services around, next to Hotmail and Yahoo, and also has one of the best deliverability rates and junk mail filtering systems. These features make Gmail a good choice as a 3rd-party email provider and even for sending programmatic emails and software notifications from web services, C# ASP .NET web applications, and desktop software, when used responsibly. Gmail provides the ability to route email through the Gmail SMTP server, using your Gmail account, and keep the From and Reply-To address listed as your own email address. With this feature, your C# ASP .NET web applications can send email automatically through Gmail, but use your own (non-Gmail) email address as the sender. This allows you to continue using your own email address for software-based notifications, but avoid any marking as spam that sending through your own domain’s SMTP server might encounter.
Creating Your Gmail Account and Linking to Your Domain
The first step to creating our Gmail emailing web service is to setup a Gmail account to act as our primary email account for our C# ASP .NET web application. The steps to create a Gmail account and setup your own (non-Gmail) email address as the sender and reply-to are listed below:
Visit http://www.gmail.com and create an account.
Once created, log in and click Settings along the top right menu.
Click the Accounts tab.
Click “Add another email address you own”.
Enter your own (non-Gmail) email address and fill in the Reply-to address as well.
Verify your email address with gmail and it should be added to your account.
Back in the Accounts tab, find your newly added email address and click “Make Default”.
Select the radio button for “Always reply from default address”.
It’s also a good idea to click the Forwarding tab and select the radio button for “Forward a copy of incoming mail to” and specify your (non-Gmail) email address. This way you’ll receive any email sent specifically to the gmail account at your standard email address, such as Mailer Daemon messages, bounce-backs, or message failures.
With your Gmail account created and setup to send email as your own domain’s email address, we’re ready to move on to creating the C# ASP .NET web service to send the emails.
Getting to Know System.Web.Mail and the MailMessage
System.Web.Mail is one of the more common methods for communicating with SMTP servers to send email from C# ASP .NET web applications. The MailMessage object is the core object for composing messages, populating the sender, recipient, cc, bcc, and other necessary fields for sending email. A typical example for using the MailMessage object to send an email in C# ASP .NET, based upon a contact page in a web application, would be as follows:
Note in the above code, we create a new MailMessage object and populate the To, From, Subject, and Body fields to prepare the email message. We then set the SMTP server and send the email. The .NET framework takes care of the details of transmitting the email, which makes the SmtpMail object particularly easy to use.
While sending email to standard SMTP servers is straight-forward with the MailMessage object and the SmtpMail feature, sending email through Gmail’s SMTP server takes a few extra steps, largely for security reasons. Therefore, we’ll need to create our own MailMessage class to specifically handle Gmail email messages.
Hacking the System.Web.Mail.MailMessage
Sending email to standard SMTP servers was quite easy using SmtpMail. However, Gmail’s server requires a secure connection with SSL enabled and requires a special port. To implement these details, we need to create our own GmailMessage class to handle the details of communicating with these security rules in place.
We’ll be using the RC.Gmail class, which inherits from System.Web.Mail.MailMessage to handle the secure communication with Gmail’s SMTP server.
public class GmailMessage : System.Web.Mail.MailMessage
The first item to note in the above code is that the class inherits from MailMessage. This allows the class to function almost exactly like a MailMessage object when sending emails. The specialized differences for sending email through Gmail is most notably apparent in the constructor, where we set the Gmail SMTP server to smtp.gmail.com, the port to 465, and the required security flags for sending emails in C# .NET using SSL (secure sockets layer) and authentication. Note, the authentication portion uses the Gmail account username and password as the valid login to send emails. With the MailMessage custom configuration complete, we can use the underlying MailMessage object as we would normally, by simply filling in the From, To, Subject, and Body properties and sending the MailMessage. Once again. the .NET framework takes care of the details of talking to Gmail’s SMTP server in secure mode and on port 465.
With our customized Gmail MailMessage class complete, we can now begin sending emails.
Sending Email Through Gmail
Just as we did with the standard MailMessage class, we can send Gmail messages using similar client code, implementing our new GmailMessage class, as follows:
Note the above code is very similar to our initial client code for sending emails with MailMessage. The only difference is that we instantiate the GmailMessage object and populate it accordingly. It’s also important to note that if you’ve registered an outside email address with your Gmail account in order to send emails from a different email address other than the Gmail account, you’ll need to provide that email address in the From field. You’ll also need to pass your Gmail account login information into the constructor of the GmailMessage, for authentication with Gmail. With the client code complete, you should be able to send email through Gmail from your C# ASP .NET web application.
Email is Nice, But Where’s the Web Service?
The client code has been completed and we can now send emails through Gmail in C# ASP .NET, using a secure connection and customized SMTP port. However, we can enhance this functionality much further and provide Gmail emailing capabilities to all of our web applications, in many different programming languages, by providing a web service containing the client code. Our custom applications could then send email through Gmail, using the same method, by simply communicating with XML to invoke the C# ASP .NET web service. Note, for simplicity, the web service code below is not setup to use SSL, https, or other web service network security. You may wish to further enhance the web service’s security before using in production.
To begin, we can create a web service GmailMailer.asmx.cs as follows:
The above code is the framework for a C# .NET web service. Our web service contains a single method GmailSend() which takes the necessary information for sending an email and an authentication key. While the code for sending the email through Gmail (inside the try/catch block) is the same as previously shown in our client code example, it’s important to note the authenitcation key (authkey) check at the beginning of the method. While this helps prevent misuse of the web service, there are several more ways to better protect the web service and, in practice, you may wish to include these further security validations to make the service more secure.
Speaking Our Language with C
Visual Studio allows us to easily invoke a web service method by referencing the web service in the Visual Studio project and instantiating the client object. To do this, simply right-click the project name in Visual Studio and select Add Service Reference. Enter the URL pointing to your .asmx web service (created in the steps above). Visual Studio will automatically create a client class to handle instantiating the C# .NET web service, as follows:
GmailMailerSoapClient gmailMailer = new GmailMailerSoapClient();
Visual Studio makes it very easy to call the web service method with the above client code and send emails through Gmail from your C# ASP .NET web applications. However, what if you have applications written in other programming languages that you wish to have send emails through Gmail as well? Rather than re-write the Gmail emailing class code in the other languages, we can call the same C# ASP .NET web service by sending an XML SOAP packet and verifying the result.
Communicating with our .NET Web Service in CGI Perl
As an example of communicating with our C# ASP .NET web service using a different programming language other than C# .NET, we’ll take the example of wanting to send email through Gmail using a CGI script or Perl. Therefore, we’ll need to call the .NET web service with CGI Perl and SOAP XML.
You’ll first need to create a CGI Perl client stub to invoke the C# .NET web service. In the case of CGI Perl, we can create the following method:
We can call the above CGI Perl code to send email with Gmail through our C# ASP .NET web service with the following line of CGI Perl code:
sendEmail("johndoe\@somewhere.com", "Hello World!", "Send from C# ASP .NET.\nHello World!");
There are a vast number of different programming languages which can utilize the C# ASP .NET web service for sending email notifications with Gmail. They simply need to be able to support posting a SOAP XML packet to a remote URL. C# .NET web services make it easy to communicate with web applications written in other languages, such as CGI Perl, PHP, ColdFusion, C, C++, and many more.
C# ASP .NET web applications often require sending email notifications from within the software to users requesting information or feedback. While some ISP mail servers may reject messages as junk or spam, Gmail provides an easy and potentially more reliable alternative SMTP server for sending email notifications from C# .NET applications. Communicating with Gmail’s SMTP server requires customizing the C# .NET MailMessage object to use a secure connection and custom port 465 to send emails through. By wrapping the Gmail email class within a C# .NET web service, we can share the functionality of Gmail email notification with our various applications written in many different programming languages, simply by posting an XML packet to the web service URL.
About the Author
This article was written by Kory Becker, founder and chief developer of Primary Objects, a software and web application development company. You can contact Primary Objects regarding your software development needs at http://www.primaryobjects.com