Solved

outlook mailto with attachment vb.net 2003

Posted on 2007-03-31
13
2,935 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to change a specific bit from a file 2 21
Get String split 5 33
index Out OF Range Exception error 4 31
Getfiles in vb.net 28 22
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 …
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now