locked
quick smtp question - check if smtp server exists? RRS feed

  • Question

  • User240536124 posted

    Not much luck on searching on this. I have an asp.net website sending notification emails after certain events. It works fine. However, it only works on the production server. My localhost doesn't have an smtp server on it and I don't really want to set one up. Is there a way to check if an SMTP server exists before attempting to send email. It hangs my development machine up and I have to restart IIS when I forget and run certain code locally.

    Is the preferred way just a Try/Catch? Seems like it takes a while to time out locally and checking ahead of time to see if there is actually an SMTP service there would be the way to go. I can't find any examples of checking ahead of time if the server exists. Interested in the best practice for this.

    Thanks,

    Monday, November 19, 2018 5:16 PM

All replies

  • User240536124 posted

    The first link you gave me is the one I used to get everything working to begin with. Good to go there.

    The pickup directory sounds promising: The following example specifies c:\maildrop as the mail pickup directory.

    Does the mail server need configuration to look there to pick it up? SMTP Server is on Godaddy. I don't want to reverse my problem and hose it up on Godaddy. I would just like something that keeps it from erroring out when running local with no SMTP server.

    Right now, I hide something on the .aspx page that lets me turn off sending email when I'm running code locally. But then I'm testing something and forget to set this bit and I have to stop and restart IIS Express (or IIS depending on how I'm running it locally).

    It just looks like there would be some way to test if the SMTP server was there first, send if it is, and do nothing if it isn't.

    Thanks,

    Monday, November 19, 2018 9:42 PM
  • User475983607 posted

    Right now, I hide something on the .aspx page that lets me turn off sending email when I'm running code locally. But then I'm testing something and forget to set this bit and I have to stop and restart IIS Express (or IIS depending on how I'm running it locally).

    Generally you have a development web.config and production web.config. The development web.config has the pickup directory which basically drops the email in a folder on the dev system.   The production web.config has the SMTP configuration.

    It just looks like there would be some way to test if the SMTP server was there first, send if it is, and do nothing if it isn't.

    It's not clear why your development machine cannot connect to an SMTP server and send emails.  Usually, though you want to sanitize all the emails on the dev systems so not to send emails to production users anyway.  The pickup directory works well as it does not send an email it just drops the email in a folder where you can view the email for correctness.

    Monday, November 19, 2018 10:22 PM
  • User240536124 posted

    I was a little lost at first, but I'm with you now. I think I have something else going on, but I didn't want my development machine to actually send email. I just test locally and upload to production, where any emails sent are actually supposed to be sent. HOWEVER, when I finally got the maildropfolder to work, it writes the emails to the folder and still hangs. So I found a fake SMTP server to run locally. It gets the emails and still my code hangs with the message, "waiting for localhost." Tried setting some breakpoints and it appears to work correctly.

    Keep in mind, this exact same code works fine when I upload it to godaddy. Not sure why it hangs locally when I even have an SMTP server going. The server catches the emails and I can read them and they look perfect. I must be missing some little thing. Again, I created my code from your first link, so it is almost identical.

    Also, I examined my code and I do have Try/Catch going on, just like I should (this is code I wrote way back, so I wasn't sure). :)

    I'm going to write some fresh test code in the morning and see if it hangs with a bare minimum of code going on. Will let you know.

    Thanks!

    Monday, November 19, 2018 11:13 PM
  • User-893317190 posted

    Hi jay8anks,

    If you are in development mode, as  mgebhard has suggested , you could use local folder as a mail test , which doesn't need a smtp server.

    You could write the code below in your web.config.

         <system.net>
    <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
    <specifiedPickupDirectory pickupDirectoryLocation="d:\LocalDir"/>
    </smtp>
    </mailSettings>
    </system.net>
    <system.web>

    And you could write your code as follows.

      var smtpClient = new SmtpClient();
                var message = new MailMessage("no-reply@suteki.co.uk", "mike@suteki.co.uk")
                {
                    Subject = "The subject",
                    Body = "The body of the message"
                };
                smtpClient.Send(message);

    After run the code, there should be a file in d:\LocalDir. I open it using outlook.And it shows:

    You could also register a gmail account and use gmail's smtp server.

    SmtpMail oMail = new SmtpMail("TryIt");
                SmtpClient oSmtp = new SmtpClient();
    
                // Your gmail email address
                oMail.From = "ackerlyx@gmail.com";
    
                // Set recipient email address
                oMail.To = "ackerlyx@gmail.com";
    
                // Set email subject
                oMail.Subject = "test email from gmail account";
    
                // Set email body
                oMail.TextBody = "this is a test email sent from c# project with gmail.";
    
                // Gmail SMTP server address
                SmtpServer oServer = new SmtpServer("smtp.gmail.com");
    
                // If you want to use direct SSL 465 port,
                // please add this line, otherwise TLS will be used.
                // oServer.Port = 465;
    
                // set 587 TLS port;
                oServer.Port = 587;
    
                // detect SSL/TLS automatically
                oServer.ConnectType = SmtpConnectType.ConnectSTARTTLS;
    
                // Gmail user authentication
                // For example: your email is "gmailid@gmail.com", then the user should be the same
                oServer.User = "ackerlyx@gmail.com"; //your address
                oServer.Password = "yourpassword";
    
                try
                {
                    Response.Write("start to send email over SSL ...");
                    oSmtp.SendMail(oServer, oMail);
                    Response.Write("email was sent successfully!");
                }
                catch (Exception ep)
                {
                   
                    Response.Write("failed to send email with the following error:");
                    Response.Write(ep.Message);
                }

    The result.

    Best regards,

    Ackerly Xu

    Tuesday, November 20, 2018 4:32 AM
  • User753101303 posted

    Hi,

    Seems really something in your code. It is not supposed to "hang" this way. See what happens with the minimal amount of code or show some  of your real code. What do you do when an exception happens ?

    Tuesday, November 20, 2018 9:47 AM
  • User240536124 posted

    Yes, something in my code. With a minimal amount of code, it does not hang. Something in that one page.

    The <specifiedPickupDirectory> was good to learn about. I had not run into that before. Also, in testing after it was still hanging with the PickupDirectory, I installed PaperCut, a fake smtp server. This is also pretty cool:

    https://github.com/ChangemakerStudios/Papercut

    Both will be useful for testing and not sending out a bunch of crap emails.

    Thanks guys,

    !!!

    Tuesday, November 20, 2018 2:15 PM