Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Send Email with attachments using C#

Posted on 2010-08-23
6
Medium Priority
?
339 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:Meir Rivkin
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:Meir Rivkin
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
Technology Partners: 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:Meir Rivkin
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:
Meir Rivkin earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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.
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
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

636 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