Solved

Send Email with attachments using C#

Posted on 2010-08-23
6
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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