Library code snippets

Sending Email With an Attachment

This code demonstrates how to send an email in VB.NET with an attachment. It currently attaches the file "C:\myattachment.zip" - in order to work, this file must obviously exist!

Dim oMsg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()

oMsg.From = "noone@nobody.com"
oMsg.To = "someone@somewhere.com"
oMsg.Subject = "Email with Attachment Demo"
oMsg.Body = "This is the main body of the email"
Dim oAttch As MailAttachment = New MailAttachment("C:\myattachment.zip")
oMsg.Attachments.Add(oAttch)
SmtpMail.Send(oMsg)

Comments

  1. 21 Jul 2008 at 18:28

    this is the vb.net coding .......

    note:- this is the coding which uses the gmail smtp server, u need to enter the valid email id and password of gmail, then only this will work...

    if u have any pblm then ask me any time...

    my id is:- k.ashish237@gmail.com 

     Imports System.Net.Mail

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim MyMailMessage As New MailMessage()

            MyMailMessage.From = New MailAddress("sender email")

            MyMailMessage.To.Add(TextBox1.Text)

            MyMailMessage.Subject = "subject title"

            MyMailMessage.Body = "enter the body text message that u want to send"
            For counter = 0 To ListBox1.Items.Count - 1
                Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment(ListBox1.Items(counter))
                MyMailMessage.Attachments.Add(Attach)
            Next

            Dim SMTPServer As New SmtpClient("smtp.gmail.com")
            SMTPServer.Port = 587
            SMTPServer.Credentials = New System.Net.NetworkCredential("yourid@gmail.com", "id password")
            SMTPServer.EnableSsl = True

            Try
                SMTPServer.Send(MyMailMessage)
                MessageBox.Show("Email Sent")
            Catch ex As SmtpException
                MessageBox.Show(ex.Message)
            End Try
        End Sub

     

    same coding with the tools of vb.net......

    hope this will help u.... 

    Imports System.Net.Mail

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim MyMailMessage As New MailMessage()

            MyMailMessage.From = New MailAddress(TextBox3.Text)

            MyMailMessage.To.Add(TextBox1.Text)

            MyMailMessage.Subject = TextBox2.Text

            MyMailMessage.Body = RichTextBox1.Text
            For counter = 0 To ListBox1.Items.Count - 1
                Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment(ListBox1.Items(counter))
                MyMailMessage.Attachments.Add(Attach)
            Next

            Dim SMTPServer As New SmtpClient("smtp.gmail.com")
            SMTPServer.Port = 587
            SMTPServer.Credentials = New System.Net.NetworkCredential(TextBox4.Text, TextBox5.Text)
            SMTPServer.EnableSsl = True

            Try
                SMTPServer.Send(MyMailMessage)
                MessageBox.Show("Email Sent")
            Catch ex As SmtpException
                MessageBox.Show(ex.Message)
            End Try
        End Sub

  2. 07 May 2008 at 03:00

    to krishna reddy..

    hi krishna how r u...

    i m making an application in vb.net to send email using smtp server... but it dosnt work... even it does not send mail to spam...

    if it is possible, then plz send me the coding of ur application and the procedure... to me by my email id... this is my colg project and it didnt work, help me plz....

    plzzzzzzzzzzzzzzzzzzzzzzzzzzzz 

    thank u....

  3. 05 Apr 2007 at 10:14
    I want  someone to help me with answers to sending sms to mobile phones from my website.
  4. 16 Jun 2006 at 11:52

    i have to develop an application that can send emails to our clients through a vb.net application...

    but da problm is dat i cant use a SMTP server... oderwise ther wudnt b ne problm @ all..!!!

    help...Sad [:(]

  5. 16 Jun 2006 at 11:47

    all u hav to do is add reference "System.web.dll" in ur project...

    that u can do by :----

    1) rite - clickin on ur project in da solution explorer and click on 'add reference'...

    2) got to da 'COM' tab and add the dll i mentioned before...Smiley Face [:)]

    njoi...!!!Smiley Face [:)]

  6. 26 Oct 2005 at 16:54

    Great Thanks

  7. 26 Oct 2005 at 08:14

    hi


    you can keep that code any where its not a problem whether a webpage calls it or a windows app.


    whats important is that it works on the .net framework.


    do you mean by client side, that you use javascript, if yes, you can use AJAX to call a function from the server side, without any trip to the server.


    Regards



  8. 26 Oct 2005 at 06:37

    I have seen this format before and I believe that format is for VB System not a Web App - I am not sure if I stated my question clearly enough.


    I am using .NET
    from the client side I want to be able to send an email and specify the email server username and password.



    I will retry that code just to be sure - I posted the message a while back and cant remember much..

  9. 25 Oct 2005 at 08:33

           Dim objSender As SmtpMail
           objSender.SmtpServer = "SMTPMAILSERVER"
           Dim objMsg As New MailMessage
           objMsg.From = "email@server.com"
           objMsg.To = "email@server.com"
           objMsg.Subject = "Test HTML"
           objMsg.BodyFormat = MailFormat.Html
           objMsg.Body = "<html><h1>hello</h1></html>"
           ''
           objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/smtsperver") = "SMTPMAILSERVER"
           objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
           objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
           objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
           objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "SMTPUSERNAME"
           objMsg.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "SMTPPASSWORD"
           ''
           objSender.Send(objMsg)

  10. 14 Sep 2005 at 21:43

    The code is simple and works fine with me!!!

  11. 08 Jun 2005 at 21:39

    I have tried to attach to oMsg.Body a richtexbox text.
    I found that it doesn't work this way. I presume is because I have to send html formated text or plain text.
    But still, I would like to send formated texts (from my app) in order to have well designed e-mails to my receivers.
    How I amm supposed to accomplished that?

  12. 18 Apr 2005 at 03:27
    ok ive tried this code but it is not working, i keep getting the error----

    <An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll>
    <Additional information: Could not access 'CDO.Message' object.>

    Please Help!!!
    Kinghenery87@aol.com
  13. 09 Apr 2005 at 05:05

    I can't get System.Web.Mail name space on my system. Is there any installation problem??


    Ram Kinkar Pandey
    PRamkinkar@pisoftek.com
    Priya International Softek Limited
    India

  14. 18 Feb 2005 at 23:05

    One thing about this code - I am trying to send with a specific SMTP server and Need to utilize the appropriate Authentication to use that SMTP server..
    Any Ideas on how to do that ?
    I know I can specify the SMTP server but how do I Specify the Authentication (username and password) obviously I might not want to send this in the clear..Can anyone help ..

  15. 28 Nov 2004 at 14:21

    First Add Reference to System.Web.Dll
           Go to top Menu
           -Project
             -Add References


           Select System.Web.dll


    Use this Coding
           oMsg.From = Me@Here.com"
           oMsg.To = "He@There.com"
           oMsg.Subject = "Dit is my Afrikaanse Boodskap"
           oMsg.Body = "Ek wil dit bysit"
           Dim oAttch As System.Web.Mail.MailAttachment = New System.Web.Mail.MailAttachment("C:\ftpfile.txt")
           oMsg.Attachments.Add(oAttch)
           System.Web.Mail.SmtpMail.Send(oMsg)
           MessageBox.Show("Mail Message Send")

  16. 09 Nov 2004 at 19:57
    Hi,

    I am using XML Text reader in PDA application to read large XML file, its working in Emulator but not in the PDA device. SO if any body can give suggetion, I will be thankful.

    Ramakrishna Tulluri
    India
  17. 30 Jun 2004 at 17:24

    Do I double click the forum to enter this code?
    AND PLEASE KEEP THE TUTORIALS ON THIS COMING!!
    javascript:smilie('')
    javascript:smilie('')

  18. 12 Feb 2004 at 18:18

    I am pretty new to VB.net programming and I seem to be missing a critical element to understanding how to use these code samples. For example the code below taken from this site does not work for me. VB underlines the two items surrounded by ** in the sample code below and says that "type Mail Attachement is not defined" also says that "name smtpMail is not declared"


    What am I missing - each time I try to download sample code I run into the same types of issues????


    Thanks!


    Dim oMsg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()


    oMsg.From = "noone@nobody.com"
    oMsg.To = "someone@somewhere.com"
    oMsg.Subject = "Email with Attachment Demo"
    oMsg.Body = "This is the main body of the email"
    Dim oAttch As *MailAttachment = New MailAttachment("C:\myattachment.zip")
    oMsg.Attachments.Add(oAttch)
    SmtpMail.Send*(oMsg)

  19. 01 Jan 1999 at 00:00

    This thread is for discussions of Sending Email With an Attachment.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

We'd love to hear what you think! Submit ideas or give us feedback