4 Replies - 42100 Views - Last Post: 22 January 2010 - 11:42 PM Rate Topic: -----

#1 srikanth_prabhala  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 31-July 07

Attach files in an email

Posted 31 July 2007 - 12:00 PM

Hello,

I am working on a .Net Web application that requires exporting the contents of the screen (typically, a report) into a pdf file and attach it in an email. I tried this option, however could only generate a link that gets automatically included in the email - I have to click on the link to open the temporary folder (into which the file is saved) and copy it from the folder into the email.

Is there a way to directly have the file show up in the email as an attachment ?

Please help.

Thanks
Srikanth

Is This A Good Question/Topic? 0
  • +

Replies To: Attach files in an email

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Attach files in an email

Posted 31 July 2007 - 02:34 PM

You can create attachments to an email by using the MailMessage object from the System.Net.Mail namespace. Just add the namespace, create the object and there among the members of that class you will see an "attachments" collection which you can add to using the method "add" (them tricky little developers. Then simply create an SmtpClient class instance and pass it the mailmessage object with attachment. It will do the rest!

MSDN for MailMessage Class (notice the attachments collection it has)
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Attach files in an email

Posted 31 July 2007 - 07:03 PM

Here is a simple example of sending an email in C# with an attachment

public static void CreateMessageWithAttachment(string server, string file)
{
	// Create a message and set up the recipients.
	MailMessage message = new MailMessage(
	   "jane@contoso.com",
	   "ben@contoso.com",
	   "Quarterly data report.",
	   "See the attached spreadsheet.");

	// Create  the file attachment for this e-mail message.
	Attachment attach = new Attachment(file);
	// Add the file attachment to this e-mail message.
	message.Attachments.Add(attach);
	//Send the message.
	SmtpClient client = new SmtpClient(server);
	// Add credentials if the SMTP server requires them.
	client.Credentials = CredentialCache.DefaultNetworkCredentials;
	client.Send(message);
}



Hope this helps :)
Was This Post Helpful? 1

#4 srikanth_prabhala  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 31-July 07

Re: Attach files in an email

Posted 02 August 2007 - 04:50 AM

Thanks a lot for your effort.

Our requirement is to send the mail thru a mail client so that the user can type email address and body of the email. The program shall just attach the file and open up an email client on user's machine.
Was This Post Helpful? 1

#5 grnayak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-January 10

Re: Attach files in an email

Posted 22 January 2010 - 11:42 PM

Thanks a lot...It helped me a lot.... :D :^:

This post has been edited by grnayak: 22 January 2010 - 11:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1