Link to home
Start Free TrialLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

outlook mailto with attachment vb.net 2003

Hi Experts,

Does anyone know an easy method to use maito function with an attachment. I've look everywhere and tried many code. I tried this (below) and it lauched a new message without an attachment,

 Dim Addr As String = "user@address.com"
    Dim Subject As String = "Subject"
    Dim Body As String = "Body"
    Dim Attach As String = "C:\Path\FileName.txt"
    System.Diagnostics.Process.Start("mailto:" & Addr & "?subject=" & Subject & "&body=" & Body & "&attachment=" & Attach

Any ideas,
Roberto
Avatar of mrwad99
mrwad99
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this:

Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
Dim attachment As New MailAttachment(Server.MapPath("test.txt")) 'create the attachment
mail.Attachments.Add(attachment) 'add the attachment
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)
Avatar of RobertoFreemano

ASKER

Hi,
I'm now getting 'MailMessage()'/ 'MailAttachment' and 'SmtpMail.SmtpServer' not defined.. this is the bit that gets me... can you please help?

Roberto
Try this, you can do it without using SMTP, just add the reference: Microsoft Outlook 9.0 Object Library to your project.

Here is a function I use:

Mail_Out("yourname@xxxx.com", "This is a test")

Function Mail_Out(ByVal EMailAddress As String, Optional ByVal EMailSubject As String = "", _
                      Optional ByVal EMailMessage As String = "")
             Dim application As Outlook.Application
            Dim objItem As Outlook.MailItem
            application = CreateObject("Outlook.Application")
            objItem = application.CreateItem(Outlook.OlItemType.olMailItem)
            objItem.Subject = EMailSubject
            objItem.To = EMailAddress
            objiteml.Attachments.Add("c:\test.txt", Outlook.OlAttachmentType.olByValue)
            objItem.Send()
            application = Nothing
            objItem = Nothing
End Function
Hi rkckjk,

So far I've done exactly as you've suggested; however, the only line that is causing trouble is:

objiteml.Attachments.Add("c:\test.txt", Outlook.OlAttachmentType.olByValue)

It states that objiteml is not declared... any ideas?
Roberto
P.s.  I will increase points as I've neglected this solution :)
Sorry, it was typo in my part...

The line should be:

objitem.Attachments.Add("c:\test.txt", Outlook.OlAttachmentType.olByValue)




Hi rkckjk,

I've amended the line and inserted the code on Button1. When Button1 is clicked, nothing appears to happen... should this launch Outlook?

Cheers,
Roberto
Did you add Microsoft Outlook 9.0 Object Library as reference in your application?
I think I added Microsoft Outlook 11 Object to it
Ok , here's my entire code and it worked great:

Module Module1
    Sub Main()
        Mail_Out("yourname@xxxx.com", "This is a test")
    End Sub

    Function Mail_Out(ByVal EMailAddress As String, Optional ByVal EMailSubject As String = "", _
                      Optional ByVal EMailMessage As String = "")
        Dim application As Outlook.Application
        Dim objItem As Outlook.MailItem
        application = CreateObject("Outlook.Application")
        objItem = application.CreateItem(Outlook.OlItemType.olMailItem)
        objItem.Subject = EMailSubject
        objItem.To = EMailAddress
        objItem.Attachments.Add("c:\test.txt", Outlook.OlAttachmentType.olByValue)
        objItem.Send()
        application = Nothing
        objItem = Nothing
    End Function
End Module
Hi rkckjk,

I've created the module in pasted your code... but how do i call the event from module1 to a button1 on a Winform?

Sorry for being a total novice :(

Cheers,
Roberto
ASKER CERTIFIED SOLUTION
Avatar of Richard Kreidl
Richard Kreidl
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent rkckjk,

Many thanks

;)