Solved

outlook mailto with attachment vb.net 2003

Posted on 2007-03-31
13
2,961 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
[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
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …

724 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