Solved

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

Posted on 2004-09-06
23
7,844 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:lsasplugas
  • 6
  • 5
  • 4
  • +3
23 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11993584
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
 
LVL 3

Expert Comment

by:Scoobyed
ID: 11994792
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11996853
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
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11996978
Is it a windows application or web ???
0
 

Author Comment

by:lsasplugas
ID: 11999942
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
 
LVL 18

Expert Comment

by:armoghan
ID: 12003071
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
 
LVL 3

Expert Comment

by:Scoobyed
ID: 12004434
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
 
LVL 3

Expert Comment

by:Scoobyed
ID: 12004462
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
 
LVL 4

Expert Comment

by:Javert93
ID: 12009600
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
 

Author Comment

by:lsasplugas
ID: 12016946
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:lsasplugas
ID: 12017828
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
 

Author Comment

by:lsasplugas
ID: 12018070
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
 
LVL 4

Expert Comment

by:Javert93
ID: 12018961
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
 

Author Comment

by:lsasplugas
ID: 12019382
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
 
LVL 4

Expert Comment

by:Javert93
ID: 12019702
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
 

Author Comment

by:lsasplugas
ID: 12019822
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
 
LVL 4

Expert Comment

by:Javert93
ID: 12019922
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
 
LVL 4

Accepted Solution

by:
Javert93 earned 500 total points
ID: 12019932
Also, MSDN contains documentation regarding the MAPISendMail function and its use.
0
 
LVL 3

Expert Comment

by:Scoobyed
ID: 12024402
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
 
LVL 2

Expert Comment

by:ahmadfuwad
ID: 12082817
'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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Starting to learn JAVA, 7 47
Merging PDFs with VB.NET 10 31
Paging GridView 7 32
Open form in the top right hand corner of screen 5 19
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now