SmtpMail.SmtpServer.Insert(0, "mail.mycompany.com") causes 0x80040220 to be thrown.


Email was being sent using SmtpMail from System.Web.Mail. The code worked on the older 2000 server, however did not work when placed on the new 2003 server. When the email was sent, the code threw the following error: (0x80040220) The 'SendUsing' configuration value is invalid.


This line was being used to send the email:


    SmtpMail.SmtpServer.Insert(0, "mail.mycompany.com");


I've seen this type of code and its error show up on cases, blogs and discussion groups.  This really has nothing to do with Windows or Exchange.  Its just incorrectly written code...



The first time I saw code with this error, I checked the customer's code and found this line...
    SmtpMail.SmtpServer.Insert(0, "mail.mycompany");


I asked them to remove the Insert part of the satement so it will be:
    SmtpMail.SmtpServer = "mail.mycompany";


Yes, it worked fine after that.  Thier code was based-upon a sample they got off a non-Microsoft site.
  


RESOLUTION:
    The code was setting the SMTP server to use with the following line:
          SmtpMail.SmtpServer.Insert(0, "mail.mycompany.com");


    It looks like a copy of SmtpMail.SmtpServer's value is being modied and not the actual value.
    Please note that this is a SmtpServer never gets upated. SmtpServer is defined as follows:


          [C#] public static string SmtpServer {get; set;}


    The code should be set as follows:
          SmtpMail.SmtpServer = "mail.mycompany.com";


Breaking this down...
    The other machine had a local smtp server running, which was setup for sending mail.
    The default value of SmtpMail.SmtpServer is blank ("").  
    Note that if SmtpMail.SmtpServer is blank (""), the email will be sent to the the SMTP server on the local machine.
    The code was modifying a copy of the value of what SmtpMail.SmtpServer was set to
    (SmtpMail.SmtpServer is a property, not a variable). This in effect caused
    SmtpMail.SmtpServer to never change, so it remained as its default value - which is blank ("").
    When the email was sent, the email was sent to the local SMTP server since the SmtpMail.SmtpServer was set to blank ("").
    The old machine had an SMTP server running on it, so the mail would go to it and then be relayed.
    The new machine did not have an smtp server on it, so the code threw the
    error:  0x80040220 - The 'SendUsing' configuration value is invalid.


 

Comments (0)

Skip to main content