Recipe16.7.Send SMTP Mail Using the SMTP Service


Recipe 16.7. Send SMTP Mail Using the SMTP Service

Problem

You want to be able to send email via SMTP from your program, but you don't want to learn the SMTP protocol and hand-code a class to implement it.

Solution

Use the System.Net.Mail namespace, which contains classes to take care of the harder parts of constructing an SMTP-based email message. The System.Net.Mail.MailMessage class encapsulates constructing an SMTP-based message, and the System.Net.Mail.SmtpClient class provides the sending mechanism for sending the message to an SMTP server. SmtpClient does depend on there being an SMTP server set up somewhere for it to relay messages through. Attachments are added by creating instances of System.Net.Mail.Attachment and providing the path to the file as well as the media type.

 // Send a message with attachments string from = "hilyard@comcast.net"; string to = "hilyard@comcast.net"; MailMessage attachmentMessage = new MailMessage(from, to); attachmentMessage.Subject = "Hi there!"; attachmentMessage.Body = "Check out this cool code!"; // Many systems filter out HTML mail that is relayed attachmentMessage.IsBodyHtml = false; // Set up the attachment string pathToCode = @"..\..\16_Networking.cs"; Attachment attachment =     new Attachment(pathToCode,         MediaTypeNames.Application.Octet); attachmentMessage.Attachments.Add(attachment); 

To send a simple email with no attachments, call the System.Net.Mail.MailMessage constructor with just the to, from, subject, and body information. This version of the MailMessage constructor simply fills in those items and then you can pass it to SmtpClient.Send to send it along.

 // Bounce this off the local SMTP service. The local SMTP service needs to // have relaying set up to go through a real email server… // This could also set up to go against an SMTP server available to // you on the network SmtpClient client = new SmtpClient("localhost"); client.Send(attachmentMessage); // Or just send text MailMessage textMessage = new MailMessage("hilyard@comcast.net",                     "hilyard@comcast.net",                     "Me again",                     "You need therapy, talking to yourself is one thing but " +                     "writing code to send email is a whole other thing…"); client.Send(textMessage); 

Discussion

SMTP stands for the Simple Mail Transfer Protocol, defined in RFC 821. To take advantage of the support for SMTP mail in the .NET Framework using the System. Net.Mail.SmtpClient class, an SMTP server must be specified to relay the messages through. Since Windows 2000, the operating system has come with an SMTP server that can be installed as part of IIS. In the Solution, the SmtpClient takes advantage of this by specifying "localhost" for the server to connect to, which indicates the local machine is the SMTP relay server. Setting up the SMTP service may not be possible in your network environment and you may need to use the SmtpClient class to set up credentials to connect to the SMTP server on the network directly.

To set up SMTP relaying after installing the SMTP service via Add/Remove Windows Components in the Control Panel, open the Internet Information Services applet and right-click on the Default SMTP Virtual Server entry. Next, choose Properties. When you select the Delivery tab, you will see the dialog shown in Figure 16-1.

Figure 16-1. Configuring SMTP relaying


Now click the Advanced button to display the Advanced Delivery dialog that you will use to set the relay parameters, as shown in Figure 16-2.

Supply your domain name and the SMTP address for a valid SMTP host, then email away. Once you have the SMTP service set up, you should configure it to respond to requests from only the local machine, or you could become a target for spammers. To do this, go to the Access tab of the Default SMTP Virtual Server Properties dialog, shown in Figure 16-3, and select Connection.

Then once you have selected Connection, select the "Only from the list below" option in the Connection dialog, shown in Figure 16-4, and click the Add button to add an IP address.

Figure 16-2. SMTP relaying, Advanced Delivery options


Finally, enter the IP address 127.0.0.1 to give access to only this machine, as shown in Figure 16-5.

You list will now look like Figure 16-6.

See Also

See the "Using SMTP for Outgoing Messages," "SmtpMail Class," "MailMessage Class," and "MailAttachment Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net