Results 1 to 25 of 25

Thread: Outlook HTML email problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Outlook HTML email problem

    I'm having trouble attaching n embedded email in an html outlook email this is what I tried:

    Code:
            Dim Outl As Object
            Outl = CreateObject("Outlook.Application")
            Me.Cursor = Cursors.WaitCursor
            If Outl IsNot Nothing Then
                Dim omsg As Object = Outl.CreateItem(0) '=Outlook.OlItemType.olMailItem'
                With omsg
                    .To = "myemail@domain.com"
                    .Subject = "[" & storeID & "] " & Subject.Text & " - Ticket#: " & Ticket.Text
                    .BodyFormat = Outlook.OlBodyFormat.olFormatHTML
                    Dim emailbody As New System.Text.StringBuilder
                    emailbody.Append("<body><span style=" & Chr(34) & "font-family: Calibri; font-size: 15;" & Chr(34) & ">Hello EK3,<br><br>")
                    emailbody.Append("<b>Description:</b><br><br>")
                    emailbody.Append(StrConv(Details.Text.Replace(vbLf, "<br />"), vbProperCase) & "<br><br>")
                    emailbody.Append("<b>Contact:</b>" & StrConv(RestContact.Text, vbProperCase) & "<br>")
                    emailbody.Append("<b>Ticket#:</b>" & Ticket.Text & "<br>")
                    emailbody.Append("<b>Store#:</b>" & storeID & "<br>")
                    emailbody.Append("<br>Thank you,<br><br>")
                    emailbody.Append("<b>" & StrConv(Agent.Text, vbProperCase) & "</b><br><br>")
                    emailbody.Append("<br>Helpdesk<br><br>")
                    Dim picture As LinkedResource
                    picture = New LinkedResource(templates & "\Logo.jpg", "image/jpeg")
                    picture.ContentId = "cid:01234@domain.com" ' a unique ID
                    emailbody.Append("<img src='cid:01234@domain.com>")
                    emailbody.Append("<span style=" & Chr(34) & "font-family: Arial Narrow; font-size: 14;" & Chr(34) & ">Company Inc. <b>|</b> 123 street <b>|</b> 123.456.1111</span></body>")
                    .HTMLBody = emailbody.ToString
                    .Resources.Add(picture)
    Unfortunately "LinkedResource" is not being found it allows me to convert to System.Net.Mail but the second line still doesn't work.

    I was trying this:

    http://aspalliance.com/1354_Sending_...Image_in_NET.8

    Email works fine, but when I added the above code it errors:

    Code:
                    Dim picture As LinkedResource
                    picture = New LinkedResource(templates & "\Logo.jpg", "image/jpeg")
                    picture.ContentId = "cid:01234@domain.com" ' a unique ID
                    emailbody.Append("<img src='cid:01234@domain.com>")
    Last edited by a_ahmed; Jun 29th, 2014 at 12:15 AM.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Outlook HTML email problem

    Hello,

    This is what I have found to work

    Code:
    Imports System.Net.Mail
    Public Class Form1
        Private Sub DemoSendMessage(ByVal Address As String, ByVal Host As String)
            Dim KateImage As String = IO.Path.Combine(Application.StartupPath, "Kate.jpg")
            '
            ' Simulate data returned from a database.
            '
            Dim dt As DataTable = New DataTable("Services")
            dt.Columns.Add("Identifier", GetType(Integer))
            dt.Columns.Add("FirstName", GetType(String))
            dt.Columns.Add("LastName", GetType(String))
    
    
            dt.Columns("Identifier").AutoIncrement = True
            dt.Columns("Identifier").AllowDBNull = False
            dt.Columns("Identifier").ReadOnly = True
            dt.Columns("Identifier").AutoIncrementSeed = 1
    
            dt.Rows.Add(New Object() {Nothing, "Kevin", "Gallagher"})
            dt.Rows.Add(New Object() {Nothing, "Mary", "Jones"})
            dt.Rows.Add(New Object() {Nothing, "Bill", "Smith"})
            dt.Rows.Add(New Object() {Nothing, "Karen", "Starr"})
    
            Dim MessageBody = _
                    <html>
                        <style>
                        TD {background-color: green;color: #F0F8FF;padding-right:15px;}
                        .THeader {background-color: Yellow;color: Black;}
                        IMG {margin-top:15px;}
                        </style>
                        <body>
                            <p>This is how it is done.</p>
                            <table border="0">
                                <tr>
                                    <td class='THeader'>ID</td><td class='THeader'>First</td><td class='THeader'>Last</td>
                                </tr>
                                <%= From T In dt.AsEnumerable _
                                    Select _
                                    <tr>
                                        <td width="80px">
                                            <%= T.Field(Of Integer)("Identifier") %>
                                        </td>
                                        <td width="45px">
                                            <%= T.Field(Of String)("FirstName") %>
                                        </td>
                                        <td width="30px">
                                            <%= T.Field(Of String)("LastName") %>
                                        </td>
                                    </tr> %>
                            </table>
                            <img src='cid:Woman'/>
                        </body>
                    </html>
    
            Dim mail As New MailMessage()
            mail.From = New MailAddress("TODO")
            mail.To.Add("TODO")
            mail.Subject = "This is an email"
    
            Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString("Sorry a text message does not do justice to the html version", Nothing, "text/plain")
            Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(MessageBody.ToString, Nothing, "text/html")
    
            If IO.File.Exists(KateImage) Then
                Dim logo As New LinkedResource(KateImage, "image/jpeg")
                logo.ContentId = "Woman"
                htmlView.LinkedResources.Add(logo)
            Else
                MessageBox.Show("Skipping Kate image as it was not located.")
            End If
    
            mail.AlternateViews.Add(plainView)
            mail.AlternateViews.Add(htmlView)
    
            Using smtp As New SmtpClient(Host)
                smtp.Send(mail)
            End Using
    
            MessageBox.Show("Message sent")
    
        End Sub
    End Class

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    But this is using SMPT and Imports System.Net.Mail no outlook

    In the above example you have to spcify smpt server and username/password (can't get current user credentials?)
    Last edited by a_ahmed; Jun 29th, 2014 at 02:01 PM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    Also our exchange is hosted in the cloud with office365 so Outlook.office365.com

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    Here;s the problem I keep running into with attachments:

    Additional information: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)).
    I tried adding import for outlook, for office, etc... it keeps erroring.

    was trying this example now:
    Code:
                Dim oApp As Interop.Outlook._Application
                Dim sFilename As String = templates & "\Logo.jpg"
                Dim sbody As String = "test23"
                oApp = New Interop.Outlook.Application
    
                Dim oMsg As Interop.Outlook._MailItem
                oMsg = oApp.CreateItem(Interop.Outlook.OlItemType.olMailItem)
    
                oMsg.Subject = "subject test"
                oMsg.Body = sBody
    
                oMsg.To = "myname@domain.com"
                'oMsg.CC = sCC
    
    
                Dim strS As String = sFilename
                Dim strN As String = "myname"
                If sFilename <> "" Then
                    Dim sBodyLen As Integer = Int(sBody.Length)
                    Dim oAttachs As Interop.Outlook.Attachments = oMsg.Attachments
                    Dim oAttach As Interop.Outlook.Attachment
    
                    oAttach = oAttachs.Add(strS, , sBodyLen, strN)
    
                End If
    
                oMsg.Display()
    Any example I use I get that error.

    I have outlook 2010 installed linked to outlook.office365

    I can send mail but not add attachments.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    I even downloaded this example and I get that same error http://code.msdn.microsoft.com/windo...EMAIL-4956ed06

    An unhandled exception of type 'System.InvalidCastException' occurred in OUTLOOK.exe

    Additional information: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)).

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    Still trying to sort this issue out, it's not a synax issues as much as it's a library/com/whatever issue it's weird.

    http://www.fieldstonsoftware.com/sup...cit_2013.shtml

    I have the correct version numbers outlook 2010 installed so this is being weird.

    It's win7 64bit

    Office 2010 premium plus installed

    And lync 2013 is installed too

    Both outlook exchange and lync are hosted through the cloud with office365

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Outlook HTML email problem

    why must you use current users password etc? Seems hackerish to me.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    https://support.netdocuments.com/ent...EMS-in-Outlook

    https://efiler.co.uk/tag/unable-to-c...-_application/

    Except I'm already set to 9.4

    Looks like this is a known issue with upgrades, maybe it's a conflict from some lync 2013 update?

  10. #10
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Outlook HTML email problem

    what happens when you change
    Code:
    oAttach = oAttachs.Add(strS, , sBodyLen, strN)
    to
    Code:
    oAttach = oAttachs.Add(strS, , 1, strN)

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    Quote Originally Posted by ident View Post
    why must you use current users password etc? Seems hackerish to me.
    I don't want to supply a user/pass to begin with.

    It's a dashboard for the helpdesk and it's used to automate certain redundant tasks such as in this case templates to certain vendors

    I don't want to have the need to supply any form of user/pass whether hardcoded within the program or prompting the user. It's a domain with exchange (albeit in the cloud). They are already logged in within the domain and outlook is setup with their credentials.

  12. #12
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Outlook HTML email problem

    no One said that. Use your own. In actual fact why you bothering with Email?

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    Quote Originally Posted by ident View Post
    no One said that. Use your own. In actual fact why you bothering with Email?
    Why? Because that's the process used to contact the vendor (this particular item of the dashboard).

    I understand I could supply my own and have everyone send on behalf of using that account. But I don't want to store such a thing in the program either.

    I didn't paste the whole code for privacy, but I also have a checkbox to preview or not to preview. It allows them to not send immediately but rather see the email prepped so if they want to make some change they can send manually but window comes all ready to go. Unchecked, and it just sends via Outlook with their profile.

    All they see is a template with drop downs, textboxes, etc.. and certain information that is autopopulated from AD, other resources from the program, and certain radio button selections which auto-select certain things, etc... Right now contacting the vendor means copy pasting a text template and editing each time (lame).

    This template generator I built does everything in one-two clicks.

  14. #14
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Outlook HTML email problem

    Do you not agree forcing a email is a tad suspect? How is it private not pasting the code? Unless you don't want us to see what you are doing.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    I knew it.. it was our stupid Lync 2013 updates that get pushed. Because it's tied to Outlook and Part of Office 2013 version 9.5, i had a key "9.5" but with nothing in it.

    So I deleted 9.5 key and left the 9.4 key in the registry, lo and behold code works. I mean none of the code snippets worked at all, was not a coding issue, rather... office/system update issue conflict between 2010 and 2013 components (with office 2010 and lync 2013 installed).

    We had a similar issue with one software package, this is how I figured it was related, because with each Lync 2013 update being pushed, this one particular software would fail to work due to the update and we'd have to push a registry fix to delete the keys related to 9.5

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    Quote Originally Posted by ident View Post
    Do you not agree forcing a email is a tad suspect? How is it private not pasting the code? Unless you don't want us to see what you are doing.
    What do you mean FORCING an email? It's a helpdesk they have their own outlook profiles. I don't want an exchange username/password encoded in the program. They don't NEED to enter a username/password, they have outlook, it's an exchange environment. Every email sent is to be trailed by their email and date stamp, I don't want to do 'send on behalf' using an encoded user/pass or what have you.

    What do you not get? It's a template generator that gets filled up by the helpdesk agent and gets sent to the vendor with either a send now or preview first (opens outlook message) before sending.

    Instead of them manually filing in the information it is prepopulated with data sources or their radio button selections (which further populate dropdowns) out of which I generate an html styled body from all the information that gets sent to a vendor (through outlook).

    I pasted a part of the code, not the whole program duh. Only issue I was running into was with image attachments and attachments in general.

    Even a code snippet (that I linked to few posts above) did not work because of the office interop library issue
    Last edited by a_ahmed; Jun 29th, 2014 at 06:07 PM.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    For anyone else who runs into this issue the problem is office library conflicts so ensure you only have the version you are using and delete extra unrelated keys:


    HKEY_CLASSES_ROOT\Type_Library\{00062FFF-0000-0000-C000-000000000046}

    Root string default value should be the version of office you have installed so for example registry string "Version" should be 9.4 for Office 2010

    Likewise any sub-keys it should be 9.4 only, in my case I had 9.5 due to lync 2013, this was causing a conflict. Any older versions should be deleted too (2k7, 2k3, etc...)

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    Got it working exactly as I want using this example:

    http://www.mrexcel.com/forum/excel-q...l-4-print.html

    So basically attach, then use the filename.jpg cid:filename.jpg

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    Confirmed with a co-worker who happened to be in the office that it worked. Came with image embedded and not as a visible attachment.

    Haven't tried multiple attachments, will do that next but so far so good. This worked fantastic.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    @kevininstructor I am interested in how you used Dim Messagebody with html.

    Your example is very cool how you constructed html on the go. I am not sure how to do that. I know HTML, but I mean the way you had HTML in vb code view. In my example I used a string builder, which is convenient too but your example would be simpler to view in plain sight as plain html right in the vb code view.

    Mind explaining to me the meaning behind:

    Code:
    %= From T In dt.AsEnumerable _
                                    Select _
    <%= T.Field(Of String)("LastName") %>
    I want to know how I can populate using my text labels and textboxes/comboboxes.

    Basically the titles are taken from labels so labelname.text and the information from textboxname.text and likewise with comboboxname.text

    The comboboxes I set their indexvalue based on selection of radiobuttons and when the template window opens.

  21. #21
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Outlook HTML email problem

    Quote Originally Posted by a_ahmed View Post
    For anyone else who runs into this issue the problem is office library conflicts so ensure you only have the version you are using and delete extra unrelated keys:


    HKEY_CLASSES_ROOT\Type_Library\{00062FFF-0000-0000-C000-000000000046}

    Root string default value should be the version of office you have installed so for example registry string "Version" should be 9.4 for Office 2010

    Likewise any sub-keys it should be 9.4 only, in my case I had 9.5 due to lync 2013, this was causing a conflict. Any older versions should be deleted too (2k7, 2k3, etc...)
    Hello,

    Messing with the registry is in my mind a 'work-around' as you should never need to alter the system registry for this purpose unless you have zero chances to do the task w/o doing so. I would invite you to consider the following Walkthrough: Embedding Type Information from Microsoft Office Assemblies (C# and Visual Basic)

  22. #22
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Outlook HTML email problem

    Quote Originally Posted by a_ahmed View Post
    But this is using SMPT and Imports System.Net.Mail no outlook

    In the above example you have to spcify smpt server and username/password (can't get current user credentials?)
    Does it matter that smtp is a valid method over OutLook? Also there are freeware web apps that can be used to do this as a help desk utility that will do what you want with very little configuration.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    Quote Originally Posted by kevininstructor View Post
    Does it matter that smtp is a valid method over OutLook? Also there are freeware web apps that can be used to do this as a help desk utility that will do what you want with very little configuration.
    I've already made it. We don't need a hundred more utilities, the whole point of what I'm doing is to automate and minimize/make more efficient the current batch of tasks the helpdesk performs in a single point of contact solution; this dashboard.

    I'm pretty much done with it and it saves a heck of a lot of time.

    Also the registry issue is a known issue with microsoft products. Different versions being mixed together come into conflict with the library and com objects. The fix is, the registry fix. Reason it's happening is office 2010 + lync 2013 on the same systems. The presence of the 9.5 key is confusing the referencing, it thinks there is office 2013 on the system (outlook, etc...) but there isn't hence 'not registered' error. Wiping out the key lets it chose properly the 9.4 key and 2010 libraries.

    The issue has come up with another program before, and it just so happened to come up for me while developing this using one of the microsoft interop references.
    Last edited by a_ahmed; Jul 1st, 2014 at 01:18 PM.

  24. #24
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Outlook HTML email problem

    Quote Originally Posted by a_ahmed View Post
    I've already made it. We don't need a hundred more utilities, the whole point of what I'm doing is to automate and minimize/make more efficient the current batch of tasks the helpdesk performs in a single point of contact solution; this dashboard.

    I'm pretty much done with it and it saves a heck of a lot of time.

    Also the registry issue is a known issue with microsoft products. Different versions being mixed together come into conflict with the library and com objects. The fix is, the registry fix. Reason it's happening is office 2010 + lync 2013 on the same systems. The presence of the 9.5 key is confusing the referencing, it thinks there is office 2013 on the system (outlook, etc...) but there isn't hence 'not registered' error. Wiping out the key lets it chose properly the 9.4 key and 2010 libraries.

    The issue has come up with another program before, and it just so happened to come up for me while developing this using one of the microsoft interop references.
    Did you look at the link I provided?

    In regards to not wanting many more utilities, my advice was not to add to the current list of applications/utilities but instead to look at a all encompassing solution. We did this, started with a application called RT, used it for several years and now migrating to a Microsoft solution. The benefits of using such a product is when many organizations use it you get things that you might not have considered and thus helping many times more with such a product. If you look at the cost for this and the cost of developing your own usually a purchase is more cost efficient.

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: Outlook HTML email problem

    The department won't spend more money, this was my own voluntary contribution to make things more efficacious. The things being done are very specific to our environment (retail -- thousands of stores) and redundant.

    We already have a ticketing system and a plethora of other things. This is just to automate specific redundant tasks from one central point for helpesk agents.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width