Unable to send mail from ASP.NET

Discussion in 'ASP.NET / ASP.NET Core' started by Klint Price, Sep 1, 2014.

  1. I'm getting a "No unauthenticated relaying permitted" when trying to send mail from my asp.net application

    Dim mailMsg As New MailMessage()

    ' To
    mailMsg.[To].Add(New MailAddress(emailTo, emailToName))

    ' From
    mailMsg.From = New MailAddress(emailFrom, emailFromName)

    ' Subject and multipart/alternative Body
    mailMsg.Subject = subject
    Dim text As String = textBody
    Dim html As String = htmlBody
    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, Nothing, MediaTypeNames.Text.Plain))
    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, Nothing, MediaTypeNames.Text.Html))

    ' Init SmtpClient and send
    Dim smtpClient As New SmtpClient("66.96.135.114") 'using IP address until I can get this to work
    Dim credentials As New System.Net.NetworkCredential("MYUSERID", "MYPW")
    smtpClient.UseDefaultCredentials = False
    smtpClient.Credentials = credentials
    smtpClient.EnableSsl = False
    smtpClient.Port = 25
    'or you can use 587

    smtpClient.Send(mailMsg)


    The error I get is as follows:

    Server Error in '/' Application.

    System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: bosauthsmtp10: Host 96.31.33.29: No unauthenticated relaying permitted
    at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
    at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
    at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    at etc.SendGridMail.SendMail(String emailTo, String emailToName, String emailFrom, String emailFromName, String subject, String textBody, String htmlBody) in **********:line 42

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Net.Mail.SmtpException: System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: bosauthsmtp10: Host 96.31.33.29: No unauthenticated relaying permitted
    at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
    at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
    at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    at etc.SendGridMail.SendMail(String emailTo, String emailToName, String emailFrom, String emailFromName, String subject, String textBody, String htmlBody) in *******
    :line 42

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [SmtpException: System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: bosauthsmtp10: Host 96.31.33.29: No unauthenticated relaying permitted
    at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
    at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
    at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    at etc.SendGridMail.SendMail(String emailTo, String emailToName, String emailFrom, String emailFromName, String subject, String textBody, String htmlBody) in
    ****
    System.Web.UI.WebControls.CreateUserWizard.OnCreatedUser(EventArgs e) +116
    System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser() +342
    System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e) +110
    System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e) +401
    System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object source, EventArgs e) +119
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
    System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +114
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +252
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446
     
  2. Anyone?
     
  3. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    That looks like the web server's address. You don't need to authenticate when using the web server's SMTP server to send email.
     
    mjp likes this.
  4. I've changed to use localhost, but after sending an email still have not received anything. Is there something I need to configure in the admin panel?

    Try
    Dim mailMsg As New MailMessage()

    ' To
    mailMsg.[To].Add(New MailAddress(emailTo, emailToName))

    ' From
    mailMsg.From = New MailAddress(emailFrom, emailFromName)

    ' Subject and multipart/alternative Body
    mailMsg.Subject = subject
    Dim text As String = textBody
    Dim html As String = htmlBody
    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, Nothing, MediaTypeNames.Text.Plain))
    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, Nothing, MediaTypeNames.Text.Html))

    ' Init SmtpClient and send
    Dim smtpClient As New SmtpClient("127.0.0.1")
    smtpClient.UseDefaultCredentials = False
    smtpClient.EnableSsl = False
    smtpClient.Port = 25
    'or you can use 587

    Dim s As Stream = New MemoryStream(ByteArray)

    Dim attachment As New Attachment(s, "FullReport.pdf")
    mailMsg.Attachments.Add(attachment)


    smtpClient.Send(mailMsg)
    Catch ex As Exception
    Throw New SmtpException(ex.ToString)
    End Try
     
  5. As a side note, this site is not yet 'live' in that I have not cut over DNS. I am testing the functionality prior to cutting over DNS, and this is the last UAT case.

    My testing is coming from: http://mypersonalp.web###.discountasp.net
     
  6. It's still not clear to me where this ASP.NET application is hosted.
    • If your ASP.NET web application is hosted on the shared DASP web server then RayH's advice is good and sending email through that local web servers' SMTP server will work as expected.
    • If this ASP.NET web application hosted anywhere other than the shared DASP web server then that advice working as expected is dependent on whether there's a locally installed and correctly configured SMTP server on that web server.
    These 2 cases aside, there's a method that I used to use for both SMTP testing and subsequent "real world" use that uses the DASP SMTP server that works everywhere: populate the <system.net><mailSettings><smtp> element in the web.config and don't bother specifying any SMTP hostname / address or credentials in code at all (the System.Net.Mail.SmtpClient class automatically uses the configuration you specify in the web.config) e.g.

    Code:
    <?xml version="1.0"?>
    <configuration>
    ..
    ..
    <system.net>
            <mailSettings>
                <smtp from="someaddress@[YOUR_DOMAIN]" deliveryMethod="Network">
                    <network host="smtp.[YOUR_DOMAIN]" userName="[SMARTERMAIL_USER_NAME]" password="[PWD_FOR_SMARTERMAIL_USER_NAME]" />
                </smtp>
            </mailSettings>
        </system.net>
    </configuration>
    This example assumes that you're using the DASP SMTP server to send email through. If not, substitute alternate configuration for whatever SMTP server you're actually using. More info here: http://msdn.microsoft.com/en-us/library/ms164240(v=vs.110).aspx

    Assuming you're using the DASP SMTP server aligned to the sample configuration in this post:
    • someaddress@[YOUR_DOMAIN] is a smarter mail email address for a configured smarter mail account to be used as a return address
    • smtp.[YOUR_DOMAIN] is the SMTP server host name as documented in the control panel in the 'Email Information' section: https://my.discountasp.net/account-info.aspx
    • [SMARTERMAIL_USER_NAME] is the user name of a smarter mail user account that will be used to send email through the DASP SMTP server
    • [PWD_FOR_SMARTERMAIL_USER_NAME] is the password for the [SMARTERMAIL_USER_NAME] account
    This method will also work if you really do want to specify these values directly in your code passing them directly to an instantiated System.Net.Mail.SmtpClient class instance. IMO the web.config option is more flexible.
     
    mjp likes this.
  7. ok, this is what I have now, but still not receiving email:

    Code:
    <system.net>
        <mailSettings>
          <smtp from="[email protected]" deliveryMethod="Network">
                  <network host="localhost" port="25" userName="[email protected]" password="TheUsersPassword" />
          </smtp>
        </mailSettings>
      </system.net>
    and the code is:

    Code:
    Dim mailMsg As New MailMessage()
    
    ' To
                mailMsg.[To].Add(New MailAddress(emailTo, emailToName))
    
    ' From
                mailMsg.From = New MailAddress(emailFrom, emailFromName)
    
    ' Subject and multipart/alternative Body
                mailMsg.Subject = subject
    Dim text As String = textBody
    Dim html As String = htmlBody
                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, Nothing, MediaTypeNames.Text.Plain))
                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, Nothing, MediaTypeNames.Text.Html))
    
    ' Init SmtpClient and send
    Dim smtpClient As New SmtpClient
    smtpClient.EnableSsl = False
    
    smtpClient.Send(mailMsg)
    
     
  8. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Remove the username and password in your web.config file, and it should work.
     
    mjp likes this.
  9. I removed both the username and password from the web.config, and it still does not work.
     
  10. You've still not clarified where this ASP.NET web application is hosted. The only hint towards environmental clarification was your post #5 and let's face it as far as we know, the UAT environment you've mentioned could mean that you're currently hosted on a server in your garden shed.
    • If your ASP.NET web application is hosted on the shared DASP web server then RayH's advice is good and sending email through that local web servers' SMTP server will work as expected. If this is the case and it's really not working I suggest you raise a support ticket to enable an investigation to be performed.
    • If this ASP.NET web application hosted anywhere other than the shared DASP web server then that advice working as expected is dependent on whether there's a locally installed and correctly configured SMTP server on that web server.
    My prior post in this thread described configuration that will work everywhere regardless of the environment the application is hosted in so long as that environment has network access. That configuration works everywhere because it doesn't use localhost as the SMTP server host, it uses a fully qualified DNS hostname of an SMTP host that should resolve and work in any network regardless of environment.

    I'm not suggesting RayH's advice is wrong because it's not so far as the DASP web server configuration is concerned. The localhost suggestion working as expected does however have an environmental slant - it makes those assumptions mentioned in the bullet points above. Your current web app host might actually fit one of these profiles. Or perhaps not.
     
    martino, mjp and RayH like this.
  11. When I say UAT, it is a site hosted at DASP, but the production DNS is pointed elsewhere. Once I get this last step working so it sends mail, I will be cutting over DNS and making this DASP site live.

    I'm at a loss. The various sample codes do not seem to be working. I have a ticket open (which keeps getting closed because I do not reply within 24 hours), but no progress has been made.
     
  12. martino

    martino DiscountASP.NET Staff

    If you're using localhost on our web server in your web application. It should work.

    What is the error message you're getting now when sending via localhost?

    Can you also try sending the email message to a completely different email address that you normally use to test the script?
     
  13. martino

    martino DiscountASP.NET Staff

    I just read the ticket you have currently open.

    You have an SPF record for the domain name that you're using as the "FROM" address in your code. I'm sure you know what an SPF record does for a domain name since you might have set it up. Just in case you don't know it basically tells the whole world.... "Hey only accept email messages from these IP numbers".

    When you try to use localhost it's using a different IP number to send the email message. So if the recipient email server cares about SPF records. It will send the email message you sent to the Junk/Spam folder. OR it might not even deliver it.

    Just for the sake of trouble shooting. Try changing the "From" field to a different email address that doesn't have a SPF record. Then try to send the email message again.
     
    mjp likes this.

Share This Page