Solved

How to send email using vb.net?

Posted on 2014-12-02
8
261 Views
Last Modified: 2014-12-10
Hi,

its been a pain trying to get my application to compose an email, but i finally managed to get it working but I have a couple questions:-
1) It seems that there is some sort of character limit on the following method, if the text in the body of the email contains a lot of information then it cuts it off at the bottom?
2) I have seen examples of some people being able to change the font type, size and colour of the text - how would I use this with the following coding?


       Dim sParams As String = ""
        Dim emailto As String = "test@test.co.uk"
        Dim subject As String = "test"
        Dim body As String = ""
        Dim LineCounter As Integer = 0
        Dim Lines As String = ""
    
        body = body & "The"
        body = body & "%0A"
        body = body & "%0A" & "Body"
        body = body & "%0A"
        body = body & "%0A" & "Of"
        body = body & "%0A"
        body = body & "%0A" & "The"
        body = body & "%0A"
        body = body & "%0A" & "Email"
        body = body & "%0A"
        body = body & "%0A" & "Goes"
        body = body & "%0A"
        body = body & "%0A" & "Here"

        sParams = "mailto:" & emailto & "?subject=" & subject & "&body=" & body
        Try
            System.Diagnostics.Process.Start(sParams)
        Catch e2 As Exception
            MessageBox.Show("Unable to load email!", "Problem Sending the email", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Open in new window


I know there are other methods like smtp but i am happy with the above method as it composes emails in outlook which is perfect, i dont require it to send emails just compose.
0
Comment
Question by:Legolas786
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40476354
The method you are using is analogous to using a link on a webpage to open up the email software on a user's PC and prepopulating the email with various pieces of information. Using this method, you can only use text for the components (from, to, body), which prevents defining fonts, colors and styles, and has the limitations of what can go in a web browser's address bar, which I believe is 2K of text, at least in Internet Explorer.

If you want more features, you will either need to write your code to use Outlook's APIs or use .NET's System.Net.Mail classes.
0
 

Author Comment

by:Legolas786
ID: 40476934
hi, thanks for your response, are you able to provide me a link to some code or a demo program that will set me on the right track? thanks.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40477114
For using the System.Net.Mail classes, here is an example.

For using the Outlook APIs, here is an example.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Legolas786
ID: 40478197
Ok, so after some research I tried to use the following coding

Dim OutlookMessage As Outlook.MailItem
        Dim AppOutlook As New Outlook.Application

        Dim objNS As Outlook._NameSpace = AppOutlook.Session
        Dim objFolder As Outlook.MAPIFolder
        objFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts)

        Try
            OutlookMessage = AppOutlook.CreateItem(Outlook.OlItemType.olMailItem)
            Dim Recipents As Outlook.Recipients = OutlookMessage.Recipients
            Recipents.Add("myemail@hotmail.com")
            OutlookMessage.Subject = "Sending through Outlook"
            OutlookMessage.Body = "Testing outlook Mail"
            OutlookMessage.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
            OutlookMessage.Save()

            OutlookMessage.Move(objFolder)
        Catch ex As System.Exception
            MessageBox.Show("Mail could not be sent") 'if you dont want this message, simply delete this line    
        Finally
            OutlookMessage = Nothing
            AppOutlook = Nothing
        End Try

Open in new window


However I get an error of

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Open in new window

On the following line

OutlookMessage = AppOutlook.CreateItem(Outlook.OlItemType.olMailItem)

Open in new window


I could not find much help online with this error, so I thought of another way.  Maybe I save a template from outlook of an email (.msg) and then I could use my program to open this .msg in outlook.  So I tried to use the following coding

Dim oApp As New outlook.Application
        Dim mitem As Outlook.MailItem
        mitem = oApp.CreateItemFromTemplate("C:\test\OutlookTest.msg")
        mitem.Display()

Open in new window


But I get the same error which is driving me mad on line

mitem = oApp.CreateItemFromTemplate("C:\test\OutlookTest.msg")

Open in new window


Would this indicate to be a problem with outlook?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40478519
It does not necessarily indicate a problem with Outlook. From the code you first posted, you may need to explicitly cast the return object from AppOutlook.CreateItem to an Outlook.MailItem using CType, as shown in the link originally provided.

It also may be caused by the differences in the version of Outlook you are using as compared to the example provided. There is a comment at this link (http://support.microsoft.com/kb/313787) that Outlook 10 and Outlook 11 are referenced differently when used in .Net code.
0
 

Accepted Solution

by:
Legolas786 earned 0 total points
ID: 40478597
hi, I was able to reinstall office and using the following method was able to compose a new email using vb.net but using this method how would I add the to, cc, subject and body fields?  

Dim Outl As Object  
        Outl = CreateObject("Outlook.Application")  
        If Outl IsNot Nothing Then  
            Dim omsg As Object  
            omsg = Outl.CreateItem(0) '=Outlook.OlItemType.olMailItem'  
            omsg = Outl.CreateItem(0) '=Outlook.  
            omsg.Display(True) 'will display message to user  
        End If  

Open in new window

0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40478877
You can use this page for reference to all of the properties of a MailItem: http://msdn.microsoft.com/en-us/library/office/ff861332(v=office.15).aspx
0
 

Author Closing Comment

by:Legolas786
ID: 40490872
Found sollution myself.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 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