Resolved Sending Mail via APP

PinLui

Member
Joined
Mar 22, 2020
Messages
5
Programming Experience
Beginner
Hello,

My router does not have any DynDNS function so I thought I can run a little console app on my server who sends me an email with my external IP.
this is my main:
C#:
private static void Main(string[] args)
        {
            bool Alive = true;
            while (Alive)
            {
                Alive = MainCode();
            }
        }

        private static bool MainCode()
        {
            Console.Clear();
            Console.Write("\r\nInput: ");

            switch (Console.ReadLine())
            {
                case "rdp":
                    Openrdp();
                    return true;

                case "mail":
                    _ = new Mailmng();
                    return true;

                case "exit":

                    return false;

                default:

                    return true;
            }
        }

this is my Mailmng class:
C#:
 public class Mailmng
    {
        public Mailmng()
        {
            Timer t = new Timer(TimeSpan.FromMinutes(2).TotalMilliseconds);
            t.AutoReset = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(Mail);
            t.Start();
        }

        public static string GetIP()
        {
            string url = "http://checkip.dyndns.org";
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string response = sr.ReadToEnd().Trim();
            string[] a = response.Split(':');
            string a2 = a[1].Substring(1);
            string[] a3 = a2.Split('<');
            string a4 = a3[0];
            return a4;
        }

        public void Mail(object sender, ElapsedEventArgs e)
        {
            string to = "******";
            string from = "*******";
            try
            {
                using (MailMessage mm = new MailMessage(from, to))
                {
                    mm.Subject = "ServiceAPP";
                    mm.Body = GetIP();
                    mm.IsBodyHtml = false;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                    NetworkCredential NetworkCred = new NetworkCredential(from, "*********");
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    smtp.Port = 587;
                    Console.WriteLine("Sending Email......");
                    smtp.Send(mm);
                    Console.WriteLine("Email Sent.");
                    //System.Threading.Thread.Sleep(3000);
                    //Environment.Exit(0);
                }
            }
            catch (SmtpException ex)
            {
                throw new ApplicationException
                  ("SmtpException has occured: " + ex.Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

eventually, I want this app to send me the IP via mail every 6h (for debugging its set to 2mins), but I have 2 Problem right now:

1.) The first email goes out perfectly but after that, no email gets sent. As soon as I type anything in the console all emails that should have been sent go out at once.
2.) Every email sent from the App increases the used RAM


I know there are other ways to do this. But I thought it would be a good beginner's project, and learn more about Timers.
 
Last edited:
My router does not have any DynDNS function so I thought I can run a little console app on my server
Double check your Terms of Service with your ISP. If you use Cox, Verizon, Time Warner, or Charter, they explicitly say that you may NOT run a server from your home.
 
What does having a shitty router have to do with you violating the terms of service? That is like telling the police officer giving you a ticket for doing 50 in a 25 zone that you are allowed to speed because your beat up Pinto has broken brakes.
 
What does having a shitty router have to do with you violating the terms of service? That is like telling the police officer giving you a ticket for doing 50 in a 25 zone that you are allowed to speed because your beat up Pinto has broken brakes.
I can have as many servers as i want from my home. no limitations
 
Last edited:
If you can afford that many servers, then you can likely afford a better router that has DynDNS support built in. If you are forced to use your ISP's router, you could just put another router behind your ISP's router and set that second router into the DMZ of the first router.
 
Anyway, the following seems to work fine for me. The blocking of happening on your thread callback seems to be caused by something else. I suggest trying to set some breakpoints and/or verifying the type of Timer you are using:
C#:
using System;

namespace ConsoleTest
{
    class ConsoleShell
    {
        public static void Main()
        {
            var t = new System.Timers.Timer(TimeSpan.FromSeconds(5).TotalMilliseconds) { AutoReset = true };

            Console.WriteLine("Type 'doit' to start timer. 'exit' to stop parsing loop.");

            bool quit = false;
            do
            {
                switch (Console.ReadLine().ToLower())
                {
                    case "doit":
                        Console.WriteLine("Starting timer.");
                        t.Elapsed += (o, e) => Console.WriteLine($"{DateTime.Now}: Doing it.");
                        t.Start();
                        break;

                    case "exit":
                        quit = true;
                        break;
                }
            } while (!quit);

            t.Stop();
            Console.WriteLine("Done.");
            Console.ReadKey();
        }
    }
}
 
Anyway, the following seems to work fine for me. The blocking of happening on your thread callback seems to be caused by something else. I suggest trying to set some breakpoints and/or verifying the type of Timer you are using:
C#:
using System;

namespace ConsoleTest
{
    class ConsoleShell
    {
        public static void Main()
        {
            var t = new System.Timers.Timer(TimeSpan.FromSeconds(5).TotalMilliseconds) { AutoReset = true };

            Console.WriteLine("Type 'doit' to start timer. 'exit' to stop parsing loop.");

            bool quit = false;
            do
            {
                switch (Console.ReadLine().ToLower())
                {
                    case "doit":
                        Console.WriteLine("Starting timer.");
                        t.Elapsed += (o, e) => Console.WriteLine($"{DateTime.Now}: Doing it.");
                        t.Start();
                        break;

                    case "exit":
                        quit = true;
                        break;
                }
            } while (!quit);

            t.Stop();
            Console.WriteLine("Done.");
            Console.ReadKey();
        }
    }
}
thx, I will look into this
 
Back
Top Bottom