Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hai Frnds,

How to send mail with one attachmnet (this attachmnet is located in ma system..how to specify the file path in mail attachmnt..i dont need fileuploader...i wnt to programtically specfy the path like 'C:/user/....'.Now i am using System.Net.Mail.MailMessage for sending mail in my c# .net web application...please help me fast..i stuck ma project....

Thanks n Advnc
Aatif from Bangalore
Posted

You can't.

The C# code runs on the server, and it has no access to the client hard drive - for good security reasons. If you want to attach a file to an email message generated by the server, the attachment file must be uploaded to the server first.
 
Share this answer
 
Comments
Aatif Ali from Bangalore 13-Jun-13 2:37am    
Thnks Brthr,suppose our prjct contain one special folder,it contain some files..can i send these files as attachmnt...
OriginalGriff 13-Jun-13 2:56am    
Only if the folder is on the server. The server cannot access any files on the client unless the client has exposed a folder to the whole of the internet - a very, very poor idea under any circumstances.
Espen Harlinn 13-Jun-13 17:48pm    
5'ed! :)
I suspect your problem is some misunderstanding of attachments. The word "attachment" is a kind of misnomer: nothing is actually "attached" to anything, so all your concerns about path names and original file locations are simply irrelevant. In fact, what they call "attachment" is merely the part of a multipart mail, which is nothing but a block of text. An "attached" file is simply embedded inside this text block, most typically in base64 form.

Please see:
http://en.wikipedia.org/wiki/Email_attachment[^],
http://en.wikipedia.org/wiki/Base64[^].

Here is the .NET class you can use with System.Net.Mail.MailMessage, complete with some code sample: http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 13-Jun-13 17:48pm    
5'ed!
Sergey Alexandrovich Kryukov 13-Jun-13 18:33pm    
Thank you, Espen.
—SA
Use the system.net.mail.attachment object... here's some VB sample code. Change it to C# and you're away.

VB
Dim sFile As New System.Net.Mail.Attachment(strPathAndFile)
mailObj.Attachments.Add(sFile)
 
Share this answer
 

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