

That will make the code a little shorter (it’s down to 26 lines now). You could simplify the previous code by instantiating the MailAddress objects in-line. Slightly simpler version, still without a wrapper/facade MailAddress toAddress1 = new toAddress2 = new toAddress3 = new ccAddress1 = new ccAddress2 = new bccAddress1 = new bccAddress2 = new bccAddress3 = new bccAddress4 = new = "Email subject here" MailAddress fromAddress = new = fromAddress

Send an e-mail - complex version, with multiple "to", "cc", and "bcc" addresses. It takes 36 lines of code to send out this sample e-mail. Plus, you need to use three different types of objects – MailMessage, several MailAddress objects, and the SmtpClient. If you want to send an e-mail to multiple recipients (or CC and BCC recipients), the code gets noticeably longer. More complex version, without a wrapper/facade You also need to remember to include “using” statements for “System.Net” and “”. SmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials Īfter you create the MailMessage, there is a little configuration that needs to be done for the mail server (SmtpClient). SmtpClient smtpClient = new SmtpClient("mailservername") New "Email subject here", "Email body here") Send an e-mail - simple version, with one "to" address If you’re sending a simple e-mail – only one “to” recipient – the code can be fairly simple, like this: If you have wrapper classes, that don’t do anything other than inherit from the library’s classes, you might be able to make one change in the wrapper class, in order to make the updated library work the way you want it to work.įor this demonstration, I’ll create a wrapper class in C# that sends an e-mail. And, occasionally, an update has a change that breaks your existing code. Many libraries are updated at least once a year. The second reason, which is less common, is to hide the implementation of third-party libraries – especially if they might change over time. It would be nice if you could create a single, simpler object that will hide all that complexity. That’s a lot of objects to create, in a specific order, just to send out an e-mail.

You also need to remember to include two “using” statements for namespaces that are rarely used. Then, you can finally send out the e-mail. In order to send an e-mail from a C# program, you need to create an e-mail message object, possibly create multiple objects for the e-mail addresses, and create an e-mail server object. For example, if you want to send an e-mail. The first is when you have a class, or a group of classes, that are complex to work with. There are two main reasons why you would use a wrapper.
