SIGN UP MEMBER LOGIN:    
ARTICLE

How to Send an Email with Attachment

Posted by Muhammad Shakir Articles | How do I August 02, 2007
This article shows how to send an email to anyone from your server. Also look how to send a file as attachment with an email.
Reader Level:

In old era, letters were used to share your feelings, thoughts and emotions (in word expression). But now days, you may share these all things in their real form as you desire via new technology (Email). There are many websites that provides you email facility as Hotmail, Yahoo. If you are programmer and you want to send an email via your application then it is very interesting idea.

 

Now, Question is how? It is be very simple way. In .net, this functionality is already build-in. you just need to call that DLL file as reference and use it. Two DLL files use 'System.Net.Mail' and 'System.Web.Mail'.

 

In my example, I am using 'system.web.mail' which send my mail with single attachment. You may send multiple attachments by creating an object of Mail Attachment then add number of files and then assign to attachment property.

 

I am creating an object of  'MailMessage' and assigns emails to TO and FROM property and set text message to body property and attachment (its single attachment) then make a new object of SmtpMail and sets local server (from where to send) then call its property Send and assign the MailMessage object. Now call this function, you email is ready to send. 

 

NOTE: Smtp server name will be your server name or IP

 

public void SendMail(string mailFrom, string mailTo, string copyTo)

{

     mail = new MailMessage();

     mail.From = mailFrom;

     mail.To = mailTo;

     mail.Cc = copyTo;

     mail.Subject = this.subject;

     mail.Attachments.Add(new MailAttachment("c:\test.txt");

     mail.Body = this.body;

     mail.Priority = MailPriority.High;

     mail.BodyFormat = MailFormat.Text;

     SmtpMail.SmtpServer = "172.16.0.23";

     SmtpMail.Send(mail);

}

Login to add your contents and source code to this article
share this article :
post comment
 

can i attach file from FileUpload control or directly via link (path) of that file on my server????

Posted by kishan gajjar Mar 03, 2010

hello, this is prabhakr reddy.iam working in starminds soultions,in bangalore.plz help me,with out using local smtp server.plz

Posted by suma latha Feb 05, 2008
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor