Solved

outlook mailto with attachment vb.net 2003

Posted on 2007-03-31
13
2,944 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:RobertoFreemano
  • 6
  • 6
13 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 18829614
0
 
LVL 2

Expert Comment

by:rkckjk
ID: 18829980
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)
0
 

Author Comment

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

Roberto
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 2

Expert Comment

by:rkckjk
ID: 18846590
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
0
 

Author Comment

by:RobertoFreemano
ID: 19205307
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 :)
0
 
LVL 2

Expert Comment

by:rkckjk
ID: 19205309
Sorry, it was typo in my part...

The line should be:

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




0
 

Author Comment

by:RobertoFreemano
ID: 19206749
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
0
 
LVL 2

Expert Comment

by:rkckjk
ID: 19207755
Did you add Microsoft Outlook 9.0 Object Library as reference in your application?
0
 

Author Comment

by:RobertoFreemano
ID: 19208496
I think I added Microsoft Outlook 11 Object to it
0
 
LVL 2

Expert Comment

by:rkckjk
ID: 19208810
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
0
 

Author Comment

by:RobertoFreemano
ID: 19210189
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
0
 
LVL 2

Accepted Solution

by:
rkckjk earned 250 total points
ID: 19210756
I didn't know you wanted a form driven application. Instead of a module, setup a winform and place a button on it.

Here just paste this code in:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(56, 72)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(80, 32)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        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 Class

don't forget to add the Microsoft Outlook reference library.


0
 

Author Comment

by:RobertoFreemano
ID: 19211508
Excellent rkckjk,

Many thanks

;)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

770 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