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:
this is my Mailmng class:
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.
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: