20120425-如何使用JMAIIL發送Email
最近因為工作的需要,設計了一個發送Email的介面,與大家分享一下如何使用jmail發送Email,也順便為這個程式留下紀錄。
【1.系統介面】
【2.新增函數-f_maillog】-負責留下LOG紀錄
【3.新增函數-f_send_jmail】
【f_send_jmail的內容】
//參數說明
//f_send_jmail( fs_from , fs_to , SUB , fs_body ,body_type ,errmsg)
//fs_from : 寄件者
//fs_to : 收件者
//fs_sub : 主旨
//fs_body : 內容
//fs_fromname:寄件者名稱
//errmsg:回傳錯誤訊息
//fs_cc:副本收件者
//fs_attach:附件
string ls_from
//if fs_to = "" then
// errmsg="無收件者address"
// return false
//end if
//定義郵件內容的結構
OleObject Msg
Msg = create OleObject
Msg.ConnectToNewObject("Jmail.message")
//msg.Charset = 'utf8'
msg.Charset = 'BIG5'
ls_from = gs_muser
msg.From = ls_from
msg.ReplyTo =fs_from
msg.FromName = fs_fromname
integer li_a =1
if UpperBound(fs_to) >1 then
for li_a = 1 to UpperBound(fs_to)
msg.AddRecipient(fs_to[li_a])
NEXT
END IF
if UpperBound(fs_to) =1 then
msg.AddRecipient(fs_to[1])
END IF
msg.MailServerPassWord = gs_mpwd
msg.MailServerUserName = gs_muser
//if trim(fs_cc) <> '' then msg.addrecipientCC(fs_cc)
if UpperBound(fs_cc) >0 then
for li_a = 1 to UpperBound(fs_cc)
msg.addrecipientCC(fs_cc[li_a])
NEXT
END IF
//msg.AddRecipientBCC('cherng32@gmail.com')
//msg.addrecipientCC('jelly.chen@gmail.com')
msg.Subject = fs_sub
//msg.Body = fs_body
msg.HTMLBody= fs_body
if trim(as_attach) <> '' then msg.AddAttachment(as_attach,true)
msg.Silent = true
msg.MailDomain="sensengo.com.tw"
msg.Send(gs_mailserver)
string ls_errmsg
integer li_ret
li_ret = msg.ErrorCode
ls_errmsg= msg.ErrorMessage
if li_ret = 0 then
errmsg="mail傳送成功"
msg.clear( )
msg.close( )
return true
else
errmsg=ls_errmsg
msg.clear( )
msg.close( )
return false
end if
------------------------------------------------------------------------------------
【Window-OPEN】
uo_connect luo_cr
string ls_1 ,ls_2,ls_3,ls_4
//gs_mailserver
select T.PARAMETER1
INTO :gs_mailserver
from t_work_sys t WHERE T.DOMAINID ='MAILSERVER' AND T.STATUS ='Y';
select T.PARAMETER1
INTO :ls_1
from t_work_sys t WHERE T.DOMAINID ='MAILSERVERUSERNAME' AND T.STATUS ='Y';
select T.PARAMETER1
INTO :ls_2
from t_work_sys t WHERE T.DOMAINID ='MAILSERVERUSERPASSWORD' AND T.STATUS ='Y';
select T.PARAMETER1
INTO :ls_3
from t_work_sys t WHERE T.DOMAINID ='MAILDATE' AND T.STATUS ='Y';
select T.PARAMETER1
INTO :ls_4
from t_work_sys t WHERE T.DOMAINID ='MAILFLAG' AND T.STATUS ='Y';
gs_muser = ls_1
gs_mpwd = gf_decrypt(ls_2,ls_3,ls_4)
luo_cr = create uo_connect
//讀取jsp 連結資料庫
f_maillog('讀取jsp網頁')
//if not luo_cr.wf_getjsp('mailsender.ini') then return
//if not luo_cr.wf_ini('mailsender.ini') then return
f_maillog('連接資計庫')
//if not f_connectdb(gs_dbms ,gs_logid,gs_logpwd,gs_server) then
// messagebox("訊息","資料庫錯誤"+sqlca.sqlerrtext)
// halt
//end if
f_maillog('資計庫連接成功')
//messagebox('','資計庫連接成功')
/*
String ls_startsentmaildate ='',ls_stopsentmaildate ='',ls_mailtime ='',ls_nowtime ='',ls_hour ='',ls_min='',ls_sec=''
Integer li_hour,li_min,li_sec,li_time,li_a
//Mail開始發送時間
ls_startsentmaildate=''
SELECT T.PARAMETER1
INTO :ls_startsentmaildate
FROM t_work_sys t
WHERE T.DOMAINID ='STARTSENTMAILDATE' AND T.STATUS ='Y';
sle_1.text =''
sle_1.text =ls_startsentmaildate
//Mail停止發送時間
ls_stopsentmaildate=''
SELECT T.PARAMETER1
INTO :ls_stopsentmaildate
FROM t_work_sys t
WHERE T.DOMAINID ='STOPSENTMAILDATE' AND T.STATUS ='Y';
sle_2.text =''
sle_2.text =ls_stopsentmaildate
//Mail每日發送時間
SELECT T.PARAMETER1
INTO :ls_mailtime
FROM t_work_sys t
WHERE T.DOMAINID ='MAILTIME' AND T.STATUS ='Y';
sle_3.text =''
sle_3.text = ls_mailtime
timer(10)
THIS.POST EVENT ue_postopen ( )
*/
【1.系統介面】
【2.新增函數-f_maillog】-負責留下LOG紀錄
【3.新增函數-f_send_jmail】
【f_send_jmail的內容】
//參數說明
//f_send_jmail( fs_from , fs_to , SUB , fs_body ,body_type ,errmsg)
//fs_from : 寄件者
//fs_to : 收件者
//fs_sub : 主旨
//fs_body : 內容
//fs_fromname:寄件者名稱
//errmsg:回傳錯誤訊息
//fs_cc:副本收件者
//fs_attach:附件
string ls_from
//if fs_to = "" then
// errmsg="無收件者address"
// return false
//end if
//定義郵件內容的結構
OleObject Msg
Msg = create OleObject
Msg.ConnectToNewObject("Jmail.message")
//msg.Charset = 'utf8'
msg.Charset = 'BIG5'
ls_from = gs_muser
msg.From = ls_from
msg.ReplyTo =fs_from
msg.FromName = fs_fromname
integer li_a =1
if UpperBound(fs_to) >1 then
for li_a = 1 to UpperBound(fs_to)
msg.AddRecipient(fs_to[li_a])
NEXT
END IF
if UpperBound(fs_to) =1 then
msg.AddRecipient(fs_to[1])
END IF
msg.MailServerPassWord = gs_mpwd
msg.MailServerUserName = gs_muser
//if trim(fs_cc) <> '' then msg.addrecipientCC(fs_cc)
if UpperBound(fs_cc) >0 then
for li_a = 1 to UpperBound(fs_cc)
msg.addrecipientCC(fs_cc[li_a])
NEXT
END IF
//msg.AddRecipientBCC('cherng32@gmail.com')
//msg.addrecipientCC('jelly.chen@gmail.com')
msg.Subject = fs_sub
//msg.Body = fs_body
msg.HTMLBody= fs_body
if trim(as_attach) <> '' then msg.AddAttachment(as_attach,true)
msg.Silent = true
msg.MailDomain="sensengo.com.tw"
msg.Send(gs_mailserver)
string ls_errmsg
integer li_ret
li_ret = msg.ErrorCode
ls_errmsg= msg.ErrorMessage
if li_ret = 0 then
errmsg="mail傳送成功"
msg.clear( )
msg.close( )
return true
else
errmsg=ls_errmsg
msg.clear( )
msg.close( )
return false
end if
------------------------------------------------------------------------------------
【Window-OPEN】
uo_connect luo_cr
string ls_1 ,ls_2,ls_3,ls_4
//gs_mailserver
select T.PARAMETER1
INTO :gs_mailserver
from t_work_sys t WHERE T.DOMAINID ='MAILSERVER' AND T.STATUS ='Y';
select T.PARAMETER1
INTO :ls_1
from t_work_sys t WHERE T.DOMAINID ='MAILSERVERUSERNAME' AND T.STATUS ='Y';
select T.PARAMETER1
INTO :ls_2
from t_work_sys t WHERE T.DOMAINID ='MAILSERVERUSERPASSWORD' AND T.STATUS ='Y';
select T.PARAMETER1
INTO :ls_3
from t_work_sys t WHERE T.DOMAINID ='MAILDATE' AND T.STATUS ='Y';
select T.PARAMETER1
INTO :ls_4
from t_work_sys t WHERE T.DOMAINID ='MAILFLAG' AND T.STATUS ='Y';
gs_muser = ls_1
gs_mpwd = gf_decrypt(ls_2,ls_3,ls_4)
luo_cr = create uo_connect
//讀取jsp 連結資料庫
f_maillog('讀取jsp網頁')
//if not luo_cr.wf_getjsp('mailsender.ini') then return
//if not luo_cr.wf_ini('mailsender.ini') then return
f_maillog('連接資計庫')
//if not f_connectdb(gs_dbms ,gs_logid,gs_logpwd,gs_server) then
// messagebox("訊息","資料庫錯誤"+sqlca.sqlerrtext)
// halt
//end if
f_maillog('資計庫連接成功')
//messagebox('','資計庫連接成功')
/*
String ls_startsentmaildate ='',ls_stopsentmaildate ='',ls_mailtime ='',ls_nowtime ='',ls_hour ='',ls_min='',ls_sec=''
Integer li_hour,li_min,li_sec,li_time,li_a
//Mail開始發送時間
ls_startsentmaildate=''
SELECT T.PARAMETER1
INTO :ls_startsentmaildate
FROM t_work_sys t
WHERE T.DOMAINID ='STARTSENTMAILDATE' AND T.STATUS ='Y';
sle_1.text =''
sle_1.text =ls_startsentmaildate
//Mail停止發送時間
ls_stopsentmaildate=''
SELECT T.PARAMETER1
INTO :ls_stopsentmaildate
FROM t_work_sys t
WHERE T.DOMAINID ='STOPSENTMAILDATE' AND T.STATUS ='Y';
sle_2.text =''
sle_2.text =ls_stopsentmaildate
//Mail每日發送時間
SELECT T.PARAMETER1
INTO :ls_mailtime
FROM t_work_sys t
WHERE T.DOMAINID ='MAILTIME' AND T.STATUS ='Y';
sle_3.text =''
sle_3.text = ls_mailtime
timer(10)
THIS.POST EVENT ue_postopen ( )
*/