4

I've a scenario in which user can make a selection from a grid (having uploaded files on local folder) and when user press "send", application should open Outlook "New mail message" window having selected files as attachments (which user selected from grid).

Any help will be appreciated.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Adnan Badar
  • 347
  • 2
  • 5
  • 14

3 Answers3

13
Imports System.Diagnostics

Process.Start(String.Format("mailto:{0}", address))

' set all possible parameters: '

Process.Start(String.Format("mailto:{0}?subject={1}&cc={2}&bcc={3}&body={4}", address, subject, cc, bcc, body))

' also escape spaces: '

Process.Start(String.Format("mailto:{0}?subject=\"{1}\"&cc={2}&bcc={3}&body=\"{4}\"", address, subject, cc, bcc, body))

Use next to include new line breaks:

body = body.Replace(Environment.NewLine ,"%0A")

will open default email client with new message composition dialog.

If Outlook is set as default client, it will be opened.


Anyway, never open explicitly non-default client (email, browser, etc) - that breaks clients' will and makes them hate you.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 1
    And how do you add attachments? – MarcelDevG Dec 27 '10 at 11:26
  • @MarcelDevG: If you need to send an email with attachments, send email programmatically: [SmtpClient.Send(MailMessage)](http://msdn.microsoft.com/en-us/library/swas0fwc.aspx) – abatishchev Dec 27 '10 at 11:47
  • This won't work if the string is longer than around 2000 characters or so because of a limitation of Internet Explorer on long urls. – nalply Nov 15 '12 at 09:41
  • Yes this is strange. I have an application with an internal error reporter and the stack traces get truncated. It's really an annoying limitation. I read about it somewhere that IE is the culprit here. Don't ask me why. – nalply Nov 15 '12 at 18:34
  • 1
    @abatishchev There is something wrong in the line Process.Start(String.Format("mailto:{0}?subject=\"{1}\"&cc={2}&bcc={3}&body=\"{4}\"", address, subject, cc, bcc, body)). It's some syntax error. Can you fix this? – tmighty Aug 26 '16 at 05:29
7
Dim Outl As Object
Outl = CreateObject("Outlook.Application")
If Outl IsNot Nothing Then
    Dim omsg As Object
    omsg = Outl.CreateItem(0)
    omsg.To = "yusuf@hotmail.com"
    omsg.bcc = "yusuf@gmail.com"
    omsg.subject = "Hello"
    omsg.body = "godmorning"
    omsg.Attachments.Add("c:\HP\opcserver.txt")
    'set message properties here...'
    omsg.Display(True) 'will display message to user
Devraj Gadhavi
  • 3,541
  • 3
  • 38
  • 67
7

If you want specifically want an outlook message and you want more options on what to send (body text, attachments, BCC, etc.):

Dim Outl As Object
Outl = CreateObject("Outlook.Application")
If Outl IsNot Nothing Then
    Dim omsg As Object
    omsg = Outl.CreateItem(0) '=Outlook.OlItemType.olMailItem'
    'set message properties here...'
    omsg.Display(True) 'will display message to user
End If
abatishchev
  • 98,240
  • 88
  • 296
  • 433
MarcelDevG
  • 1,377
  • 10
  • 10
  • Dear MarcelDevG, Thank you so much, this exactly what i want and its working fine, to only abnormal thing is right now that i am having 2 message windows (having same settings, address, body, message), any idea whats going on. THANKS AGAIN! – Adnan Badar Dec 27 '10 at 13:55
  • I tackled it by not displaying it as Modal. 'omsg.Display(False) Thanks again! – Adnan Badar Dec 27 '10 at 14:41
  • I am getting a bit greedy now :) in new mail message window i am not getting the default signature of the user, how this problem will resolve? – Adnan Badar Dec 27 '10 at 14:44
  • You should really open a new question, and also search first for an existing answer. This is what I found: http://www.outlookcode.com/codedetail.aspx?id=615 It's VBA but you should be ably to translate it. – MarcelDevG Dec 27 '10 at 19:43