Failure to Send SMTP Mail

Discussion in 'Silverlight' started by rlcraven, Oct 14, 2010.

  1. Hello,

    There is a problem sending email from my SL4 application when it's deployed on the DASP server. When running on the development server, the messages send correctly.

    I change the host to "localhost" when the system is deployed. No error is being captured, but the recipient does not receive the message. I have tried using my postmaster account and that did not make a difference.

    I'm opening a support ticket as well since this is the first time sending email from the application. However, I tend to believe it's a code error.

    Also, in smartmail there is no record of smtp activity after attempting to send the message.

    Thank you for any suggestions!

    Randy


    Code:
       
            Dim sEmailAddress as string = "[email protected]"     
            Dim oMessage As New MailMessage
    
            Try
                With oMessage
                    .From = New MailAddress("[email protected]")
                    .To.Add(sEmailAddress)
                    .Subject = "This is The Subjetct"
                    .Body = "This is the body"
                End With
                Dim oClient As New SmtpClient
                With oClient
                    '.Host = "smtp.mydomain.com"      ' development server
                    .Host = "localhost"                        ' deployment
                    '.Credentials = New System.Net.NetworkCredential("[email protected]", "mypassword")     ' only used in development
                    .Send(oMessage)
                End With
    
            Catch ex As SmtpException
    
                Throw New ApplicationException("An Error Occurred Sending the Email Message to " & oMessage.To.ToString & vbCrLf & "The Error Message is: " & ex.Message)
    
    
            End Try
     
  2. dmitri

    dmitri DiscountASP.NET Staff

    When you are sending messages through a web application, please specify "localhost" as the SMTP server with user name and password left blank to send messages through the local mail relay. When you are sending emails with localhost smtp server, you are not using your SmarterMail accounts and there will be no smtp log entries. Please see this KB article for sample code which sends emails from web application.
     
  3. Thank You for your message, Dimitri.

    I am already using "localhost", so that is not the issue.

    Thanks for the code sample. My code is not running in code behind but I think that it's functionally equivalent to the example.

    I'm using a Silverlight Web Service to send the mail (silverlight clients cannot access system.net.mail) and calling it from the client. I'm going to try a different method on the server like a generic ashx handler and see if that makes a difference.

    Thanks,

    Randy
     
  4. Bruce

    Bruce DiscountASP.NET Staff

    Silverlight do not really report the error message. I recommend you try to capture the error into a log file on the server.
     
  5. Thanks for the message, Bruce. I'll work on this.

    I don't have experience with logging on a web server, do you have a suggestion or link for some code to get started with?

    Thanks again.

    Randy
     
  6. Please clarify. Is this code running within the Silverlight application itself or is the Silverlight application consuming a service on the server where this runs?

    The value of localhost will be different depending on the above. As for using smartermail (smtp.your-hosted-domain.com), the issue is most commonly related to authentication. Try taking your code and running it in a regular asp.net application hosted on the server to confirm it works.
     
  7. Thanks for your reply, Chuck.

    The code to send mail is running on the server as a WCF Service. The client calls the service, sending params for to address, subject, and body. (For ease of reading the code I posted doesn't show using passed parameters but rather local vars)

    When I run on the development server I use: .Host = "smtp.mydomain.com" plus credentials.

    When I deploy I change this to .Host = "localhost" and no credentials.

    Thanks,

    Randy
     
  8. Right now I am looking into how the WCF services are invoked, something about endpoints... I have another WCF service that isn't working either, one that worked in the development server. So I suspect the problem lies in there somewhere.

    Of course, having the correct SMTP setup would be needed also...

    Randy
     
  9. Problem solved -- and lesson learned about how to call a web service from the SL client. My code for sending mail was fine, but calling the web service from the client was failing.

    Thanks to Joe for today's posting "Silverlight 4 with secured WCF services", studying that was the ticket.

    The service worked locally I believe, because the systems sets up a default binding and endpoint for the development server. To successfully call in deployment one has to set up an enpoint address that is a valid path on the web server (or something like that).

    As previously noted, to send mail with DASP one only has to reference "localhost" as the SMTP host, and no authentication is required.

    Here is the code I used to call the webservice. The binding name is system generated and can be found in the ServiceReferences.ClientConfig file.

    Code:
            Dim baseUri As New Uri(System.Windows.Application.Current.Host.Source, "..")
            oMailer = New DocMailerServiceClient("CustomBinding_DocMailerService", New EndpointAddress(baseUri.AbsoluteUri & "WebServices/DocMailerService.svc"))
    Randy
     
  10. I'm glad you managed to fix your problem. The sample I posted was intended to demonstrate something else but looks like it included enough for you to solve this one too.
     
  11. Yes, understood that this was not the purpose of your post. I'm looking forward to studying your example in more detail.

    Randy
     

Share This Page