I need to run a script to retrieve any emails from an Exchange server. However the code below reports there are no messages all the time. Running the same on my standalone laptop I can only download one message at a time. I need the message in one table and the attachments in another table.
Can anyone help? Sorry if my code is crude - this is a new area to me.
NB lines starting with dots are to show indents which do not seem to work when pasting in code.
'RETRIEVE EMAIL TO INBOX TABLE, ATTACHMENTS TO EMLFILE TABLE
hourglass_cursor(.T.)
dim cAttach as C
cAttach = ""
dim e as p
e.dummy = ""
e = email.open()
statusbar.Set_Text("Logging on to Email server...")
'e.client_hostname = use defaults setup in email profile on each pc
'e.client_identity =
'e.client_password =
'e.client_username =
'e.mail_hostname =
e.receive_attachments_field = "Attachments"
e.receive_autodelete = .f.
e.receive_bcc_address_field = "Bcc"
e.receive_cc_address_field = "Cc"
e.receive_date_field = "Sent_Date"
e.receive_from_address_field = "From"
e.receive_limit = -1.000000
e.receive_message_body_field = "Body"
e.receive_reply_field = "Reply"
e.receive_sort_date_field = "Sort_Date"
e.receive_subject_field = "Subject"
e.receive_to_field = "To"
'mark attachments as processed on previously retrieved emails with attachments
dim ti as p
ti = table.open("inbox")
query.options = ""
query.order = "recno()"
query.filter = "Attachments <> '' .AND. Process = .F."
qi = ti.query_create()
if Qi.records_get() > 0 then
.while .not. ti.fetch_eof()
..ti.change_begin()
..ti.Process = .T.
..ti.change_end()
..ti.fetch_next()
.end while
end if
ti.query_detach()
ti.close()
'open inbox & retrieve new emails to it
dim ti as P
ti = table.open("inbox")
statusbar.Set_Text("Retrieving Emails...")
recs = e.receive(ti)
if Recs > 0 then
.ui_msg_box("Email","" + recs + " messages were retreived")
else
.ui_msg_box("Email", "No messages to retrieve")
end if
'link newly retrieved email records with attachments to table storing attachments
query.options = ""
query.order = "recno()"
query.filter = "Process = .F. .AND. Attachments <>''"
qi = ti.query_create()
if qi.records_get() > 0 then
.statusbar.Set_Text("Storing Attachments...")
.tf = table.open("emlfile")
.while .not. ti.fetch_eof()
..cAttach = ti.Attachments
..cAttach = comma_to_crlf(ti.Attachments) 'convert comma delimited list to crlf list
...for each item in cAttach
....tf.enter_begin()
....tf.Emailid = ti.Emailid
....tf.Filename = ltrim(item.value) 'may be space before file name file1, file2 ->file1<crlf> file2
....tf.Filepath = "C:\Torclad\attachments"+chr(92) 'leave attachments folder in email profile blank
....tf.enter_end()
...next
...ti.fetch_next()
..end while
.end if
ti.query_detach()
ti.close()
query.filter = ".T."
delete e 'close the email object
statusbar.Set_Text("")
hourglass_cursor(.F.)
Can anyone help? Sorry if my code is crude - this is a new area to me.
NB lines starting with dots are to show indents which do not seem to work when pasting in code.
'RETRIEVE EMAIL TO INBOX TABLE, ATTACHMENTS TO EMLFILE TABLE
hourglass_cursor(.T.)
dim cAttach as C
cAttach = ""
dim e as p
e.dummy = ""
e = email.open()
statusbar.Set_Text("Logging on to Email server...")
'e.client_hostname = use defaults setup in email profile on each pc
'e.client_identity =
'e.client_password =
'e.client_username =
'e.mail_hostname =
e.receive_attachments_field = "Attachments"
e.receive_autodelete = .f.
e.receive_bcc_address_field = "Bcc"
e.receive_cc_address_field = "Cc"
e.receive_date_field = "Sent_Date"
e.receive_from_address_field = "From"
e.receive_limit = -1.000000
e.receive_message_body_field = "Body"
e.receive_reply_field = "Reply"
e.receive_sort_date_field = "Sort_Date"
e.receive_subject_field = "Subject"
e.receive_to_field = "To"
'mark attachments as processed on previously retrieved emails with attachments
dim ti as p
ti = table.open("inbox")
query.options = ""
query.order = "recno()"
query.filter = "Attachments <> '' .AND. Process = .F."
qi = ti.query_create()
if Qi.records_get() > 0 then
.while .not. ti.fetch_eof()
..ti.change_begin()
..ti.Process = .T.
..ti.change_end()
..ti.fetch_next()
.end while
end if
ti.query_detach()
ti.close()
'open inbox & retrieve new emails to it
dim ti as P
ti = table.open("inbox")
statusbar.Set_Text("Retrieving Emails...")
recs = e.receive(ti)
if Recs > 0 then
.ui_msg_box("Email","" + recs + " messages were retreived")
else
.ui_msg_box("Email", "No messages to retrieve")
end if
'link newly retrieved email records with attachments to table storing attachments
query.options = ""
query.order = "recno()"
query.filter = "Process = .F. .AND. Attachments <>''"
qi = ti.query_create()
if qi.records_get() > 0 then
.statusbar.Set_Text("Storing Attachments...")
.tf = table.open("emlfile")
.while .not. ti.fetch_eof()
..cAttach = ti.Attachments
..cAttach = comma_to_crlf(ti.Attachments) 'convert comma delimited list to crlf list
...for each item in cAttach
....tf.enter_begin()
....tf.Emailid = ti.Emailid
....tf.Filename = ltrim(item.value) 'may be space before file name file1, file2 ->file1<crlf> file2
....tf.Filepath = "C:\Torclad\attachments"+chr(92) 'leave attachments folder in email profile blank
....tf.enter_end()
...next
...ti.fetch_next()
..end while
.end if
ti.query_detach()
ti.close()
query.filter = ".T."
delete e 'close the email object
statusbar.Set_Text("")
hourglass_cursor(.F.)