1

I'm working on my very first project, and I need to add a function as the button_click event. Function should open a "send new e-mail" form of the default email client, empty, without any destination,subject or body just with an attached file.

I went through many similar tutorial on the stackoverflow and codeproject but couldn't solve it. I found similar functions which send a message from code, but not just open an empty e-mail form and attach the required file. But couldn't modify successfully.

I'm sure there are people looking for this kind of solution as well.

what I tried so far is :

protected void btnSend_Click(object sender, EventArgs e)
{
    string value;
    value = lstpdfList.SelectedItem.Text;
    string file = "W:/" + value + ".pdf";

    MailMessage message = new MailMessage();
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
    ContentDisposition disposition = data.ContentDisposition;
    disposition.CreationDate = System.IO.File.GetCreationTime(file);
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
    disposition.ReadDate = System.IO.File.GetLastWriteTime(file);
    message.Attachments.Add(data);
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
N K
  • 287
  • 3
  • 11
  • 19
  • 2
    MailMessage is a .Net class within the System.Net.Mail namespace and is effectively an email client without a UI. If you want to manipulate an external program, you need to interact with its API. If you stipulate which email packages are supported it's usually a lot easier - here's an example of what you want to do using Outlook: http://stackoverflow.com/questions/6148639/how-to-open-outlook-new-mail-window-c-sharp – dash Sep 25 '12 at 07:57
  • D'oh. Your edit changes the question significantly. You'll need to have some client code in the browser to open the mail package and attach a file. There are several issues with this - for example, would you be very happy if you visited a website which then proceeded to open your email client and attach files to it? That's one of the reasons this functionality is hard to provide over the net outside of the default `` links. – dash Sep 25 '12 at 08:06
  • It will be a web application (asp.net web site) for internal use in company. There is a list of manuals in a listbox, and when they select one, they should be able to add it as attachment to a new e-mail form in outlook. Could you help me ? really important.. Thank you – N K Sep 25 '12 at 08:08
  • 1
    Your last comment contains what looks to me like an impossible requirement. The closest you can come to this in a web application is to send the email from the server without involving Outlook at either end. In this case, just follow the instructions on http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx – tomfanning Sep 25 '12 at 08:19
  • It's not impossible, but it's not pleasant, either. If you are in an intranet environment, you can do the following: http://social.msdn.microsoft.com/Forums/pl-PL/netfxjscript/thread/8c0438cf-be8c-41e7-95fa-0aa2e8f94b77 but it's unlikely to work with anything other than Internet Explorer. – dash Sep 25 '12 at 08:57
  • As @tomfanning said it's an impossibility to open outlook from asp.net with an attachment but no other content, your options are: Use SMTP code in tom's answer to send an email via code automatically or use my option to use exchange services. – Ryan McDonough Sep 25 '12 at 08:58
  • It's not impossibe, but it's not pretty either - you can either attempt to manipulate Outlook via some client side code (with the appropriate trust levels etc) or embed a WinForms/WPF user control in the browser to do this for you (with the usual caveats). It might just be easier to use SmtpClient and just send the email - you can still choose the attachment, have the user type in the body, preview the message, it's just done via a WebForm instead of the native email application. – dash Sep 25 '12 at 09:10

2 Answers2

1

You can't attach a file from ASP.net to outlook, it's a security issue.

If you have access to the Exchange Web Services you can interact directly with Exchange to send an e-mail from that users account with attachments etc.

You may have to delegate access to the user account used to execute the ASP.NET request to successfully be able to interact with the Exchange Server Services, you could use ASP.net impersonate as well.

Check out the documentation at:

http://msdn.microsoft.com/en-us/library/exchange/bb409286(v=exchg.140).aspx

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
  • Why bother doing this? SmtpClient is 2-3 lines of code and supports everything the user has asked for. Completely unnecessary to use EWS. – tomfanning Sep 25 '12 at 08:55
  • It's just another option, as it's an intranet site it may be more useful it's planning to scale to use the EWS. – Ryan McDonough Sep 25 '12 at 08:59
-1

You can't automate Outlook on the client-side from a web application. And you shouldn't invoke Outlook on the server.

What you can do however is to send an email from the web server, without involving Outlook.

To do this, just follow the example on MSDN for SmtpClient.Send().

There's also an example of programmatically creating a MailMessage with attachments here.

public static void CreateTestMessage2(string server)
{
    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    MailMessage message = new MailMessage(from, to);
    message.Subject = "Using the new SMTP client.";
    message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
    SmtpClient client = new SmtpClient(server);
    // Credentials are necessary if the server requires the client  
    // to authenticate before it will send e-mail on the client's behalf.
    client.UseDefaultCredentials = true;

    try 
    {
        client.Send(message);
    }  
    catch (Exception ex) 
    {
        Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString() );           
    }              
}
tomfanning
  • 9,552
  • 4
  • 50
  • 78