Search code examples
asp.netoutlook-2016

Send email with Outlook 2016 from ASP.NET


I need to send emails but preview them first before sending. I am using Visual Studio 2019 and my Outlook version is 2016 32-bit on a Windows 10 development machine.

I get an error

FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.

Yet I installed the COM libraries via browse to C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Tools.Excel.v9.0\15.0.0.0

I use Outlook version 16 and from what I can tell there is no version 16 of the COM library.

Is there another way to get this working?

private void SendEmail()
{
    try
    {
        Outlook.MailItem mailItem = (Outlook.MailItem)this.CreateItem(Outlook.OlItemType.olMailItem);
        mailItem.Subject = "This is the subject";
        mailItem.To = "[email protected]";
        mailItem.Body = "This is the message.";
        mailItem.Importance = Outlook.OlImportance.olImportanceLow;
        mailItem.Display(false);
    }
    catch (Exception eX)
    {
        throw new Exception("cDocument: Error occurred trying to create an Outlook e-mail"
                            + Environment.NewLine + eX.Message);
    }
}

This is the VB code I used to run Outlook on local machine.

   Public Sub SendEmail(ByVal sTo As String, ByVal sSubject As String, ByVal sBody As String)
        Try
            If Not Process.GetProcessesByName("OUTLOOK").Any() Then
                System.Diagnostics.Process.Start("Outlook.exe")
            End If

            'Dim oApp As Outlook._Application
            'oApp = New Outlook.Application()
            Dim oApp As Object

            ' Create a new MailItem.
            'Dim oMsg As Outlook._MailItem
            Dim oMsg As Object
            Const olMailItem As Long = 0 'à commenter si marche pas
            oApp = CreateObject("Outlook.Application") 'à commenter si marche pas


            'oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
            oMsg = oApp.CreateItem(olMailItem)
            oMsg.Subject = sSubject
            oMsg.Body = sBody

            ' TODO: Replace with a valid e-mail address.
            oMsg.To = sTo

            ' Send
            oMsg.Send()

            ' Clean up
            oMsg = Nothing
            oApp = Nothing

        Catch ex As Exception

            Debug.Print(ex.ToString)
        End Try
    End Sub 

Solution

  • Is this really asp.net and a web based application???? You do realize the VAST difference between windows desktop software and web based, right? You can't use or display or show ANYTHING from a desktop program in a client side web browser. I have to assume you taking about desktop ONLY software here, and not web based? I mean, even if you could use Outlook in code, it would run on the web server, and the user would not see outlook running on that one web server computer box, would they?

    And you certainly can't use and launch Outlook on the end user's client side machine. I mean, it would be really cool then if you come to my web site to view a cat picture, and I can now use YOUR copy of outlook, or Excel. Hey, why not launch your banking application and steal all that information? Or search your local hard drive for a Excel sheet called passwords? If you think about this, if that was possible, then ZERO people would EVER risk using the internet, right?

    so, you have ZERO ability to use or mess with MY computer. You can't even search or look for a file on MY computer. It is my computer, and if just going to some web site could let you mess IN ANY WAY with my computer would be the worlds largest security hole.

    You can certainly put up a nice big text box, even a send to box as a web form or web page. But sending that email will be based on YOUR own email settings and account that you setup on the web site. I mean, you think that YOUR web site is going to use MY email system and email password and information? Really? So I can now use YOUR software on YOUR computer from my web site? You really have to stop and think about what you asking? As noted, if you think about this, then you will well realize that this type of approach to web software is not possible, but it should ALSO be VERY obvious that such operations with web software could not be possible, since if it was, then no one would ever risk or even consider using the internet to do anything.

    Even when you use a up-load file control, you can NEVER EVER even set the file to be picked for up-load - only the end user can do that. Your server side software cannot touch, see, know ANYTHING about the client side computer. Besides, that client computer might be a iPad, a smartphone, or your Apple Mac computer anyway - you have no idea, can't have any idea, and it can't matter.

    so, client side software and files are 100% hands off. The end user has a browser sitting on their desktop. You can ONLY send it some HTML for that browser to then display.

    And trying to use server side software? Well, if you did manage to do that, then on the server you would see outlook pop up on the computer. But the end user? They would not see anything - since you not able to see and use software.

    It would be cool when you go to buy something on amazon, that you web browser lets YOU mess around with software on THEIR web server??

    All you can do with a web system is create a web page and pass it down to the end user, and their browser will THEN render and display the markup. What you run on that web server (which is just a computer) is up to you, but you ONLY have the ability to pump out some HTML down to the users browser - nothing more, nothing less. Any windows program launched on the web server would only be seen if you were sitting at the console on and at the web server.

    so, what would I do for a template? I would toss up a text box, or web form, and SAVE some template say to a database.

    And if you REALLY smart you adopt ckedit, or a screen that allows you to enter markup. So, now you can drop in any graphics or fonts or whatever.

    Say like this:

    enter image description here

    So, what I do with above is "save" my email templates into a database. I then have a simple routine that search/replace the fields like [Email] with the data from the database. So fonts, graphics etc? I can just edit and build and cut + paste in graphics.

    But, I certainly can't really use outlook for this purpose. And the "sending" email address will have to be one for the web site - it can't be any old sending email.