There's something out there in life that will delight and amaze you.
Get up, get moving, and go find out what it is. - (Ralph Marston)

Wednesday, February 01, 2006

Sending mail using ASPNET

Sending mail using ASPNET

This article will mainly focus in two parts. First sending a mail with out attachments and then sending a mail with the attachments. Further this article gives some points concerning when creating an email control using DotNetNuke. As to illustrate the points; code written below is in C#.

In order to send a mail first we need to reference the Web.Mail class library as follows.

using System.Web.Mail;

If you have already designed the page layout with the relevant controls that you will need, now let’s take a look at the code for the SEND button.
Lets assume you have four text boxes for the, To address, From Address, Subject and the Description in the UI and a link button to SEND the mail.

MailMessage objMail = new MailMessage();

objMail.To = txtToAddress.Text;
objMail.From = txtFromAddress.Text;
objMail.Subject = txtSubjectTitle.Text;
objMail.Body = txtDescription.Text;


The MailMessage class in .NET is in charge of sending emails. There fore, first we create an object from MailMessage class. Once you create a mail object, you need to assign the vales for the properties of the mail object. It has the properties such as to, from, subject, body and so on. In the above code we have assigned the relevant values from the controls.

When sending a mail we need to specify for which email server we are sending the mail to. As for that, we should set a value for the SMTPMail.SMTPServer. This value should be your email server IP Address. If you have hosted in the production server the corresponding email server used with in the organisation should be referenced.

SmtpMail.SmtpServer = "192.167.0.24"

To send the mail, we could pass the mail object to the send method of the SmtpMail.

SmtpMail.Send(objMail);


Sending a mail with attachments.

Since we are attaching a file, first we need to create an HTML Input File as below.

protected System.Web.UI.HtmlControls.HtmlInputFile my_attFile;


MailAttachment is a class which is responsible for handing mail attachments. There fore we will create a MailAttachment object and assign the relevant file to it as shown in the code below. But first we could check whether a file is attached or not.

if (my_attFile.PostedFile.FileName.ToString() != "" )
{
MailAttachment objmyAtt = new MailAttachment(my_attFile.PostedFile.FileName);
objMail.Attachments.Add(objmyAtt);
}

But, do keep in mind that you will come across some problems as below.

The files or directories with names with spaces will not be attached by this method.
As a solution to this problem the following approach could be used.
Save the attached file in a directory with a unique name and store the file in the server till it’s attached. Once the mail is sent; delete the directory containing the attachment.

As an extension of the above article, following guide will provide some additional points to concern when creating an email control in DNN.


Email control in dnn

Apart from the above code, when creating an email control in DNN; which is will be used with in the portal, it needs to correspond to a common IP to the entire site. In host settings in dnn, there are SMTP settings which are allowed for host to set. As per the SMTP IP address; we could use the common IP address which will be entered by the host.

First you need to access the Host Settings of the nuke. This returns a hash table; giving the key of the SMTP IP we could retrieve the corresponding value of the SMTP IP Address, by this way it will be possible to set up mails (using an email control) for which, a one mail server for the entire site. And your dnn email control could pick up the IP automatically by this way.

Stating that this control has been used with in a user module in DNN, in order to deploy the control in the DNN portal following should be done.
First you need to copy the email control dll to the common DotNetNuke bin directory. The corresponding .cscx file should be copied to the controls folder and the corresponding .resx file should be copied to the app_resouces folder which is which in the controls folder.

Please note that deployment of a module is not explained here.

2 Comments:

At 10:19 AM, Blogger G said...

Beauty + Brains. what a combo!
:D
G

 
At 6:38 PM, Blogger Gayan Herath said...

share ur knowledge more with others.

 

Post a Comment

<< Home