B4J Question Send multiple email

incendio

Well-Known Member
Licensed User
Longtime User
I have an app to send email that based on this example http://www.b4x.com/android/forum/threads/building-a-mini-email-based-server.35030/

My app has an email distribution list, stored in sql database.

Codes something like this :
B4X:
Cursor = Sql.ExecQuery("select * from table where tag = 'N' )
Do While Cursor.NextRow
  Id = Cursor.GetInt("ID")
  To = Cursor.GetString("TO")
  Subject = Cursor.GetString("SUBJECT")
  Body = Cursor.GetString("BODY")
  SMTP.To.Add(To)
  SMTP.Subject = Subject
  SMTP.Body    = Body
  SMTP.Send
Loop
Cursor.Close
Since Cursor can return more than 1 row, should I paused a few second for each rows after email sent or the code above is ok (means no delay/only a few ms delay to send each email)?
 

incendio

Well-Known Member
Licensed User
Longtime User
In message sent event, if i have multiple emails, how to catch which one that failed to send?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You will need to change your program flow and send the next message from MessageSent. It will not be slower (it will actually be more efficient).

This way you will know which message was sent.

Create a custom type with all the fields that you need for your messages.
Create a global list filled with all the "messages".
Send the first one. In MessageSent remove the first item and send the new first item.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
I am able to build app that send mail one by one.

Not using a list, still using an sql table and a global variable to hold primary key of the table.

This the table structure
CREATE TABLE MAIL
(
ID Integer NOT NULL,
SNT_TO Varchar(100) NOT NULL,
SUBJECT Varchar(100) NOT NULL,
BODY Varchar(640) NOT NULL,
DT2SND Date NOT NULL, /* date to send an email */
DTSNT Timestamp, /* time and date for mail actually sent, has null value if mail was not sent */
FAILED Smallint DEFAULT 0 NOT NULL, /* number of failed sent, if any */
PRIMARY KEY (ID)
);

App runs in background in a tray icon and app has a timer, check every 10 min for new data to send. If new data found, it retrieve only 1 row that value on DTSNT is null and failed is not more than 3.

If it found new data, send this mail and on MessageSent event, if success, update the value of DTSNT column with actual time sent and retrieve another row again.

That's the main idea, it works well but I am surprised with its memory consumption on Windows 64, java 1.7.0_67.

First time runs took about 60MB and for each mail sent, memory consumption raised again, don't know how much, last time I checked, after sent about 7 email, memory consumption is about 77MB.

Is this normal ? Won't it consume to much memory if there are a lots mail to sent?
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Ok, thanks for the clarification.

In case someone needed, here is the source code.
This is a general bulk send mail app. I made it using Firebird database. You can change to any database, just change the JDBC.

On your database, you should create these tables :

CREATE TABLE T_MAIL
(
ID Integer NOT NULL,
SNT_TO Varchar(100) NOT NULL,
SUBJECT Varchar(100) NOT NULL,
BODY Varchar(640) NOT NULL,
DT2SND Date NOT NULL, /* date email must send*/
DTSNT Timestamp, /* date & time actual email sent*/
FAILED Smallint DEFAULT 0 NOT NULL,
PRIMARY KEY (ID)
);
CREATE INDEX IDX_MAIL1 ON T_MAIL (DTSNT);

To send mails, just insert data to this table (insert id,snt_to,subject,body,dt2snd)

CREATE TABLE M_INIT
(
TAG Varchar(20) NOT NULL,
VAL Varchar(40),
PRIMARY KEY (TAG)
);

INSERT INTO M_INIT (TAG, VAL) VALUES ('Time 2 Sent Mail', '10');
INSERT INTO M_INIT (TAG, VAL) VALUES ('Interval (mn)', '10');

This is table for settings,
- Time 2 Sent Mail : email will send if current time is > 10
- Interval (mn) : app will check for new data every 10 minutes

CREATE TABLE ERR_LOG
(
MAIL_ID Integer,
ERR Varchar(1000)
);

Table to log if mail failed to sent. Mail_Id = Id in table MAIL.
 

Attachments

  • email.zip
    2.9 KB · Views: 308
Upvote 0
Top