locked
Having trouble with System.Net.Mail.MailMessage() RRS feed

  • Question

  • User201647903 posted

    Hi All :-)

    I'm sure I am missing something simple, but since it's been months since I've done any VB coding and I have just recently upgraded from .Net 2.0 to .Net 3.5 I need a little help (please).  I am using Visual Studio 2008 coding in VB.Net and can't remember how to send email from my web page.  I have a page that has a form with the TO, FROM, SUBJECT and MESSAGE area defined. I have searched on-line (and here) and can't find a good example to show me what I am missing.  Here's what I have: 

     
    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
      Dim Mailobj As System.Net.Mail.SmtpClient.Send(lsFrom, lsTo, lsSub, lsBody)
      Dim MailMsg As New System.Net.Mail.MailMessage()
      Dim lsTo As String = "info@ourheartbreakingchoices.com"
      Dim lsFrom As String = ""
      Dim lsSub As String = ""
      Dim lsBody As String = ""
    
      lsFrom = txtFrom.Text.ToString()
      lsSub = txtSubject.Text.ToString()
      lsBody = txtMessage.Text.ToString()
    
      If lsFrom = "" Then
       lblMessage.Text = "Please enter a return email address."
       lblMessage.Visible = True
      End If
    
      If lsSub = "" Then
       lblMessage.Text = "Message has no Subject. Please enter a Subject."
       lblMessage.Visible = True
      End If
    
      If txtMessage.Text = "" Then
       lblMessage.Text = "You have not entered a message, please enter your message and retry."
       lblMessage.Visible = True
      ElseIf txtMessage.Text <> "" Then
       If lsFrom = "" Then
         lblMessage.Text = "Please enter a return email address."
         lblMessage.Visible = True
       ElseIf lsSub = "" Then
         lblMessage.Text = "Message has no Subject. Please enter a Subject."
         lblMessage.Visible = True
       Else
         MailMsg.From = lsFrom
         MailMsg.Subject = lsSub
         MailMsg.Body = lsBody
         Mailobj.Send(MailMsg)
         lblMessage.Text = "<span style='font-style: italic; font-weight: bold; color: #8981b3;'<br />
         Thank you!<br />We have received your request<br />and will be responding shortly.<br />"
         lblMessage.Visible = True
       End If
    
      End If
    
    End Sub
     

    Just a note on the line of code above that is showing it as a comment - I put a break in the code I uploaded here for viewing purposes, so it would not scroll - but in my actual code there is no hard return to cause the ' to render in VB as a comment. 

    The problem I am having is first: I am getting an error (blue squigley) on lsFrom in the declaration of Mailobj As System.Net.Mail.SmtpClient.Send of: "Array bounds cannot appear in type specifiers." and second I am getting an error (blue squigley) on MailMsg.From = lsFrom of:  "Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'." then, I am getting an error (blue squigley) on Mailobj.Send(MailMsg) of: "Name 'Mailobj' is not declared." when obviously I HAVE declared Mailobj in the very first line of code in my sub!

    Can someone please point me in the right direction to correctly set up my email in VB?  Or if you can tell me what I have done wrong here, I would really appreciate any help given!  TIA

    Sunday, September 21, 2008 6:54 PM

Answers

  • User-1161063654 posted
    No problem at all... here is a snapshot of my web.config:  
    <system.net>
        <mailSettings>
          <smtp>
            <!--<network host="relay-hosting.secureserver.net" userName="email@email.com" password="abc123" />-->
            <network host="smtpout.secureserver.net" userName="email@email.com" password="abc123" />
          </smtp>
        </mailSettings>
      </system.net>

     The above configuration is for when I am developing locally.  Like I said, I do not know exactly why the dedicated hosting server works from my personal development machine.

     My web.config that is deployed on GoDaddy as the smtpout server commented out and the relay-hosting instance active.

    When you use the web.config method of specifying the host information you do not need to use the SmtpClient.Host property.  So you can remove that line from your code and just build your mail message object and use SmtpClient.Send(MailMessage).

    Hope this helps a little more.

    - Jesse

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 23, 2008 11:16 PM
  • User201647903 posted

    FINALLY! After getting this email from Godaddy:

    "We have received notice that you are having difficulties using a custom form mailer on OURHEARTBREAKINGCHOICES.COM . We have reviewed your web.config file and it appears it is not setup correctly. In order for mail to be sent from a shared hosting account, you will need to use "relay-hosting.secureserver.net" for the SMTP server; no other SMTP server will work. Additionally, you do not need to specify a user name or password when using this SMTP server."

    It finally works!  What I did was to completely remove my user name and password info from my web.config file so the cod is now just:  

    <system.net>
         <mailSettings>
    	<smtp>
    	   <network host="relay-hosting.secureserver.net" />
    	</smtp>
         </mailSettings>
    </system.net>

    and I set my VB.Net code to:  Dim smtp As New SmtpClient("relay-hosting.secureserver.net")  -  I never did get it to work on my local host, but id DOES work once uploaded to Godaddy!

    Jesse - your help has been invalueable with this - Thank you!!!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 24, 2008 9:37 PM

All replies

  • User-1161063654 posted

    Dim Mailobj As System.Net.Mail.SmtpClient.Send(lsFrom, lsTo, lsSub, lsBody)

    Try and split this line up.   

    Dim Mailobj As System.Net.Mail.SmtpClient

    Then after you populate all the fields with the to, from, subject, and message body execute the Send method as such:

      

    Mailobj.Send(lsFrom, lsTo, lsSub, lsBody)
    Hope this provides some insight.
    - Jesse 
    Sunday, September 21, 2008 11:05 PM
  • User201647903 posted

    Hope this provides some insight.

    It does, thank you - I had (finally) found some information on how to set up my email last night on-line, but I am having one major issue: no matter how I seem to declare my TO address, I keep getting errors!  If I declare it as shown in my original code Dim lsTo As String = info@ourheartbreakingchoices.com then I get the error: "Property 'To' is 'ReadOnly'." and if I declare it as I found on-line last night as Dim ToAddress As New MailAddress(info@ourheartbreakingchoices.com) it gives me the error: "Value of type 'System.Net.Mail.MailAddress' cannot be converted to string."  (btw - the forum won't allow me to put the quotes around the email address - it automatically converts it to a link - even when I "break" the link!) So, I could use a little help in getting that part to work correctly.

    Below is my new code from the blog I fould last night: http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1251&count=no 

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
    
      Dim MailMsg As New System.Net.Mail.MailMessage()
      Dim lsFrom As String = ""
      Dim lsSub As String = ""
      Dim lsBody As String = ""
    
      lsFrom = txtFrom.Text.ToString()
      lsSub = txtSubject.Text.ToString()
      lsBody = txtMessage.Text.ToString()
    
      Dim FromAddress As New MailAddress(lsFrom)
      Dim ToAddress As New MailAddress("info@ourheartbreakingchoices.com")
    
      If lsFrom = "" Then
       lblMessage.Text = "Please enter a return email address."
       lblMessage.Visible = True
      End If
    
      If lsSub = "" Then
       lblMessage.Text = "Message has no Subject. Please enter a Subject."
       lblMessage.Visible = True
      End If
    
      If txtMessage.Text = "" Then
       lblMessage.Text = "You have not entered a message, please enter your message and retry."
       lblMessage.Visible = True
      ElseIf txtMessage.Text <> "" Then
       If lsFrom = "" Then
            lblMessage.Text = "Please enter a return email address."
            lblMessage.Visible = True
       ElseIf lsSub = "" Then
    	lblMessage.Text = "Message has no Subject. Please enter a Subject."
    	lblMessage.Visible = True
       Else
    	Dim mailSender As New System.Net.Mail.SmtpClient()
    	mailSender.Send(lsFrom, ToAddress, lsSub, lsBody)
    
    	MailMsg.From = FromAddress
    	MailMsg.To = ToAddress
    	mailSender.Host = "(relay-hosting.secureserver.net)"
    
    	Try
    	  mailSender.Send(MailMsg)
    	  lblMessage.Text = "<span style='font-style: italic; font-weight: bold; 
                color: #8981b3;'<br />Thank you!<br />We have received your request<br />
                and will be responding shortly.<br />"
    	  lblMessage.Visible = True
    	Catch ex As Exception
    	  lblMessage.Text = ex.Message
    	End Try
       End If
      End If
    End Sub
    
     The issues here are those I outlined above - and, I am not sure if I have the mailSender.Host = (Hostname) correct.  I got that from my hosting service, but I really don't think it's correct - their "Help" deparetment really sucks! 
    Monday, September 22, 2008 8:17 AM
  • User-1161063654 posted

     

    MailMsg.To = ToAddress

    Try this:  

    MailMsg.To.Add(ToAddress)

    Also, I see that you are using GoDaddy for your hosting, right?  Check out this post that someone else had regarding using the email thru GoDaddy: http://forums.asp.net/p/1321642/2628777.aspx#2628777

    I also, add the email host information in the web.config for the application.

     

    <system.net>
        <mailSettings>
          <smtp>
            <network host="relay-hosting.secureserver.net" userName="youremail@email.com" password="abc123" />
          </smtp>
        </mailSettings>
      </system.net>

      Obviously, change youremail@email.com / abc123 to the specific address and password you want to use for authentication.

    Hope this helps!

    - Jesse

    Monday, September 22, 2008 9:51 AM
  • User201647903 posted

    Thank you so much for your response!  I made all those changes, and uploaded them to Godaddy, but when I go to my site to send the email - it isn't working.  I have a message that will display for the user when the email is sent successfully, and it is not displaying.  I tested this on my local host, and it did not work either, but I thought it might be due to not having my SMTP set up on my local host.  Here is the error message I get when running it in debugger:

    System.Net.Mail.SmtpException was caught
      Message="Failure sending mail."
      Source="System"
      StackTrace:
           at System.Net.Mail.SmtpClient.Send(MailMessage message)    at _Default.btnSend_Click(Object sender, EventArgs e) in C:\inetpub\wwwroot\ourheartbreakingchoices\Default.aspx.vb:line 59
      InnerException: System.Net.WebException
           Message="The remote name could not be resolved: '(relay-hosting.secureserver.net)'"

    Any help/suggestions are MUCH appreciated! 

    Monday, September 22, 2008 12:29 PM
  • User-1161063654 posted

    Instead of relay-hosting.secureserver.net try, smtpout.secureserver.net".

    relay-hosting.secureserver.net is the server for the shared hosting environments in GoDaddy.com.  smtpout.secureserver.net is the server for dedicated hosting solutions.  HOWEVER, I did notice that when I test send emails thru my local development environment I need to use smtpout.secureserver.net; but use relay-hosting.secureserver.net for my production (GoDaddy hosted).

    Hope this helps!

    - Jesse

    Monday, September 22, 2008 1:07 PM
  • User201647903 posted

    Well, obviously I am still doing something wrong, because it still does not work!  I am litterally at my wits end!  I have Googled this and found all kinds of information, but none of it seems to work for me.  Some say that you can't use the System.Net.Mail with Godaddy, yet when I change that back to System.Web.Mail, it gives me errors on the class MailAddress!

    I have tried looking up the atcual SMTP address from Godaddy, using it, using what they sent me, using what you have suggested - nothing works!  I am extremely frustrated and honestly don't know where else to turn!  I am getting the message: Message="The remote name could not be resolved: '(relay-hosting.secureserver.net)'" even when I change this to the smtpout.secureserver.net I get the same error. I have also made the chages to the Web.config file, and I just don't know what else to do.  Any suggestions would be greatly appreciated!  TIA

    Monday, September 22, 2008 6:42 PM
  • User201647903 posted

    Well, the problem is definitely with Godaddy!  Here is their "unhelpful" response to my request for information on how to get my email to work on their servers using Imports System.Net.Mail:

    Dear Coleen,

    Unfortunately we do not support the System.Net.Mail function, but we do support the System.Web.Mail function. You can not use our standard smtpout server. You must use relay-hosting.secureserver.net and if you have configured a port it uses any any way it must use port 25. This is all the support we can give on assisting third party scripting. Thank you for understanding this matter.

    At this point, I m so mad at Godaddy that I am ready to switch hosting services!  If anyone has any good recommendations for a hosting service that will allow me to host multiple domains under one account (that is, I will only be charged for ONE hosting account, but will be able to have several different web sites with different URL's hosted under ONE account -- with ONE fee.)  That is the ONLY reason that I like Godaddy - I am able to host multiple sites under one account, and as long as I don't go over my allotted storage space (all my web sites are only about 3mb a piece - I only have one that is 12mb!) If anyone has a suggestion for a hosting service that has this same feature, I would appreciate a recommendation!

    Also, I would really appreciate it if anyone has links or can point me to a good example of how to get this to work using VB.Net and the Imports System.Web.Mail class instead of the PROPPER way using System.Net.Mail, which Godaddy does NOT Support, I would again appreciate it! 

    Thanks Jesse, for all of your help - I appreciate it, and am sure that if Godaddy actually supported .Net.Mail, that it would work!

    Tuesday, September 23, 2008 9:58 AM
  • User-1161063654 posted

    No problem at all, glad that I could provide some insight.

    Really?  That is a really strange answer because I use System.Net.Mail on my application hosted in GoDaddy and I have no issues w/sending mail thru their system.  I'm a little baffled at their response - my code is below. 

    MailAddress fromAddress = new MailAddress(strEmailAddress, strName);
    
    MailMessage msgContactUs = new MailMessage();
    msgContactUs.From = fromAddress;
    msgContactUs.To.Add("email@email.com");
    msgContactUs.Subject = strSubject;
    msgContactUs.Body = strMessage;
    
    SmtpClient smtp = new SmtpClient();
                
    smtp.Send(msgContactUs);

     I use the relay server in production (when I have my code sitting on GoDaddy) and smtpout when I am developing locally.  I don't quite understand how that works, but ok!  I wouldn't think anything w/proxies would be an issue since you are hosting internally to GoDaddy.  Do you have the email forwarding options configured for the relay?

    I would argue their response since I am using System.Net.Mail perfectly fine.

    Tuesday, September 23, 2008 11:07 AM
  • User201647903 posted

    Thanks Jesse [:)]

    I had to put it away for today and go work on something else...before I called Godaddy any thing I might regret!   I do have a question about your web.config file... do you use the relay-hosting.secureserver.net or the smtpout.secureserver.net in the web.config?  I have changed the code so many times, that now I don't know which set of code is causing me the problem!

    Thanks again for all of your help!

    Tuesday, September 23, 2008 10:14 PM
  • User-1161063654 posted
    No problem at all... here is a snapshot of my web.config:  
    <system.net>
        <mailSettings>
          <smtp>
            <!--<network host="relay-hosting.secureserver.net" userName="email@email.com" password="abc123" />-->
            <network host="smtpout.secureserver.net" userName="email@email.com" password="abc123" />
          </smtp>
        </mailSettings>
      </system.net>

     The above configuration is for when I am developing locally.  Like I said, I do not know exactly why the dedicated hosting server works from my personal development machine.

     My web.config that is deployed on GoDaddy as the smtpout server commented out and the relay-hosting instance active.

    When you use the web.config method of specifying the host information you do not need to use the SmtpClient.Host property.  So you can remove that line from your code and just build your mail message object and use SmtpClient.Send(MailMessage).

    Hope this helps a little more.

    - Jesse

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 23, 2008 11:16 PM
  • User201647903 posted

    Thanks Jesse!

    Well, I am making progress... with Dim smtp As New SmtpClient("smtpout.secureserver.net") now my error is "Mailbox name not allowed. The server response was: Sorry, that domain isn't in my list of allowed rcpthosts."

    So I changed the SMTP code to:  Dim smtp As New SmtpClient("relay-hosting.secureserver.net") and I get the error message: " Message="Mailbox name not allowed. The server response was: sorry, relaying denied from your location [my IP address] (#5.7.1)"

    Of coures my IP address is my personal IP address which I removed for my privacy [;)]

    Now I'll go try changing it on the Host Service and testing it there - I don't think I have my laptop set up to correctly display email options, so I think I have to test this live.  But if you have any other suggestions for why I am getting these errors, I'd be eternally grateful!  Thanks again for all your help.

    Wednesday, September 24, 2008 10:03 AM
  • User-1161063654 posted

    No problem at all.  That's great news.

    I'm pretty sure w/GoDaddy in their email services section you have to configure the email address that you want to use for relay.  That is why you might be getting the "Mailbox name not allowed error".  I'd check in the GoDaddy email admin that you have an account for relay.  If you do, use that account for the authentication purposes; and if you don't add an email address for relay.

    Like I said before when I work locally on my machine I use smtpout.secureserver.net and relay-hosting.secureserver.net when the app is live.

    Hope this helps some more!

    - Jesse

    Wednesday, September 24, 2008 10:59 AM
  • User201647903 posted

    FINALLY! After getting this email from Godaddy:

    "We have received notice that you are having difficulties using a custom form mailer on OURHEARTBREAKINGCHOICES.COM . We have reviewed your web.config file and it appears it is not setup correctly. In order for mail to be sent from a shared hosting account, you will need to use "relay-hosting.secureserver.net" for the SMTP server; no other SMTP server will work. Additionally, you do not need to specify a user name or password when using this SMTP server."

    It finally works!  What I did was to completely remove my user name and password info from my web.config file so the cod is now just:  

    <system.net>
         <mailSettings>
    	<smtp>
    	   <network host="relay-hosting.secureserver.net" />
    	</smtp>
         </mailSettings>
    </system.net>

    and I set my VB.Net code to:  Dim smtp As New SmtpClient("relay-hosting.secureserver.net")  -  I never did get it to work on my local host, but id DOES work once uploaded to Godaddy!

    Jesse - your help has been invalueable with this - Thank you!!!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 24, 2008 9:37 PM