PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 20, 2022

[FIXED] How to send an email when internet connection return back in java?

 April 20, 2022     connection, email, java     No comments   

Issue

I'm working on a project should download some files. If a problem happened to the internet connection, it will be catch an exception. When this happened I should send an email to some people.

I need to send the email, but no internet connection, so I have 2 ideas:

1- trying to send the email, but because I don't have a connection, I need to save the email until the connection return back and send it again.

2- making a thread and check if the connection is stable, and with a condition if the internet is stable I will send the email.

I have another idea to make an infinity loop to check the internet connection and send the email and end the loop when connection is back.

Any one can help with that?


Solution

There isn't really a need to add a check for a stable internet connection. Just keep trying to send the email until it succeeds. The logic for your email notification thread seems like it would be very simple:

long RETRY_DELAY = 60*1000;
boolean emailSent = false;
while(!emailSent ) {
    emailSent = sendEmail();
    if (!emailSent) {
        Thread.sleep(RETRY_DELAY);
    }
}

The sendEmail() method should return false if there was any issue sending the email. E.g. exceptions related to networking.

You can set the eventual status in some other object and add a maximum number of retries, or maximum length of time to continue retrying until you give up etc. Those would just add more conditions to terminate the loop. You can also interrupt that emailer thread if there is a manual abort.



Answered By - swpalmer
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing