Google Answers Logo
View Question
 
Q: sending email using vb.net ( Answered,   2 Comments )
Question  
Subject: sending email using vb.net
Category: Computers > Programming
Asked by: vbnewbie-ga
List Price: $30.00
Posted: 09 Nov 2005 03:13 PST
Expires: 09 Dec 2005 03:13 PST
Question ID: 590955
how do i send an email with attachment from vb.net using an exchange
server? please include the vb.net code to do this... tia
Answer  
Subject: Re: sending email using vb.net
Answered By: studboy-ga on 09 Nov 2005 12:13 PST
 
Dear vbnewie-ga,

The correct way to send attachment via Exchange using VB.NET is to use
the Outlook Object Library.  The way to do this is described in the 
following support article from Microsoft:

http://support.microsoft.com/?kbid=313803

(assuming you are using Visual Studio .NET)

I have copied and pasted the page below in case the link doesn't work:

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:a.  On the Project menu, click Add Reference.
b.  Click the COM tab, locate Microsoft Outlook 11.0 Library or
Microsoft Outlook 10.0 Object Library, and then click Select.
c.  In the Add References dialog box, click OK.  
d.  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
Comments  
Subject: Re: sending email using vb.net
From: madman1234-ga on 25 Sep 2006 11:25 PDT
 
Can you suppress the Outlook message :
"A program is automatically trying to send email on your behalf "
Subject: Re: sending email using vb.net
From: pinkfreud-ga on 25 Sep 2006 11:50 PDT
 
madman1234-ga,

To ask a new question on Google Answers, you'll need to start here:

http://answers.google.com/answers/askquestion

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy