Results 1 to 8 of 8

Thread: [RESOLVED] Using a 'Namespace' in ASP.net app

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Resolved [RESOLVED] Using a 'Namespace' in ASP.net app

    Ultimately I am trying to send email asynchronously, and in by investigation I was routed here http://msdn.microsoft.com/en-us/libr...mtpclient.aspx. Microsoft's sample code mentioned "Namespace Examples.SmptExamples.Async"

    I just don't know how to use this code. I tried to put it in a aspx.vb file, but it give me an error saying "Namespace statements can only occur at the file or namespace level." Again not sure what this means.

    If all the code goes in a file, what to I call the file? I tried putting it in a module, but I got the same error. I could not find any guidance on how to use the Namespace.

    Can anyone tell me how to use this sample code. It appears to be exactly what I was looking for.

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Using a 'Namespace' in ASP.net app

    What is the error you get, if you get an error it's always useful to post it.

    As for the namespace it is just an a method of organisation. See this description of namespaces

    You can name the namespace whatever you like, in the example you posted they have used Examples.smtpExamples.Async as it describes what the code is doing.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Using a 'Namespace' in ASP.net app

    The error is:

    "Namespace statements can only occur at the file or namespace level."

  4. #4
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Using a 'Namespace' in ASP.net app

    In that case check where you are declaring the namespace, it cannot be declared inside a class but it is ok to nest namespaces.

    Make sure the namespace is declared directly after your import statements at the top of the file and you should be ok.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Using a 'Namespace' in ASP.net app

    yes, I get this, but what type of file do I put this in (what is the extension)? And how do I reference the public sub SendAnEmail?

    Without Namespaces, I would normally put this code in a Module so it is accessible from any class. When I try this (saved as Module.vb)
    Code:
    Module Module1
    
    ' All of the NameSpace sample code
    
    End Module
    I get the same error that the namespace must be at the file level. If I remove the first and last lines and just leave the sample code, the error goes away, but I can not access the Public sub SendAnEmail.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Using a 'Namespace' in ASP.net app

    I am making progress. I saved the same code in a as modTextMgt.vb code. I left out the Module and end module lines (so just included Microsoft's sample code).

    Then I wrote my application, and include an
    Code:
    Imports TextMgmt.Example.Smpt.Async
    at the top of the Class. Note, TextMgmt is the name of my app, and Example.smpt.Async was the name of the Namespace.

    I can run this but now I have a different problem. I can modify the code so the email is sent immediately (this works), but the Async send does not. This code is only slightly modified from the sample. It sends 2 emails - one immediately and one synchronously.

    Code:
    Imports System
    Imports System.Net
    Imports System.Net.Mail
    Imports System.Net.Mime
    Imports System.Threading
    Imports System.ComponentModel
    
    Namespace Example.Smpt.Async
        Public Class SimpleAsynchronous
            Private Shared mailSent As Boolean = False
            Private Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
                ' Get the unique identifier for this asynchronous operation.
                Dim token As String = CStr(e.UserState)
    
                If e.Cancelled Then
                    Console.WriteLine("[{0}] Send canceled.", token)
                End If
                If e.Error IsNot Nothing Then
                    Console.WriteLine("[{0}] {1}", token, e.Error.ToString())
                Else
                    Console.WriteLine("Message sent.")
                End If
                mailSent = True
            End Sub
            Public Shared Sub SendAnEmail(ByVal args() As String)
                ' Command line argument must the the SMTP host.
                Dim client As New SmtpClient(args(0))
                ' Specify the e-mail sender.
                ' Create a mailing address that includes a UTF8 character
                ' in the display name.
                client.Credentials = New Net.NetworkCredential("[email protected]", "mypassword")
                client.Port = 587
                client.Host = "smtp.gmail.com"
                client.EnableSsl = True
                Dim mail As New MailMessage
                mail.From = New MailAddress("[email protected]")
                mail.To.Add("[email protected]")
    
                mail.Subject = "email test"
                mail.Body = "This is the test message"
    
    
                AddHandler client.SendCompleted, AddressOf SendCompletedCallback
                ' The userState can be any object that allows your callback 
                ' method to identify this send operation.
                ' For this example, the userToken is a string constant.
    
                Dim userState As String = "test message1"
    
                client.Send(mail)                                ' <<< this works
                Try
                    client.SendAsync(mail, userState)     '  <<< this does not work
                Catch ex As Exception
                    Dim s As String = ex.Message
                End Try
    
            End Sub
        End Class
    End Namespace
    The exception is just "Failure Sending Email", and the code never goes into SendCompletedCallBack. Any idea why the Async does not work?

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: "Failure Sending Email" Error

    I am able to send an email using SMTPClient Send, but when I use SendAsync I get an SmtpException error "Failure Sending Email." On further investigation I found from the InnerException the following:

    Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.
    So how do you set the Async Attribute of the page to true?

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Using a 'Namespace' in ASP.net app

    I did figure it out. I had to set the Async = "True" in the @ Page settings at the top of my aspx page. This allowed the async processing of emails.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width