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

Open new email window with attachment in VB

I have a button which, when clicked, needs to open up a "New Email" window with an attachment.  I don't need it to send the email, just open up the email window with a file attached, and maybe set the subject and body also.  The recipient can be left blank as the user can then decide who to send the file to.

The file I want to attach has already been generated at this point (which is an Excel file).  I understand this will not work with web based email, but 99% of the users will be in a corporate environment and will likely be using a desktop application such as Outlook.  TIA!
0
LD147
Asked:
LD147
  • 9
  • 8
  • 2
1 Solution
 
3_SCommented:
http://support.microsoft.com/kb/161088

See this example on how to automate outlook.  Are you using VB6 or .Net?
0
 
LD147Author Commented:
.Net (Visual Basic 2008)

I'm not trying to automated outlook.  It should bring up the New Email window using the user's default email program (which will likely be outlook, but could vary).  Right now I am trying this:

System.Diagnostics.Process.Start("mailto: ?subject='Subject' ?body='Body here'")

It opens up a new email window but the subject is showing as 'Subject' ?body='Body here', and I'm not sure how to specify an attachment.
0
 
3_SCommented:
Sorry can not help you further with this. No experience with VB.Net and email.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Meir RivkinFull stack Software EngineerCommented:
try:

            Process.Start("mailto:user@email.com&subject=EMAIL-SUBJECT&body=EMAIL-BODY");    
0
 
LD147Author Commented:
Well i already have this:

System.Diagnostics.Process.Start("mailto:&subject=Subject&body=Body&attach=c:/gatewaydata/myData_excel.xls")

It works except it's not attaching the file....
0
 
LD147Author Commented:
I also tried "Process.Start" instead of "System.Diagnostics.Process.Start"
0
 
Meir RivkinFull stack Software EngineerCommented:
use '&' character to distinguish between email parts.
0
 
LD147Author Commented:
That's what I am doing.
0
 
Meir RivkinFull stack Software EngineerCommented:
regarding "mailto" with attachments, the "attach" is an unofficial extension to the mailto protocol. It's not supported in a lot of e-mail clients, and those that implement it all work a little differently (for instance, some use "attachment" instead of "attach".
There's a security advisory for mailto attachments in Outlook 2003:
http://secunia.com/advisories/19819/
0
 
Meir RivkinFull stack Software EngineerCommented:
@LD147

in your post u didn't enter the email address it self (xxx@yyy.zzz):
Process.Start("mailto:<EMAIL-ADDRESS>&subject=<EMAIL-SUBJECT>&body=<EMAIL-BODY>");    

replace <EMAIL-ADDRESS>, <EMAIL-SUBJECT>, <EMAIL-BODY> with appropriate data.
0
 
LD147Author Commented:
I purposely left the email address out because I want that to be blank.  I want the user to be able to choose who to send it to.  The mail window opens fine, with nothing in the "To:" field, a subject and a body - just no attachment.  Searching is showing it may not be possible to add attachments.
0
 
Meir RivkinFull stack Software EngineerCommented:
the attachment is no longer supported by most of email clients due to security reasons.
alternatively, u can use MailMessage and SmtpClient:
Public Sub CreateMessageWithAttachment(ByVal server As String)
        ' Specify the file to be attached and sent.
        ' This example assumes that a file named Data.xls exists in the
        ' current working directory.
        Dim file As String = "MyAttachment.file"
        ' Create a message and set up the recipients.
        Dim message As New MailMessage("jane@contoso.com", "ben@contoso.com", "Quarterly data report.", "See the attached spreadsheet.")

        ' Create  the file attachment for this e-mail message.
        Dim data As New Attachment(file, MediaTypeNames.Application.Octet)
        ' Add time stamp information for the file.
        Dim disposition As ContentDisposition = data.ContentDisposition
        disposition.CreationDate = System.IO.File.GetCreationTime(file)
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file)
        disposition.ReadDate = System.IO.File.GetLastAccessTime(file)
        ' Add the file attachment to this e-mail message.
        message.Attachments.Add(data)

        'Send the message.
        Dim client As New SmtpClient(server)
        ' Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials

        Try
            client.Send(message)
        Catch ex As Exception
            Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString())
        End Try
        ' Display the values in the ContentDisposition for the attachment.
        Dim cd As ContentDisposition = data.ContentDisposition
        Console.WriteLine("Content disposition")
        Console.WriteLine(cd.ToString())
        Console.WriteLine("File {0}", cd.FileName)
        Console.WriteLine("Size {0}", cd.Size)
        Console.WriteLine("Creation {0}", cd.CreationDate)
        Console.WriteLine("Modification {0}", cd.ModificationDate)
        Console.WriteLine("Read {0}", cd.ReadDate)
        Console.WriteLine("Inline {0}", cd.Inline)
        Console.WriteLine("Parameters: {0}", cd.Parameters.Count)
        For Each d As DictionaryEntry In cd.Parameters
            Console.WriteLine("{0} = {1}", d.Key, d.Value)
        Next
        data.Dispose()
    End Sub

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
another option is to use outlook automation (if this is your default mail client):
http://support.microsoft.com/kb/310263

or using MAPI32.DLL:
http://www.codeproject.com/KB/IP/SendFileToNET.aspx
0
 
LD147Author Commented:
Well, it appears that I will have to program this for Outlook specifically, which is ok because 99.9% of the users will be using Outlook anyway.  I'm not going to worry about catering to the other .1%

So now I will do some searching for code to do this with outlook.  Sedgwick, thank you for the code.  It seems overkill for what I want to do though. I don't need to worry about SMTP or anything like that.
0
 
LD147Author Commented:
Those are for C#.  I'm using VB (.Net).  I'm still looking, lol....many of the examples I find just don't work for some reason.
0
 
LD147Author Commented:
I got it.  This works.  i got it from: http://www.eggheadcafe.com/community/aspnet/14/36056/send-email-in-outlook-fro.aspx then added the attachment code.


Dim objOutlook As Object
        Dim objOutlookMsg As Object
        Const olMailItem = 0
        Const olBCC = 3
        Const olImportanceHigh = 2
        Const olCC = 2
        Const olTo = 1


        objOutlook = CreateObject("Outlook.Application")

        objOutlookMsg = objOutlook.CreateItem(olMailItem)

        With objOutlookMsg
            Dim objOutlookRecip As Object = .Recipients.Add("name@domain.com")
            objOutlookRecip.type = olTo
            .Subject = "hello world"
            .Body = "body"
            .Importance = olImportanceHigh
            .attachments.add("c:\folder\filename.xls")
            .Display() ' use  .send() to send
        End With
        objOutlookMsg = Nothing
        objOutlook = Nothing

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
here's in vb.net:
Try
    ' Create the Outlook application by using inline initialization.
    Dim oApp As New Outlook.Application()
    
    'Create the new message by using the simplest approach.
    Dim oMsg As Outlook.MailItem = DirectCast(oApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
    
    'Add a recipient.
    ' TODO: Change the following recipient where appropriate.
    Dim oRecip As Outlook.Recipient = DirectCast(oMsg.Recipients.Add("e-mail address"), Outlook.Recipient)
    oRecip.Resolve()
    
    'Set the basic properties.
    oMsg.Subject = "This is the subject of the test message"
    oMsg.Body = "This is the text in the message."
    
    'Add an attachment.
    ' TODO: change file path where appropriate
    Dim sSource As [String] = "C:\setupxlg.txt"
    Dim sDisplayName As [String] = "MyFirstAttachment"
    Dim iPosition As Integer = CInt(oMsg.Body.Length) + 1
    Dim iAttachType As Integer = CInt(Outlook.OlAttachmentType.olByValue)
    Dim oAttach As Outlook.Attachment = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName)
    
    ' If you want to, display the message.
    ' oMsg.Display(true); //modal
    
    'Send the message.
    oMsg.Save()
    oMsg.Send()
    
    'Explicitly release objects.
    oRecip = Nothing
    oAttach = Nothing
    oMsg = Nothing
    oApp = Nothing
Catch e As Exception
    
    ' Simple error handler.
    Console.WriteLine("{0} Exception caught: ", e)
End Try

'Default return value.
Return 0

Open in new window

0
 
LD147Author Commented:
I pasted that code into the program, as I wanted to compare it to what I already had (which is working), and there are errors.  See pic.
errors.JPG
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 9
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now