Sending email from desktop application usin DASP smtp server

Discussion in 'Email' started by OlgaE, Dec 2, 2010.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. I have hosting account int DASP.net
    I want that my desktop application in some cases will send emails using DASP smtp server.
    This is not work. I don't receive any exceptions. Just email is not sent.
    What's wrong
    The code is:

    Code:
            public void SendEmail(string strSubject, string strTo, string strName, string strMessage)
            {
                string strTextToSend = PrepareMessage(strMessage, strName, strTo);
    
                try
                {
                    MailMessage msg = new MailMessage();
                    string strFrom = "[email protected]";
                    string strDisplayName = "dname";
                    string strUser = "uname";
                    string strPwd = "pwd";
                    string strSmtpClient = "smtp.mydomain.com";
    
                    MailMessage mail = new MailMessage();
                    NetworkCredential cred = new NetworkCredential(strUser, strPwd);
    
                    mail.To.Add(strTo);
    
                    mail.From = new MailAddress(strFrom, strDisplayName, System.Text.Encoding.UTF8);
                    mail.Subject = strSubject;
                    mail.SubjectEncoding = System.Text.Encoding.UTF8;
                    mail.Body = strTextToSend;
                    mail.BodyEncoding = System.Text.Encoding.UTF8;
                    mail.IsBodyHtml = true;
                    mail.Priority = MailPriority.Normal;
    
                    SmtpClient smtp = new SmtpClient(strSmtpClient);
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = cred;
                    smtp.Host = "localhost";
                    smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                    try
                    {
                        smtp.Send(mail);
                    }
                    catch (Exception E)
                    {
                        string strErrorMessage = E.Message;
                        MessageBox.Show(strErrorMessage);
                    }
                    finally
                    {
                        mail.Dispose();
                    }
    
                }
                catch (Exception ex)
                {
                    string strRes = ex.Message;
                }
            }
    
     
  2. The outer block in code you posted is handling and swallowing exceptions without throwing them so you never will see a problem when something goes wrong there; that's one problem so change that to see if it helps you to see any exception occurring. Other things are delivery method should probably be network not IIS and host should probably be smtp.YourHostedDomainName.com not localhost.
     
  3. The outer block helps me to see the exception in debugger. I don't want it to be thrown in run time.
    This code works on other computer and on mine not:confused: What can be the difference?
    I configured the IIS to rely on 127.0.0.1. It didn't help.
    When I say "I don't receive any exceptions" It means that the line
    Code:
    smtp.Send(mail);
    passed successfully without jumping to catch block..
     
  4. If you're trying to catch errors one part that's missing is where you assign strings.
    Bringing in values for those might fail, on an individual basis.
    You should also right it so you know if a string value is false.
     
  5. Bruce

    Bruce DiscountASP.NET Staff

    If this is a desktop application on your computer, you'll need to set the SMTP server to smtp.yourdomain.com. You can set the SMTP server to localhost only if you have a mail server instance running on your computer.
     
  6. Thank you bruce, but I already set the smtp server to what you say.
    I set to local host only smtp.Host.

    I am sure that at the moment I am sending the email every strings are OK. (I am running it with debugger). There is no any exception on anything. Just the email doesn't arrive.
    If some string was null or something like that there would be exception.
    Exactly the same code is running on other computer without problems.
     
  7. Bruce

    Bruce DiscountASP.NET Staff

    if you do not get any error, it looks like the message is received by the mail server.

    Have you tried send mail to gmail or hotmail to test?
     
  8. I am sending all time to gmail account. And even checked the spam folder. Nothing.:confused:
    And the difference is - different computers with different configuration and internet providers - not the code. On one computer it works and on other doesn't.
     
  9. kstep

    kstep Guest

    Having the same problem.

    For a long while I have been able to send e-mails from a windows service running on my computer using an SMTP host at DASP with no problems. Now suddenly e-mails sent from my local server to the DASP.com SMTP account will not go through.

    Last night I setup a little test on a different machine. At first I was able to send SMTP messages, no problem. Then after about an hour, I could no longer send them from my local windows app.

    Any suggestions are greatly appreciated.
     
  10. mjp

    mjp

    There's nothing on this end that would prevent it after a certain amount of time. You might want to try disabling anti-virus and/or firewalls, etc. to see if that has an effect.

    If it works temporarily, or from one computer and not another, it's something on your end, and it's difficult for us to troubleshoot.
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page