Get your IP address (Part. 2)

In a preceding post (Get your IP address for remote connection), I motivated the need of an application which could get my IP address in order to use it to remotely connect to my computer with Windows Remote Connection. When the IP was updated by my internet provider, the application updated a Google Drive document and eventually send me an email.

However, the approach used to send an email was hazardous as I needed to set enable  Less Secure Applications in my Google developper account. This approach is not a good one for sure, but I liked to go fast. In this new post, I introduce two major changes in the application. Firstly, I now use the Gmail service API to send an email which is far more secure. And secondly, the application benefits of the WPF technology thanks to the awesome control NotifyIcon.

1- Gmail API

The same way I use the Google Drive API, Gmail API is not more difficult to deal with. We need to instanciate a service provider and consume it further :

The principle is then the same, first my "secrets" are retrieved from the stream of an embedded resource, then my  UserCredentials are instanciated and used to construct the GmailService. This service will then be used to send an email. The approach is quite easy but with little precautions to take :

A couple of things to notice. I defined a MailSecrets class to store my email address, my name and my password. Here again, this is store in an embedded JSON file. The mail message is taken from the open source library AE.Net.Mail. The MailMessage of this class is very close to the System.Net.Mail.MailMessage one at the difference that it is possible to save the message in a stream. This would not have been a trick to implement my own, but it is already done in this library so, why not ! For deeper reasons, refer to This Great Post.


2- WPF NotifyIcon

Tray application were easy to code with Windows Forms, but Notify Icon are not supported out of the box in WPF, which is a shame because of the high customization capabilities of WPF. Fortunely, here again an open source exists : NotifyIcon. The notify icon is simply declared in a resource dictionary as no window is need for this application :

After that, the fun begins ! I customized the controls to the final result :


The control is in two parts. At the top we find the current IP address, and in the second part we have the commands to send an email, update/check the current IP or to exit from the application. Note that I use the word command and not the word event or click action as I use the MVVM design patten in this project. I love this pattern as it is allows to completely separate the view from the viewmodel so we can use the binding power of the WPF at its maximum ! I strongly recommand you to use it too !

You could find all the code of this application on my GitHub account : IpAddressGetTrayApplication

Leave a Reply

%d bloggers like this: