Solved

Send Email with attachments using C#

Posted on 2010-08-23
6
300 Views
Last Modified: 2013-11-08
Hi Experts,
I am using C# aspx page. There are 4 text boxes and browse button in my page. Browse each file  to display in the textbox(show path\name of the file). When i click send button , send these files  in email.

Kind Regards,
Pooja
0
Comment
Question by:poojasureshkumar
  • 4
6 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33501606
using snmp server (http://www.devx.com/tips/Tip/29291, http://www.aspnettutorials.com/tutorials/email/email-attach-aspnet2-csharp.aspx):String server = "127.0.0.1";private void btnSend_Click(object sender, System.EventArgs e){MailAddress to = new MailAddress("admin@.mail.adr");MailAddress from = new MailAddress(txtFrom.Text);MailMessage message = new MailMessage(from, to);message.Subject = txtSubject.Text;message.Body = @"" txtContent.Value;Attachment attachFile = new Attachment(txtAttachmentPath.Text);message.Attachments.Add(attachFile);SmtpClient client = new SmtpClient(server);try{client.Send(message);}catch (Exception ex){throw new Exception(ex.Message, ex);}}The inputs you set in you html body.for tutorial also check http://www.jscape.com/articles/sending_email_using_csharp.html
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33501636
replace txtAttachmentPath.Text with the file path to attach to the email.
replace server with the ip of Smtp server.
in case you need Smtp credentials:

SmtpClient smtp = new SmtpClient("<SMTP_SERVER_IP>", 25);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(<USAERNAME>, <PASSWORD>)
smtp.Send(mail);
0
 
LVL 11

Expert Comment

by:bmatumbura
ID: 33501734
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:poojasureshkumar
ID: 33505169
Hi Experts,
I am confused . Sorry about that. To begin with,
Please help me how to send one file in email. Consider page has text box1 ,browse button ,submit button.When i click browse , i should select the (.txt or .img or .pdf or.???) file . The file name should display on text box1. When i click send , it should send to target emailid with attachment.

2nd step, how to send multiple files. These files should not save locally.

Please enlighten me Step 1 first.

Kind Regards,
Pooja
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33508048
use the function posted here.
on submit button click event handler call this function and pass the parameters required.
void SendEmail(string server, string fromAddress, string toAddress, string subject, string body, string[] attachments)
        {
            MailAddress to = new MailAddress(toAddress);
            MailAddress from = new MailAddress(fromAddress);
            MailMessage message = new MailMessage(from, to);

            message.Subject = subject;
            message.Body = body;
            foreach (string attach in attachments)
            {
                message.Attachments.Add(new Attachment(attach));
            }

            SmtpClient client = new SmtpClient(server);

            client.Send(message);
        }

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33508052
for example:
SendEmail("127.0.0.1", "jonny@hotmail.com", "roy@hotmail.com", "Hello Brother", "Check the pic i sent you...", new string[]{"c:\temp\pic.jpeg"});
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now