Search code examples
vb.netemail-attachmentsemail-clientmapi

How to send an email with attachment using default email client in vb.net


I am wondering if how it could be done. Is it possible to do it?

Here is my simple code:

Try
    MsgBox("Please wait this may takes time to load", vbInformation, "Mailing System")
    System.Diagnostics.Process.Start("mailto:" & txtEmailadd.Text)
Catch ex As Exception
    MsgBox(ex.Message & " Having some technical difficulties, kindly check if the email textbox has data in it", vbCritical, "System Advisory")
End Try

I want to add an attachment inside of this before the default client loads. Unfortunately, i don't find any answers in the web. Can you give some advice? Thanks so much in advance.


Solution

  • you need to call the MailMessage.Attachments(). Here's the example code:

    Dim MailMsg as New MailMessage
    Dim loAttachment As Attachment = Nothing
    Dim ls_email_attach as String
    Dim server as String
    
    ls_email_attach = "attach.xls"
    server = "Mail server info"
    
    //add the attachment
    If Not ls_email_attach.Trim = "" Then
            loAttachment = New Attachment(ls_email_attach)
            loMailMsg.Attachments.Add(loAttachment)
    End If
    
    //send the email
    MailMsg = New SmtpClient(server)
    MailMsg.Send(loMailMsg)
    

    Please see this for reference.

    Hope it helps