Powerbuilder夾帶檔案發送郵件的方法

案例版本:PowerBuilder 6.5
powerbuilder本身有呼叫exchange郵件的能力,所以發送郵件只需要使用內定物件與函數即可達成,但是有許多需要注意的地方,例如附件檔案、郵件內容等等。

先看一下畫面的設計:

收件者欄位是 sle_recipient
主旨是 sle_subject
附加檔案是 lb_1
郵件內容是 mle_notetext



在『傳送』按紐的clicked事件裡面放上一段程式碼:

mailSession lm_Ses
mailReturnCode lm_Ret
mailMessage lm_msg
mailFileDescription mfd
integer    li_no
Long        ll_pos
String    ls_file,ls_path

lm_Ses = create mailSession

//登入目前郵件伺服器
lm_Ret = lm_Ses.mailLogon()
If lm_Ret<>mailReturnSuccess! Then
    MessageBox('提示訊息','郵件Logon失敗')
    //登出郵件伺服器
    wf_logoff_mail(lm_Ses)
    Return
End If

//附件
for li_no = 1 to lb_1.totalitems()
    //分出檔案名稱
    ll_pos    = wf_lastpos(lb_1.text(li_no),'\')
    ls_file    = mid(lb_1.text(li_no),ll_pos + 1)
    //添加到郵件訊息附件   
    lm_Msg.AttachmentFile[li_no].FileType=mailAttach!
    lm_Msg.AttachmentFile[li_no].Filename = ls_file
    lm_Msg.AttachmentFile[li_no].Pathname = lb_1.text(li_no)
    lm_Msg.AttachmentFile[li_no].position = li_no
next

//郵件主旨
lm_Msg.Subject = sle_subject.text
//郵件內容
lm_Msg.notetext = '    ~r~n' + mle_notetext.text
//收件者
lm_Msg.Recipient[1].Name = sle_recipient.text

//傳送郵件
lm_Ret=lm_Ses.mailsend(lm_Msg)
choose case lm_Ret
    case mailReturnSuccess!
        MessageBox('提示訊息','郵件發送成功')
    case mailReturnFailure!
        MessageBox('提示訊息','郵件發送失敗')
    case mailReturnInsufficientMemory!
        MessageBox('提示訊息','記憶體不足')
    case mailReturnLoginFailure!
        MessageBox('提示訊息','郵件登入失敗')
    case mailReturnUserAbort!
        MessageBox('提示訊息','使用者中斷')
    case mailReturnDiskFull!
        MessageBox('提示訊息','磁碟已滿')
    case mailReturnTooManySessions!
        MessageBox('提示訊息','太多SESSION')
    case mailReturnTooManyFiles!
        MessageBox('提示訊息','太多檔案')
    case mailReturnTooManyRecipients!
        MessageBox('提示訊息','太多收件人')
    case mailReturnUnknownRecipient!
        MessageBox('提示訊息','收件人未知')
    case mailReturnAttachmentNotFound!
        MessageBox('提示訊息','附加檔案未找到')
    case mailreturnattachmentwritefailure!
        MessageBox('提示訊息','附加檔案寫入失敗')
    case mailreturninvalidmessage!
        MessageBox('提示訊息','無效郵件訊息')
    case mailreturnmessageinuse!
        MessageBox('提示訊息','郵件被使用中')
    case mailreturnnomessages!
        MessageBox('提示訊息','沒有郵件內容')
    case mailreturnaccessdenied!
        MessageBox('提示訊息','郵件無法存取')
    case mailreturntexttoolarge!
        MessageBox('提示訊息','郵件文字太大')
End choose

//登出郵件伺服器
wf_logoff_mail(lm_Ses)


裡面有兩個window function:

wf_logoff_mail(mailsession a_mse)
這個function主要是將 MAIL Logout的部分寫出來

string ls_ret
MailReturnCode lm_ret

lm_ret=a_mse.mailLogoff()
If lm_ret<> mailReturnSuccess! Then
    MessageBox('提示訊息','郵件Logoff失敗')
    return
End If
Destroy a_mse


和wf_lastpos(String as_text,String as_key) return long
(wf_lastpos主要是用來找出關鍵字(as_key)在字串(as_text)中出現的最後位置)

Long    ll_pos,ll_next,ll_find

ll_find    = 0
if isnull(as_text) then return ll_find
if len(as_text) <= 0 then Return ll_find

ll_next = 1
do
    ll_pos    = pos(as_text,as_key,ll_next)
    if (ll_pos <= 0) then exit
   
    ll_next    = ll_pos + 1
    ll_find     = ll_pos
loop while ll_pos < len(as_text)

return ll_find


其中主程式段的部份
    //添加到郵件訊息附件   
    lm_Msg.AttachmentFile[li_no].FileType=mailAttach!
    lm_Msg.AttachmentFile[li_no].Filename = ls_file
    lm_Msg.AttachmentFile[li_no].Pathname = lb_1.text(li_no)
    lm_Msg.AttachmentFile[li_no].position = li_no

可以改成
    //添加到郵件訊息附件   
    mfd.FileType = mailAttach!
    mfd.Filename = ls_file
    mfd.Pathname = lb_1.text(li_no)
    mfd.position = li_no
    lm_Msg.AttachmentFile[li_no] = mfd

意思是一樣的

比較有爭議的部份是 mailFileDescription.position 和 mailMessage.notetext 這兩個部份!在我的測試中如果你夾帶兩個以上的附件時,若不指定 mailFileDescription.position的值,將會導致郵件發送失敗。

另則,mailMessage.notetext若沒有任何內容時也將會導致郵件發送失敗。

還有一點較為奇特的就是郵件內文居然會含有附件的文字敘述,例如:郵件內文為
HELLO EVERY BODY
而附件為 file1.txt 和 file2.xls

而收到的郵件內文卻會變成
H<<file1.txt>>ELLO EVERY BODY
<<file2.xls>>
這種奇怪現象,也因此,我刻意在本文前面加上『空白』,以避開此問題
//郵件內容
lm_Msg.notetext = '    ~r~n' + mle_notetext.text


也有人說會變成其他樣式,如(file1.txt)(file2.xls)
所以,僅能猜測是否受到Microsoft Outlook和Exchange Server的影響了。

留言

匿名表示…
很難得遇到PowerBuilder的同好,我可以教你朋友嗎???
我也有很多設計PowerBuilder的經驗,說不定可以分享
我的MSN: jodan134@hotmail.com
我叫做老呂

設計PowerBuilder有5年的資歷,在東部一家基金會擔任程式設計的工作

靜候您的消息

老呂
老呂

這個網誌中的熱門文章

【研究】列印的條碼為什麼很難刷(掃描)

C# 使用 Process.Start 執行外部程式

統一發票列印小程式