Results 1 to 2 of 2

Thread: Send attachments in an e-mail message by using Visual Basic .NET

  1. #1
    Join Date
    Apr 2008
    Posts
    438

    Send attachments in an e-mail message by using Visual Basic .NET

    To send attachments in an e-mail message by using Visual Basic .NET, follow these steps:

    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the Visual Basic Projects types list, click Console Application.

    By default, the Module1.vb file is created.
    4. If Office Outlook 2003 is installed on your development computer, add a reference to the Microsoft Outlook 11.0 Object Library. If Outlook 2002 is installed on your development computer, add a reference to the Microsoft Outlook 10.0 Object Library. To do so, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab, locate Microsoft Outlook 11.0 Library or Microsoft Outlook 10.0 Object Library, and then click Select.
    3. In the Add References dialog box, click OK.
    4. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
    5. In the code window, replace the code with the following:

    Module Module1

    Sub Main()
    ' Create an Outlook application.
    Dim oApp As Outlook._Application
    oApp = New Outlook.Application()

    ' Create a new MailItem.
    Dim oMsg As Outlook._MailItem
    oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
    oMsg.Subject = "Send Attachment Using OOM in Visual Basic .NET"
    oMsg.Body = "Hello World" & vbCr & vbCr

    ' TODO: Replace with a valid e-mail address.
    oMsg.To = "user@example.com"

    ' Add an attachment
    ' TODO: Replace with a valid attachment path.
    Dim sSource As String = "C:\Temp\Hello.txt"
    ' TODO: Replace with attachment name
    Dim sDisplayName As String = "Hello.txt"

    Dim sBodyLen As String = oMsg.Body.Length
    Dim oAttachs As Outlook.Attachments = oMsg.Attachments
    Dim oAttach As Outlook.Attachment
    oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)

    ' Send
    oMsg.Send()

    ' Clean up
    oApp = Nothing
    oMsg = Nothing
    oAttach = Nothing
    oAttachs = Nothing
    End Sub

    End Module

    6. Search for the TODO text string in the code, and then modify the code for your environment.
    7. Press the F5 key to build and to run the program.
    8. Make sure that the e-mail message and the attachment have been sent.

  2. #2
    Join Date
    Apr 2008
    Posts
    439

    Re: Send attachments in an e-mail message by using Visual Basic .NET

    The Email Factory for .NET SMTP component provides methods for communicating with an SMTP server. The process for establishing an interactive session with an SMTP server using the Email Factory for .NET component is as follows:

    1. Creating a new Smtp instance with events
    2. Establishing a connection
    3. Creating an email message
    4. Defining addressing and other message properties
    5. Defining the message body
    6. Adding message attachments
    7. Sending an email message
    8. Releasing a connection

    Each of these processes is described in the sections below.

    Creating a new Smtp instance with events


    Before creating a new Smtp instance, ensure that the Jscape.Email scope is defined in your Imports statements, and that the Jscape.Email.dll is referenced in your project. Refer to Getting Started in the Email Factory for .NET Help for more information about adding the Jscape.Email.dll reference to your projects.

    The Smtp component may publish one or more events during the lifetime of an SMTP session. Any object that subscribes to events published by the Smtp component can receive and process the following events.

    * The ConnectedEvent is fired by the Smtp instance once a connection to the SMTP server has been established.
    * The DisconnectedEvent is fired by the Smtp instance once the connection to the SMTP server has been released.
    * The CommandSentEvent is fired by the Smtp instance for each command sent to the SMTP server.
    * The DataReceivedEvent is fired by the Smtp instance for each response received from the SMTP server.

    After you have the Jscape.Email.dll reference added to your project, you can define an Smtp instance with events.

    Dim WithEvents mySmtp As Smtp = Nothing

    To create a new Smtp instance, provide the SMTP server hostname as an argument.

    mySmtp = New Smtp("hostname")

    Now you can create the event handler methods for the Smtp events.

    Public Sub OnConnected(ByVal sender As Object, ByVal e As SmtpConnectedEventArgs) Handles mySmtp.ConnectedEvent

    Console.WriteLine("Connected to {0}", e.Host) End Sub

    Public Sub OnDisconnected(ByVal sender As Object, ByVal e As SmtpDisconnectedEventArgs) Handles mySmtp.DisconnectedEvent

    Console.WriteLine("Disconnected.") End Sub

    Public Sub OnDataReceived(ByVal sender As Object, ByVal e As SmtpDataReceivedEventArgs) Handles mySmtp.DataReceivedEvent

    Console.WriteLine("Response: " + e.Response) End Sub

    Public Sub OnCommandSent(ByVal sender As Object, ByVal e As SmtpCommandSentEventArgs) Handles mySmtp.CommandSentEvent

    Console.WriteLine("Command: " + e.Command) End Sub

    Establishing a connection

    Once an Smtp instance has been created and the Smtp event handlers defined, you may establish a connection to the SMTP server by invoking the Connect() method.

    mySmtp.Connect()

    Creating an Email message

    To create an email message, construct a new EmailMessage instance passing the To: address and From: address to the constructor. You may also assign the To and From properties on the EmailMessage instance if you are using the default US-ASCII character set encoding. See the following section for more information about specifying message properties. If you are using another character set encoding, use the SetTo and SetFrom methods. Refer to the Email Factory for .NET Help for more information.

    Public message As EmailMessage = Nothing
    ' create instance with To and From addresses
    message = New EmailMessage("ToAddress", "FromAddress")

    Defining addressing and other message properties


    In addition to passing the To and From addresses to the EmailMessage constructor, you can specify the properties on the message instance. The EmailMessage class also has properties and methods for adding carbon-copy, blind-carbon-copy, and reply-to recipients, and message subject and priority.

    Note: The To and From addresses are required properties.

    message = New EmailMessage
    message.To = "ToAddress"
    message.From = "FromAddress"
    message.Cc = "CcAddress"
    message.Bcc = "BccAddress"
    message.ReplyTo = "ReplyToAddress"
    message.Priority = "normal"
    message.Subject = "This is a test subject"

    Defining the message body


    You can set the message body to the contents of a file, a database query result, or a hand-coded message, as shown in the following example. Refer to the Email Factory for .NET Help for more information about defining the message body contents from a FileStream.

    message.SetBody("This is a sample message")

    Adding message attachments


    You can add one or more attachments to an email message by invoking the AddAttachment() method passing an Attachment instance as an argument. The attachment instance can be constructed by passing a file path as an argument.

    Note: Due to the way that the message body is copied when adding attachments it is important that attachments be added only after the body of the message has been defined. Changing the Body, Content-Type, or Content-Transfer-Encoding of the message after attachments have been added may result in undesired effects.

    Public att As Attachment = Nothing
    att = New Attachment("c:\home\report.doc")
    message.AddAttachment(att)

    Sending an Email message

    To send a message through an established connection simply invoke the Send() method passing the message instance as an argument.

    mySmtp.Send(message)

    Releasing a connection

    To release an established connection simply invoke the Disconnect() method as follows:

    mySmtp.Disconnect()

Similar Threads

  1. Unable to send mail with Hotmail it gives error message
    By VeGA BacK in forum Technology & Internet
    Replies: 3
    Last Post: 26-11-2010, 12:21 AM
  2. Windows mail - CAN'T SEND MAIL!! Error message - 0x800CC0F
    By Rachel G in forum Windows Vista Mail
    Replies: 2
    Last Post: 07-04-2010, 12:08 AM
  3. Send Organized pictures attachments while sending an e-mail
    By Adrina_g in forum Technology & Internet
    Replies: 3
    Last Post: 24-11-2009, 01:31 PM
  4. Windows Mail "send" error message - need help to repair
    By Lupin in forum Windows Vista Mail
    Replies: 2
    Last Post: 23-08-2009, 04:54 PM
  5. Visual Basic 2005 or Visual Basic 6
    By Aasha in forum Software Development
    Replies: 5
    Last Post: 15-01-2009, 06:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,057,782.57199 seconds with 17 queries