Results 1 to 3 of 3

Thread: [RESOLVED] Problems with System.Net.Mail

  1. #1

    Thread Starter
    New Member ulv81's Avatar
    Join Date
    Sep 2009
    Location
    Norway
    Posts
    14

    Resolved [RESOLVED] Problems with System.Net.Mail

    Hello.

    When I use the code below to send an e-mail from my form, the mail is not sent before the program is closed...

    Anybody have any idea why this is so?!

    Code:
            'create the mail message
            Dim mail As New MailMessage
    
            'set the addresses
            mail.From = New MailAddress("[email protected]")
            mail.To.Add("[email protected]")
    
            'set the content
            mail.Subject = "Mail Subject here."
            mail.Body = "Mail body here."
            mail.IsBodyHtml = False
    
            'send the message
            Dim smtp As New SmtpClient("YOUR SMTP SERVER")
            smtp.Credentials = New System.Net.NetworkCredential("Username", "Password")
    
            Try
                smtp.Send(mail)
    
            Finally
                mail.Dispose()
                smtp = Nothing
    
            End Try

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Problems with System.Net.Mail

    Add a Catch block to your Try block and then you can actually see what the error is that is undoubtedly being thrown and is currently hidden from you... Alternatively, while you are still writing the program like this then you could just remove the Try block altogether so that any exceptions are thrown in the debugger and you can easily look at all of the information available with the exception

    EDIT: Actually it seems I was wrong, your current code should still actually throw exceptions. However, just to be sure, if you stick a Catch block in there then do you get any exceptions thrown? For example:
    Code:
    Try
                smtp.Send(mail)
    Catch ex As Exception
              MessageBox.Show(ex.Message)
    Finally
                mail.Dispose()
                smtp = Nothing
    
    End Try
    Last edited by chris128; Sep 28th, 2009 at 09:57 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    New Member ulv81's Avatar
    Join Date
    Sep 2009
    Location
    Norway
    Posts
    14

    Re: Problems with System.Net.Mail

    Thank you. I got it now

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