Solved

Send Email with attachments using C#

Posted on 2010-08-23
6
312 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
Independent Software Vendors: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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