2

I'm working on an issue to my project that when I click the button, a default email client should pop out and if there's an attachment, it should be automatically attach to the default email client like this.

enter image description here

I already tried a lot of methods how to do this. First I used MAPI, but the MAPI cannot detect my Default Email Client even though I already set it in Control Panel, It shows this two message box

enter image description here

enter image description here

I already searched the internet about those error but there's no definite or clear answer to me. HERE'S the code I used in MAPI.

I used also the mail:to protocol to call the default email client who's handling to the aforementioned protocol with using this line of codes.

Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()

            Dim filename = Convert.toChar(34) & "C:\USERS\JOSHUA~1.HER\DOWNLO~1\ASDPOR~1.PDF" & Convert.toChar(34)
            Debug.Writeline(filename)
            Dim asd As String = String.Format("mailto:someone@somewhere.com?subject=hello&body=love my body&Attach={0}", filename)
            proc.StartInfo.FileName = asd
            proc.Start()

But still, no luck. I read a thread that the mail:to don't handle attachment anymore, but this line of code opened my default email client with body and subject, but there's no attachment. In terms of the filename variable, I already tried every path format, I read that I should use 8.3 path format. But still doesn't work.

The last method I used is extending the System.Net.MailMessage.MailMessage() following THIS answer. This works in terms of opening the default email client and attaching an attachment to a mail, but this is not editable and there's no send button on the default email client because this line of code just generating an .eml file and opening it. I'm thinking of parsing the eml file but still I don't know how to open the default email client progmatically in a new message form. Here's the photo

enter image description here

You guys have any idea how to make this possible? Thanks!

Rich
  • 3,928
  • 4
  • 37
  • 66
  • Have you tried the MAPI approach using a new ThreadStart with ApartmentState.STA? That was the only way I could get this to work for me. – Parrish Husband Oct 16 '17 at 02:49
  • Did you find a solution yet? Am stuck at exactly the same point as you outlined here... – Ruehri May 10 '18 at 20:49

1 Answers1

0

I am afraid that this will not be possible to do using some generic method for any mail client. But you can easily create your own solution using System.Net.Mail.SmtpClient and some simple custom UI.

Ondřej
  • 1,645
  • 1
  • 18
  • 29
  • If I do a custom UI, then I need to work with the `authkey` to be able to log in their personal email? – Rich Jul 19 '17 at 11:34
  • If you want to only send mail messages, then you only need to know credentials for SMTP server. But remember that anything you send using SmtpClient WILL NOT be in their mailbox (it is send by something else than their mail client). – Ondřej Jul 19 '17 at 11:57
  • I want the users manually send the message, I just want to open their mail client with the attachment being selected. Actually most of the people's default email client is `Mail` in Windows 10, because most of them don't install other mail client, so technically I want to be specific to that app how to open a new message with the attachment. If I make a custom mail client, I think I will need to work on `authkey` in Google, Yahoo, etc. to be able to send an email using their accounts. What do you think? – Rich Jul 20 '17 at 01:58
  • You will not need any key to send mail message from your application. You will only need username and password for SMTP server (if authentication is enabled on it). As I said, sent messages will not appear in their outbox folder of their mail client. – Ondřej Jul 20 '17 at 06:17