-1

Hello I'm building a new site. But my usual mail code working only sometimes with this site.I was able to send mail within 01.00 - 01.10 and get a error "Failure to send mail" for 15 minutes.And now I'm able to send again. Its probably a issue with my hosting firm but can you review the code just to be safe? Thanks in advance.

[HttpPost]
    public string processData(PersonDTO p)
    {
        try
        {
            var bdy = string.Format("Alınacak Yer: {0}\nAd Soyad: {1}\nTarih: {2}\nSaat: {3}\nBırakılacak Yer:{4}\nEmail: {5}\nUçuş No: {6}\nTelefon: {7}\nAdres Tarifi: {8}\nYolcu Sayısı: {9}", p.PickupSite, p.Name, p.Date, p.Time, p.DropSite, p.Email, p.FlightNumber, p.Phone, p.AddressLocation, p.NumberOfPassengers);
            var msg = new MailMessage();
            msg.From = new MailAddress("system@globalairporttransfer.com");
            msg.To.Add("info@globalairporttransfer.com");
            msg.Subject = "Yeni Istek";
            msg.Body = bdy;
            msg.IsBodyHtml = false;
            var client = new SmtpClient("mail.globalairporttransfer.com", 25);
            client.Credentials = new NetworkCredential("system@globalairporttransfer.com", "mypasswordishere");
            client.EnableSsl = false;
            client.Send(msg);
            return "Kaydınız alındı. En kısa sürede ileşime geçeceğiz.";
        }
        catch (SmtpException e){return "Mesaj Gönderilemedi ! Hata Mesajı: \n" +e.Message;}
        catch (SocketException e) { return "Mesaj Gönderilemedi ! Hata Mesajı: \n" + e.Message; }
        catch (FormatException e) { return "Mesaj Gönderilemedi ! Hata Mesajı: \n" + e.Message; }
    }
}
  • Although this probably isn't the cause of your problem, you should be aware that SmtpClient and MailMessage implement IDisposable, and so should be either disposed, or wrapped in a using block. In the case of SmtpClient, it is possible to exhaust underlying resources if you never dispose of them (which can then cause further mail sends to fail). – hatchet - done with SOverflow Sep 01 '15 at 22:53
  • I really didnt understand what are you trying to say ? Can you show me an example if you can ? Thanks – lord-angelus Sep 01 '15 at 23:02
  • see http://stackoverflow.com/questions/2781103/c-sharp-how-to-correctly-dispose-of-an-smtpclient/2781118#2781118 – hatchet - done with SOverflow Sep 01 '15 at 23:06

1 Answers1

0

After fighting several hours with my hosting firm I proved that there is a problem with their end. After some configuration from their end I noticed Dns couldnt be resolved. If you have that problem you can try write ip adress of smtp instead of name.. I hope it helps some

var client = new SmtpClient("Ip of the smtp server", 25);