Click here to Skip to main content
15,392,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi after clcikcing the file upload control can anybody please help me in how to send a mail to someperson incluidng that attachment
Posted
Updated 25-Dec-13 17:55pm
v2
Comments
ravikiran from Hyderabad 26-Dec-13 0:04am
   
Details required?

You have a webserver or what?

you can send Emails using gmail also.
spanner21 26-Dec-13 0:07am
   
yes i am having my default server to there i will be semding mail from my controls like textbox texta and fileupoadcontrols file..now after sending mail to that server it will automatically send to the users who needs that mail ..

Please follow this link. It will help you lot.

http://www.aspsnippets.com/articles/Attach-files-to-email-without-storing-on-disk-using-ASP.Net-FileUpload-Control.aspx[^]

Or take a look at this ,
 Here’ I’ll explain how to attach files to email without saving them on the disk

You can refer my articles on how to send emails in

Send SMTP Emails using System.Net Class in C#   and Send SMTP Emails using System.Net Class in VB.Net

When the File is uploaded using FileUpload Control the file is received in the httppostedfile

property.

To attach the HttpPostedFile  to the email. You will need to convert it to Stream.

The code below attaches a posted file to email. Note that txtAttachment is  a FileUpload control

<asp:FileUpload ID="txtAttachment" runat="server" />

C#

if (txtAttachment.PostedFile != null)

{

    try

    {

        string strFileName =

        System.IO.Path.GetFileName(txtAttachment.PostedFile.FileName);

        Attachment attachFile =

        new Attachment(txtAttachment.PostedFile.InputStream, strFileName);

        mm.Attachments.Add(attachFile);

    }

    catch

    {

 

    }

}
   
v2
Comments
spanner21 26-Dec-13 0:05am
   
HI bhavesh it is showing file not found at that link
BK 4 code 26-Dec-13 0:26am
   
Please see my updated solution
Two Situations

1. You are running your website in Host.

2. You are running your website in Another machine(Client).


1.

Using different browsers you will get problem

(suppose chrome displays only filename insted of path.)


2.

eg: In your client machine filepath (C:\asdf.txt)
server you may not contain C:\asdf.txt file.


Solution:
Better thing upload file into Server.
and send from that path.
delete file after sent email.

(suggesition use different program for delete).
   
Copy file to server.
Attach to mail.
Send mail.
Delete file after sending.
use namespaces
C#
using System.Net.Mime;
using System.Text;
using System.Net.Mail;
using System;


and method

private Boolean SendMailUsingGmail(String mailTo, String mailText, String attachmentFileWithPath)
 {
     Boolean sendMailResult;
     try
     {
         SmtpClient smtpServer = new SmtpClient();
         smtpServer.Credentials = new System.Net.NetworkCredential(mailFromId, mailFromPassword);
         smtpServer.Port = 25;
         smtpServer.Host = "smtp.gmail.com";
         MailMessage sampleMail = new MailMessage();
         sampleMail.From = new MailAddress(mailFromId);
         sampleMail.Subject = "Mail";
         string file = attachmentFileWithPath; //file path
         Attachment data = new Attachment(file, MediaTypeNames.Application.Zip);
         ContentDisposition disposition = data.ContentDisposition;
         disposition.CreationDate = System.IO.File.GetCreationTime(file);
         disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
         disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
         // Add the file attachment to this e-mail message.
         sampleMail.Attachments.Add(data);
         sampleMail.To.Add(mailTo);
         sampleMail.Body = mailText;
         smtpServer.Send(sampleMail);
         sendMailResult = true;
         return sendMailResult;
     }
     catch (Exception ex)
     {
         string exceptionString = ex.Message;
         sendMailResult = false;
         return sendMailResult;
     }
     finally
     {
     }

 }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900