|
The C# Column
Email-enabled Windows application
Want
to send email from a .NET Framework application? You could do it the hard way
by using sockets to establish a connection to a mail server and then transmit
a mail message using Simple Mail Transfer Protocol (SMTP). Or you could do it
the easy way and rely on classes in the System.Web.Mail namespace.
System.Web.Mail provides a simple managed interface to SMTP.
The core classes are MailMessage, which helps prepare an email message; MailAttachment,
which helps send attachments; and SmtpMail, which places a friendly wrapper
around the host system’s SMTP mail service.
These classes construct and send messages using the CDOSYS
(Collaboration Data Objects for Windows 2000) message component. To send the
mail, either the SMTP mail service of Microsoft Windows 2000 or the specified
SMTP server is used. In our application we will use ‘localhost’
as the SMTP server.
Although the mail facility is required mainly by Web applications,
often even WinForm applications need to send mails.
We
will create a WinForm application that is capable of sending mail and/or an
attachment. Design the form as shown in the following screen capture.
We have placed several labels and text boxes along with two
buttons. The textboxes are respectively given names as txtto, txtfrom, txtcc,
txtbcc, txtsubject, txtattach and txtbody. The buttons are named as butsend
and butattach respectively.
Declare a reference msg to the MailMessage class as data member
of the Form1 class and initialise it in the constructor as shown below.
msg = new MailMessage( ) ;
Add the Click event handler for both the buttons. These handlers
are given below.
private void butattach_Click(object sender,
System.EventArgs e)
{
if ( filedlg.ShowDialog( ) == DialogResult.OK )
{
MailAttachment ma = new MailAttachment
(
filedlg.FileName ) ;
txtattach.Text = filedlg.FileName
;
msg.Attachments.Add ( ma ) ;
}
}
To enable the user to select a file for attaching to the
mail, we have displayed the OpenFile dialog with name filedlg. We have passed
the selected file name and path to the constructor of the MailAttachment class.
The attachment represented by an object of the MailAttachment class is then
added to the Attachments collection of the MailMessage object.
After attaching the file, click the ‘Send’ button.
In the ‘Send’ button handler we have filled various properties of
the MailMessage class with respective values. Here, mentioning To and From properties
are mandatory.
private void butsend_Click ( object sender, System.EventArgs
e )
{
if ( txtto.Text != null )
msg.To = txtto.Text ;
else
return ;
if ( txtfrom.Text != null )
msg.From = txtfrom.Text ;
else
return ;
msg.Cc = txtcc.Text ;
msg.Bcc = txtbcc.Text ;
msg.Subject = txtsubject.Text ;
msg.Body = txtbody.Text ;
try
{
SmtpMail.SmtpServer = “localhost”
;
SmtpMail.Send ( msg ) ;
}
catch ( Exception ex )
{
MessageBox.Show (
“Error in sending mail”
) ;
}
}
The SmtpServer property specifies the SMTP server through
which mail will be sent.
If the SmtpServer Property is not set, the mail is, by default,
queued on a Windows 2000 system. We can check this mail in the ‘C:\Inetpub\mailroot\Queue’
folder.
Add reference to the System.Web.Mail namespace at the beginning
of the program. Also add the reference to the ‘System.Web.dll’ in
the Solution Explorer. Run the program and enjoy mailing.
If the application throws an exception when you click the
‘Send’ mail button, make sure your machine’s SMTP service
is running. You can do that in the IIS configuration manager or in the ‘Administrative
Tools | Services’ Control Panel applet.
Second, make sure the SMTP service is configured to allow
relaying from ‘localhost’. To do that, open the IIS configuration
manager from the Administrative Tools, right-click ‘Default SMTP Virtual
Server’, select ‘Properties’, click the ‘Access’
tab, click the ‘Relay’ button, select ‘Only List Below’,
and use the Add button to add 127.0.0.1 to the list of computers allowed to
relay.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert, speaker
and author with several best-sellers to his credit, including titles like
“Let Us C” and the “Fundas” series. Contact him at kanetkar@dcubesoft.com |
|