none
Create outlook message from VB.Net

    Question

  • I would like to create an Outlook message with most of the text filled and the user just has to click send.  I rather have the user click Send than for the program to send the message automatically.  The below code doesn't work.  What modifications does it need? 


    Dim
    objSmtpMail As System.Net.Mail.SmtpClient

    Dim Attachment As System.Net.Mail.Attachment

    Dim Mailmsg As New System.Net.Mail.MailMessage

    Dim mM As New MailMessage

    mM.From = New MailAddress("a@b.com")

    mM.To.Add("a@b.com")

    mM.Subject = "Subject"

    mM.Body = "Body"

    mM.IsBodyHtml = False

    mM.Priority = MailPriority.Normal

    Dim sC As New SmtpClient("mail.b.com")

    Dim outlookMsg As MapiMessage = MapiMessage.FromMailMessage(Mailmsg)

    Dim strMsgFile As String = "sample.msg"

    outlookMsg.Save(strMsgFile)

    Tuesday, February 17, 2009 5:27 PM

Answers

  • Try this:

            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 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 

    Don't forget to keep the following line on top:

    Imports outlook = Microsoft.Office.Interop.Outlook 
    Tuesday, February 17, 2009 9:06 PM

  • Meesage can be sent using outlook this way also

    1. Add reference to Microsoft.Office.Interop.Outlook which can be done through, Project Menu, Add Reference

    2. Following line at top 
    Imports outlook = Microsoft.Office.Interop.Outlook

     
    Dim OutlookMessage As outlook.MailItem 
            Dim AppOutlook As New outlook.Application 
            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.Send() 
            Catch ex As 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 
     
     


    Arjun Paudel
    Tuesday, February 17, 2009 7:05 PM

All replies


  • Meesage can be sent using outlook this way also

    1. Add reference to Microsoft.Office.Interop.Outlook which can be done through, Project Menu, Add Reference

    2. Following line at top 
    Imports outlook = Microsoft.Office.Interop.Outlook

     
    Dim OutlookMessage As outlook.MailItem 
            Dim AppOutlook As New outlook.Application 
            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.Send() 
            Catch ex As 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 
     
     


    Arjun Paudel
    Tuesday, February 17, 2009 7:05 PM
  • Thanks for the code.

    This was trying to send a message.  I would like for it open a new mail message inside of outlook with the data prefilled in.

    I found changing "OutlookMessage.Send() " to

    OutlookMessage.SaveAs("c:\temp\TestMessage.msg")

    Dim StartInfo As New ProcessStartInfo("Outlook.exe")

    StartInfo.FileName = "c:\temp\TestMessage.msg"

    Process.Start(StartInfo)

    gets me a little closer to what I want but it gives an error about trying to access Outlook.

    Tuesday, February 17, 2009 8:18 PM
  • Try this:

            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 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 

    Don't forget to keep the following line on top:

    Imports outlook = Microsoft.Office.Interop.Outlook 
    Tuesday, February 17, 2009 9:06 PM
  • Thank you Arjun and Junner for your friendly help.

    Hi chazparks,

    Besides Outlook.Application, we usually use System.Net.Mail namespace to send email in VB.NET. 

    Imports System.Net.Mail  
    Public Class Form1  
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            Dim mailInstance As MailMessage = New MailMessage("FromMailAdress""ToMailAdress""Subject""Body")  
            mailInstance.Attachments.Add(New Attachment("filename")) 'Optional  
            Dim mailSenderInstance As SmtpClient = New SmtpClient("smtpHostAdress", 25) '25 is the port of the SMTP host  
            mailSenderInstance.Credentials = New System.Net.NetworkCredential("LoginAccout""Password")  
            mailSenderInstance.Send(mailInstance)  
            mailInstance.Dispose() 'Please remember to dispose this object  
        End Sub 
     
    End Class 


    Please check this thread for detailed code samples:
    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/1a47815d-2b7e-4002-8368-50d5449170f6/


    Best regards,
    Martin Xie



     

    Tuesday, February 24, 2009 9:30 AM