HomeWeb HostingReseller Web HostingDedicated ServerResources & Support
 

HOW TO SEND MAIL WITH AN ATTACHMENT

As a developer in many faces of development we need to send e-mail to client, which provide meaningful information to client and there we need to use sending/receiving mail facility available in ASP.NET. Sometimes we also need to provide some files to client when we send email and at that time we have to attach document with mail, we want to send to client.

As I have seen that most of the developer or other people who are related to development field, programmer, or students, are facing problem when they have to send mail or I can say when they have to send mail with an attachment. You will be surprised if I say that it is very easy to send mail and also very easy to send mail with attachment. At this stage you may not be agree with me but when you go through this article, you will feel that it is really easy to send mail and also easy to send mail with an attachment.

Now let us focus on few programmatic aspects for sending email. First of all you will have to check whether SMTP service is on or not, if you want to send mail using SMTP service. If it is not in a ready state than you will not be able to send mail using SMTP service, so before starting other work just make SMTP service on. Now as we know that we must have to import required namespace for our application to run, if we never import required namespace then we will receive an error because all the namespace contain required method and sub classes which is compulsory to inherit for our application to run.So if you want to send mail using ASP.NET then you have to import one namespace called ‘System.web.mail’. Now let me give you one full example on how to send mail using ASP.NET so that you can understand all the programmatic aspects very clearly and after going through the article you will be able to send mail. One example is given below observe it because I have given line by line explanation of full code.

 

EXAMPLE:

 

we have to create object of MailMessage which contains all methods to send mail

        Dim mailmessage As MailMessage

 

‘we have to create object of MailAttachment to attach document with mail

        Dim mailattachment As MailAttachment

 

‘it is body of the massage

        Dim body As String

 

‘here I have instantiate the MailMessage  object

        mailmessage = New MailMessage

 

‘here I have instantiate the MailAttachment  object

        mailattachment = New MailAttachment(Server.MapPath("abc.txt"))

 

‘here we have to specify the sender of the mail

        mailmessage.From = "abc@xyz.com"

 

‘here we have to specify the recipient of the mail

        mailmessage.To = "pqr@mno.com"

 

here we have to specify the subject of the mail

        mailmessage.Subject = "Testing of sending mail"

 

‘here we have to specify the format of the mail here it is in html format.

        mailmessage.BodyFormat = MailFormat.Html

 

‘below 2 lines is body of the tag.

        body = "Hello "

 

        body += "I have sent mail to you<br><br>"

 

‘store the whole message to body method of MailMessage class.

        mailmessage.Body = body

 

‘here we have to provide the attachment we want to attach with mail.

        mailmessage.Attachments.Add(mailattachment)

 

‘here we have to specify the SMTP server we are going to use for sending mail.

        SmtpMail.SmtpServer = "mail.xyz.com"

 

‘always use the try catch block here for security purpose.

        Try

 

‘use the send method of MailMessage class and pass the object of MailMessage

            SmtpMail.Send(mailmessage)

 

        Catch ex As Exception

 

‘try block is over here.

        End Try

 


(Posted By Aden on Thursday, April 27, 2006)

 Read All Stories
 

 
   
 
 
» Host multiple domains in one plan
» Manage all your domains in one control panel
» Both Windows and Linux Options
» Windows Plans starting at $9.95 per month
» Linux Plans starting at $6.95 per month
» Windows Plan: MS SQL 2005 Database Included
 
Window Plans   Linux Plans
 
Close  

Copyright 2003 - 2007 All rights reserved Accu Web Hosting