Send an e-mail from VB .NET with default email program

Hello,

First of all, excuse-me for my English level. It's not my first language.

I want to send an e-mail from VB. NET with user default email program (essencially could be Outlook Express or Outlook).

- The URL method mailto is not a good method because the maximum url lenght is 1024 characters. With a lot of recipients or a lot of attachments, I will have problems.
- The MAPI method seams to be interesting but I need the message window from the user default email program to be opened before sending the email to allow the user to modify the e-mail, use the language corrector, look for more recipients in the address book, ...

Any idea?
A piece of code (vb .net) would be apreciated. I need also an explanation of how to find and include the references needed by any solution given.

Thanks!
lsasplugasAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Javert93Connect With a Mentor Commented:
Also, MSDN contains documentation regarding the MAPISendMail function and its use.
0
 
DotNetLover_BaanCommented:
Why don't you use Javascript ? It will open Outlook express(Default mail application) at the client side.

        Dim strScript As String
        strScript = "<script language=JavaScript>document.location='mailto:address@domain.com'</script>"
        Page.RegisterClientScriptBlock("clientScript", strScript)
Put it inside a Button_Click event.

-Baan
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
armoghanCommented:
If you need to run it using the default Mail client attached, you should use this line only

It can solve your problem of default program
 System.Diagnostics.Process.Start("mailto:address@domain.com")
0
 
DotNetLover_BaanCommented:
Is it a windows application or web ???
0
 
lsasplugasAuthor Commented:
Comment for DotNetLover Bean:
Your method true javascript has a problem: it use the "mailto" method and I'vs alreay say that the mailTo url is limited at 1024 characters. I have to add attachments (as many as the user wants) and may be a lot of recipients (as many as the user wants)

Comment for Scoobyed:
The SMTP method don't open the default mail program. Read again my question an you will see that your answer don't match with my needs.

Comment for armoghan:
Same comment given to DotNetLover Bean

2nd Comment for DotNetLover Bean:
It's a windows application.

Comment for all:
Please, note that what I need is to open the default mail program and show the mail window without sending the mail and by this way, allow the user to use his own mail program tools (adress book, language corrector, ...). When the user has finish the mail edition, he must send the mail by his own.

Thanks
0
 
armoghanCommented:
It means that you want to control the default mailing progam and there can be many of them each having the different behavious..
If the problem is up to One Mailing program then it could be done.. Like the solution presented for Outlook, But I dont think that you can attach files programmatically to any default program,

Sol 1. Use mailto and let user attach his/her/ own files and you can give subject to it only
Sol 2. Stick yourself to one or two mailing programs like outlook etc
0
 
ScoobyedCommented:
the MAPI controls do display the mail dialaogue if required (as you say in you question 'to allow the user to modify the e-mail, use the language corrector, look for more recipients in the address book, ...') this is done by AxMAPIMessages1.Send(True) - the true is the show dialog bit

this works as expected (displaying the send dialog) for me, I have outlook 2000 already set up to use our corportate exchange server, and it just opens a sned dialogue as if I'd clicked compose email

            Dim lSession As Integer
            Dim sEmailAddress As String

            'sEmailAddress = "smtp:" & "a@b.com"
            sEmailAddress = "a@b.com"

            Try
                  ' close session
                  With Me.AxMAPISession1
                        .SignOff()
                  End With
            Catch ex As Exception
                  ' session may already have been opened
            End Try

            ' open session
            With Me.AxMAPISession1
                  '.NewSession = True
                  .SignOn()
                  lSession = .SessionID
            End With

            ' message
            With Me.AxMAPIMessages1
                  .SessionID = lSession
                  .Compose()

                  .RecipAddress = sEmailAddress
                  .RecipDisplayName = sEmailAddress
                  .ResolveName()
                  .RecipIndex = 0
                  .RecipType = CType(MSMAPI.RecipTypeConstants.mapToList, Short)
                  .MsgReceiptRequested = True

                  .MsgSubject = "test subject"
                  .MsgNoteText = "test body text"

                  .AttachmentIndex = 0
                  .AttachmentPosition = 0
                  .AttachmentPathName = "c:\autoexec.bat"

                  .Send(True)
            End With
0
 
ScoobyedCommented:
apparently, this also works to do same without interop to mapi controls
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21121131.html
0
 
Javert93Commented:
This function should lainch the default mail application:

    Public Function OpenDefaultMailApp() As Boolean
        Try
            ' First, get the name of the default mail application
            Dim mailAppsKey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\Clients\Mail")
            Dim defaultAppName As String = Convert.ToString(mailAppsKey.GetValue(""))
            ' If the value is empty, there is no default mail app
            If defaultAppName = "" Then Return False

            ' Now get the command line for the default mail application
            Dim subKeyPath As String = String.Format("{0}\shell\open\command", defaultAppName)
            Dim defaultMailCmdKey As RegistryKey = mailAppsKey.OpenSubKey(subKeyPath)
            Dim commandLine As String = Convert.ToString(defaultMailCmdKey.GetValue(""))
            ' This should never be empty, but if it is, we need to check for it
            If commandLine = "" Then Return False

            ' Now that we have the command line, we execute it using Shell().
            ' NOTE: Normally, the preferred method is to use Process.Start() to
            '      launch an application, but the command line may contain arguments,
            '      which Start() can only use if they are in a sepereate string.
            Shell(commandLine, AppWinStyle.NormalFocus)
            Return True
        Catch ex As Exception
        End Try

        Return False
    End Function
0
 
lsasplugasAuthor Commented:
Comment for Scoobyed:

That looks very interesting! I tried MAPI solution but i didn't realise that I could use the "show dialog bit" (parameter "true" in the sentence  .Send(True)).

I will try your solution later and I will post my feedback.


Comment for Javert93:

Your solution don't match exactly with my needs. I need to open the default email program but also the e-mail window and adding programatically some attachments, message bodiy, recipients, ... However, I must say I found your solution very interesting.


0
 
lsasplugasAuthor Commented:
Scoobyed,

Your solution don't explain wich reference I have to include in my project for being able to use AxMAPISession and AxMAPIMessage objects. Once I will find the include, will I have any license restriction for distribute my solution?

Thanks!

0
 
lsasplugasAuthor Commented:
Hello all,

I supose I need msmapi.ocx library for being able to use mapiSession and mapiMessage objects.

It is not available in my computer. What can I do?

lsasplugas
0
 
Javert93Commented:
The only mail program that I know of that allows you to programmatically control the mail message is sent is Outlook (through it's Automation model). I think all other programs are restricted to using the mailto shell command. However, if you are willing to create your own UI, you can use the SmtpMail, MailMessage, and MailAttachment classes in the System.Web.Mail namespace. Through these classes you can:

    1) Attach files
    2) Control the message encoding
    3) Control the message format (e.g. text vs. html)
    4) Use To, Cc, and Bcc
    5) Control the message priority flags

However, the only drawback is that you need an SMTP server to send the message (which does not necessarily have to be the local machine), and it does not support using an SMTP server that requires authentication (UserName/Password). However, for the first issue, I believe MAPI stores the user's current SMTP server in the registry. Also, note that you must reference System.Web.dll to use these classes.
0
 
lsasplugasAuthor Commented:
Hello Javert93,

It seems that it's possible with "MAPI" protocol.

With system.web namespace, I'm not able to use the default e-mail program tools.

lsasplugas
0
 
Javert93Commented:
The MAPI control is part of the previous version of visual studo. So unless you have Visual FoxPro 6.0, or Visual Basic 6.0, you won't have access to it. However, if you want to use MAPI, you can use the Simple MAPI Win32 APIs (which is what the control uses anyway), and write your own wrapper class in VB.NET.
0
 
lsasplugasAuthor Commented:
Javert93,

Ok, Simple MAPI may match with my needs:
 - my application is Visual Basic .Net aplication
 - moreover, simple MAPI methods seem to be enaught for multiple recipients and multiple attachments
 - I just have to try If it's possible to display the message before sending it (by the solution ofered by scoobyed)

But I can't find the way to use the Sinmple Mapi control in my code. I need to include .COM or .NET reference but wich one? May be the CDO library?

Lazlo
0
 
Javert93Commented:
It's not a control, it's a set of API's exported by MAPI32.DLL. That means you need to use Declare statements, not library references. You can get the MAPIVB32 module at http://support.microsoft.com/default.aspx?scid=kb;en-us;163216. Note that this is using the old VB 6 syntax, so you will have to massage the declarations a bit (e.g. changing Global to Public). Also, know that there may be an issue using MAPI (the control or the API's) with Outlook Express (http://support.microsoft.com/default.aspx?scid=kb;EN-US;315458).
0
 
ScoobyedCommented:
not tried, but a separate mapi download / install may be possible
http://www.zerog.com/downloads_02d.shtml
http://goldengate.zerog.com/releases/ianetmm/msmapi32.msm
0
 
ahmadfuwadCommented:
'Add system.web in refferances
 Dim mail As New System.Web.Mail.MailMessage

        With mail
            .From = "ahmadwad@yahoo.com"
            .To = "ahmadwad@yahoo.com"
            .Subject = "this is the subject"
            .Body = "<hr><code>testing a mail using html format<br>it works great :D</code><hr>"
            .BodyFormat = Web.Mail.MailFormat.Text
        End With
        Dim smtp As Web.Mail.SmtpMail
              smtp.SmtpServer = "192.168.1.252"
        smtp.Send(mail)
        MessageBox.Show("done")
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.