• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2995
  • Last Modified:

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
0
RobertoFreemano
Asked:
RobertoFreemano
  • 6
  • 6
1 Solution
 
mrwad99Commented:
0
 
Richard KreidlSoftware DeveloperCommented:
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
 
RobertoFreemanoAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Richard KreidlSoftware DeveloperCommented:
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
 
RobertoFreemanoAuthor Commented:
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
 
Richard KreidlSoftware DeveloperCommented:
Sorry, it was typo in my part...

The line should be:

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




0
 
RobertoFreemanoAuthor Commented:
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
 
Richard KreidlSoftware DeveloperCommented:
Did you add Microsoft Outlook 9.0 Object Library as reference in your application?
0
 
RobertoFreemanoAuthor Commented:
I think I added Microsoft Outlook 11 Object to it
0
 
Richard KreidlSoftware DeveloperCommented:
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
 
RobertoFreemanoAuthor Commented:
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
 
Richard KreidlSoftware DeveloperCommented:
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
 
RobertoFreemanoAuthor Commented:
Excellent rkckjk,

Many thanks

;)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now