Email send retry

M

Monty

I have to write code to send an email and if for some reason it is not
sent it should keep retrying, i am new to java but coded for a few
years in c++ so i jot down kinda rough algorithm
can you guys see the below code and comment whether it looks ok or not,
any suggestions are welcome

--------------------
import java.util.Timer;
import java.util.TimerTask;

// Usage
// EmailSender Sender = new EmailSender();
// Sender.sTo =
// Sender.sFrom =
// Sender.sSubject =
// Sender.sBody =
// Sender.SendMail();

public class EmailSender {

//Email Related Data
String sTo;
String sFrom;
String sSubject;
String sBody;
//End

//Tries related data
int nNumTries;
int nRetryTimeOut; //in secs
//end

Timer timer;

public int SendMail()
{
//mail send logic here

//if failed
MailSendRetryTask Task = new MailSendRetryTask();
timer.schedule(Task,nRetryTimeOut * 1000);
return 0;

//if success
//Need to delete this object here, no idea how this happens in java
garbage collector might interfere
return 1;
}

public EmailSender() {
nNumTries = 0;
nRetryTimeOut = 10; //10 seconds
timer = new Timer();
}

//////////////////////////////////////////////////
//Timer Class
//run function will be called automatically by timer proc
///////////////////////////////////////////
class MailSendRetryTask extends TimerTask {
public void run() {

timer.cancel(); //Terminate the timer thread

nNumTries ++;

SendMail(); //Send mail will send the mail if it cannot for some
reason it will create another timer

//Need to delete this object here
//no sure how to do that in java
//in c++ delete this;
}
}
//////////////////////////////////////////////
}
 
A

Alex Hunsley

Monty said:
I have to write code to send an email and if for some reason it is not
sent it should keep retrying, i am new to java but coded for a few
years in c++ so i jot down kinda rough algorithm
can you guys see the below code and comment whether it looks ok or not,
any suggestions are welcome

Why don't you try actually compiling and running it? We're not a
compiler here! It looks ok on a very quick look, but I haven't looked at
it in depth.
 
A

Alex Hunsley

Alex said:
Why don't you try actually compiling and running it? We're not a
compiler here! It looks ok on a very quick look, but I haven't looked at
it in depth.

Actually, scrub "it looks ok" at first look, I've not given the code
enough attention to warrant that!
 
G

Gordon Beaton

I have to write code to send an email and if for some reason it is not
sent it should keep retrying, i am new to java but coded for a few
years in c++ so i jot down kinda rough algorithm
can you guys see the below code and comment whether it looks ok or not,
any suggestions are welcome

Here's a suggestion: your MTA (i.e. your mail server) already does
this for you, so just let it do its job.

Just deliver the mail to your local mail server and *it* will keep
trying for (normally) 5 days. It will even send you a warning after a
few hours telling you what's going on.

What specific kind of failures are you trying to recover from?

/gordon
 
M

Monty

I've not given the code
enough attention to warrant that!

Actually i don't know much about the Java garbage collector, the code
given creates a new Object every time a timer expires (which might be a
few seconds) how does the java garbage collector know when i am through
with the object, when will it delete it, can i delete an object
forcibly ?
 
O

Owen Jacobson

I've not given the code

Actually i don't know much about the Java garbage collector, the code
given creates a new Object every time a timer expires (which might be a
few seconds) how does the java garbage collector know when i am through
with the object, when will it delete it, can i delete an object
forcibly ?

In reverse order:

- No, you can't forcibly delete an object. To do so would risk creating
"invalid" references that point to a deleted object.

- Broadly speaking the garbage collector starts with all references
(variables, members, statics, &c) that are directly reachable from a
thread and marks those as "reachable", then repeats the process for
every reachable object until it reaches the end of the reachable object
graph. Any objects not reachable from a live thread are eligible for
garbage collection.

So as soon as you stop referencing an object (eg., because the only
reference was in a function that's since returned) it might be GCed.

Oh, and don't top-post.
 
G

Greg R. Broderick

I have to write code to send an email and if for some reason it is not
sent it should keep retrying

Depends on what sort of result code is returned by the recipient mail
server, see RFC 2822 for details. In brief, I would probably requeue and
retry a mail if the result code is 4xx (indicates transient failure), but
would not automagically retry sending a mail if the result code is 5xx,
indicating a permanent failure.

Cheers
GRB
 
T

Timo Stamm

Gordon said:
Here's a suggestion: your MTA (i.e. your mail server) already does
this for you, so just let it do its job.

Absolutely!

Depending on the application, it might be an option to use a bounce
management that analyzes _incoming_ mails for bounce messages. This is a
huge help in maintaining customer data in a newsletter system, for example.



Timo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top