send mail

Hi,

i have problem in writing send mail codes. can someone pls help me regarding it i have written code to send mails but somehow there are errors. i have attach my codes below. when i click on the send button, an error message prompt out saying that "An Unhandled exception of type 'System.nullReferenceException' occured in testingMail.exe. Additional information: object reference not set to an instance of an object."

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

Dim message As System.Web.Mail.MailMessage

message.To = "li-ching.ooi@avagotech.com"

message.From = "wei-luen.tan@avagotech.com"

message.Subject = "test"

message.Body = txtMsg.Text

Try

SmtpMail.SmtpServer = "web-proxy.mys.agilent.com"

SmtpMail.Send(message)

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub




Answer this question

send mail

  • Ugla

    Most likely, that exception has some nested 'inner exceptions', to access those, you will have to catch the error and enumerate the innerexeptions

    Try the code bellow:

    Try

    SmtpMail.Send(mail)

    Catch ex As Exception

    While Not (ex.InnerException Is Nothing)

    MessageBox.Show(ex.InnerException.ToString())

    ex = ex.InnerException

    End While

    End Try



  • Bs61014

    You have to create new instance of the System.Web.Mail.MailMessage class by using the new keyword

    Dim message As New System.Web.Mail.MailMessage()

    The reason you don't have to do this for the SmtpMail.SmtpServer and SmtpMail.Send, is because these are Shared methods of the SmtpMail class



  • Grigori Somin MSFT

    Hi. Are you using VS 2005 if yes then use SmtpClient class instead of SmtpMail which is now obsolete in .Net framework 2.0

    SmtpMail was more like a wrapper for CDO (Collaboration Data Objects) which is COM.  

    If you have, for example, authentication problems you'll receive the same error message if you use SmtpMail. Check things like if you have access to that server on port 25 or if you have to authenticate to the server.

    SmtpClient is not a warpper and it doesn't use CDO.

     


  • Ian M

    If it's any other than port 25, you should add the port numer. The error message is very clear, stating that it can not connect to the server.

    I tried connecting to that server myself (web-proxy.mys.agilent.com) and my telnet session timed out.

    It's also possible that your (windows) firewall is blocking you application



  • RBecker

    the inner exception:

    system.reflextion.TargerInvocationException: Exception has been thrown by target of an invocation. --> System.Runtime.InteropServices.COMException (0x80040213) : The transport failed to connect to the server.

    --- End of inner exception stack trace ---

    * is this indicating the i enter wrong smtp server name or my connection to the LAN is improper



  • BadAndre

    ok..thanks a lot.

  • C Stephens

    hi,

    before i continue tracking the error, can you please tell me where can i get the smtpServer address i jus wana confirm that i have inserted the correct 1.



  • Hussein

    Can you post the full exeption please. The innerexception will most likely contain usefull information about the error.



  • Twitchy

    ok.. here is the error.. (is this the error exception u asked me to post if not, where can i get the full exception ) i'm sorry as i'm new in this field.

    'DefaultDomain': Loaded 'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded.

    'TestingMail': Loaded 'C:\Documents and Settings\lichioo\My Documents\Visual Studio Projects\TestingMail\bin\TestingMail.exe', Symbols loaded.

    'TestingMail.exe': Loaded 'c:\windows\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll', No symbols loaded.

    'TestingMail.exe': Loaded 'c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No symbols loaded.

    'TestingMail.exe': Loaded 'c:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll', No symbols loaded.

    'TestingMail.exe': Loaded 'c:\windows\assembly\gac\system.web\1.0.5000.0__b03f5f7f11d50a3a\system.web.dll', No symbols loaded.

    An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll

    Additional information: Could not access 'CDO.Message' object.

    'TestingMail.exe': Loaded 'c:\windows\assembly\gac\microsoft.visualbasic\7.0.5000.0__b03f5f7f11d50a3a\microsoft.visualbasic.dll', No symbols loaded.

    > TestingMail.exe!TestingMail.Form1.Button1_Click(Object sender = {System.Windows.Forms.Button}, System.EventArgs e = {System.EventArgs}) Line 153 + 0x8 bytes Basic

    TestingMail.exe!TestingMail.Form1.Main() Line 5 + 0x1d bytes Basic



  • LanceM

    Hi,

    i would like to ask whether how am i going to add attachment in my mails from my program.



  • ataha13

    i'm using VS2003 only.

    must i add the port number in the smtpserver address



  • Arran

    The smtp server is usually given to you by your provider. When you are in a company, they usaully have their own smtp server (like a Microsoft Exchange Server). If you are a home user, this is the same server as you specify in the 'outgoing mail server' of you mail client (like Outlook).

    Depending on where the application is used, the smtp server address will vary, so it's a good idea to store it in a configuration file so that it can be changed without having to recompile the application.

    As for specifying a different port number. I've been looking at it, and apparently there is no standard way to specify the port number in the SmtpMailSmtpServer. Normally you won't need to do this as almost every mail server will operate on port 25.



  • David Hayden

    how can i include the port number in the smtpserver address

    by the way, really thanks a lot for helping...



  • vgjorgov

    hi,

    i still have problems regarding it. i have add the "New" keyword already but it show another error message, saying that "could not access 'CDO.Message'" object.



  • send mail