Solved

Send Email with attachments using C#

Posted on 2010-08-23
6
305 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stream.BeginRead and Stream.EndRead in .NET Core 5 37
Connection String 16 43
Automated testing suggestions? 2 26
C# Comparing String Arrays. 5 20
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

21 Experts available now in Live!

Get 1:1 Help Now