发邮件问题
本人在网上载了一段发邮件代码,经测试确实可用,我想对它作一些改进:
自动给100个用户发一份内容相同,但附件不同的邮件.
如:
给1用户发附件为1.txt的邮件;给2用户发2.txt的附件``````````直至给100用户发100.txt的附件。
附件存放在同一目录下!
请问如何改动?
local cTo,cSub,cAtta,cBody,cSmtp,cFrom,cUid,cPwd,cErrMsg
cTo = "收件人 <1@163.com> "
cSub = "主题 "
cBody = "测试发件 "
cAtta = "C:\\1.txt "
cSmtp = "smtp.163.com "
cFrom = "发件人 <123@163.com> "
cUid = "123 "
cPwd = "123 "
cErrMsg = SendMail(cTo, " ", " ",cSub,cBody,cAtta,cSmtp,cFrom,cUid,cPwd)
IF EMPTY(cErrMsg)
MESSAGEBOX( "邮件发送成功! ",64, "系统消息 ")
ELSE
MESSAGEBOX(cErrMsg,64, "发送邮件出错消息 ")
ENDIF
FUNCTION SendMail(;
cMail,;
cCC,;
cBCC,;
cSubject,;
cBody,;
cAttachFile,;
cSmtp,;
cFrom,;
cUid,;
cPwd)
LOCAL cError,cErrMsg,m,n,i,j,k,objFields
m.cErrMsg = " "
m.cError = ON( "ERROR ")
ON ERROR m.cErrMsg = MESSAGE()
m.objMail = CREATEOBJECT( "CDO.Message ")
m.objFields = m.objMail.Configuration.Fields
FOR i = 0 TO objFields.Count - 1
m.objFields.Delete(i)
ENDFOR
m.objFields.Update
m.objConf = m.objMail.Configuration
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/languagecode ",2)
m.objConf.Fields(0).Value = "zh-cn "
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/postusing ",2)
m.objConf.Fields(1).Value = 0
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/sendemailaddress ",2)
m.objConf.Fields(2).Value = m.cFrom
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/sendpassword ",2)
m.objConf.Fields(3).Value = m.cPwd
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/sendusername ",2)
m.objConf.Fields(4).Value = m.cUid
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/sendusing ",2)
m.objConf.Fields(5).Value = 2
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/smtpaccountname ",2)
m.objConf.Fields(6).Value = "网易服务器 "
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate ",2)
m.objConf.Fields(7).Value = 1
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout ",2)
m.objConf.Fields(8).Value = 180
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/smtpserver ",2)
m.objConf.Fields(9).Value = m.cSmtp
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/smtpserverport ",2)
m.objConf.Fields(10).Value = 25
m.objConf.Fields.Append( "http://schemas.microsoft.com/cdo/configuration/usemessageresponsetext ",2)
m.objConf.Fields(11).Value = .T.
m.objConf.Fields.Append( "urn:schemas:calendar:timezoneid ",2)
m.objConf.Fields(12).Value = 21
m.objConf.Fields.Update
m.objMail.To = m.cMail
m.objMail.CC = m.cCC
objMail.BCC = m.cBCC
m.objMail.Subject = m.cSubject
m.objMail.TextBody = m.cBody
objMail.From = m.cFrom
IF !EMPTY(m.cAttachFile)
m.cAttachFile = m.cAttachFile + "; "
m.j = 0
m.n = OCCURS( "; ", m.cAttachFile)
FOR m.i = 1 TO m.n
m.k = AT( "; ", m.cAttachFile, m.i)
m.cFile = SUBSTR(m.cAttachFile, m.j+1, m.k-m.j-1)
IF !FILE(m.cFile)
ON ERROR &cError
RETURN m.cErrMsg
ENDIF
objMail.AddAttachment(m.cFile)
m.j = m.k
ENDFOR
ENDIF
m.objMail.Send
ON ERROR &cError
RETURN m.cErrMsg
ENDFUNC
[解决办法]
local cTo,cSub,cAtta,cBody,cSmtp,cFrom,cUid,cPwd,cErrMsg
cTo = "收件人 <1@163.com> "
cSub = "主题 "
cBody = "测试发件 "
cAtta = "C:\\1.txt "
cSmtp = "smtp.163.com "
cFrom = "发件人 <123@163.com> "
cUid = "123 "
cPwd = "123 "
cErrMsg = SendMail(cTo, " ", " ",cSub,cBody,cAtta,cSmtp,cFrom,cUid,cPwd)
IF EMPTY(cErrMsg)
MESSAGEBOX( "邮件发送成功! ",64, "系统消息 ")
ELSE
MESSAGEBOX(cErrMsg,64, "发送邮件出错消息 ")
ENDIF
改为
FOR I=1 TO 100
lcCto= '收件人 < '+transform(I)+ '@163.com> ' &&如果这100个邮件地址在一个表中就循环这个表即可。
lcCAtta= 'C:\\ '+transform(I)+ '.txt ' &&如果这100个附件路径在一个表中就循环这个表即可。
local cTo,cSub,cAtta,cBody,cSmtp,cFrom,cUid,cPwd,cErrMsg
cTo = lcCto
cSub = "主题 "
cBody = "测试发件 "
cAtta = lcCAtta
cSmtp = "smtp.163.com "
cFrom = "发件人 <123@163.com> "
cUid = "123 "
cPwd = "123 "
cErrMsg = SendMail(cTo, " ", " ",cSub,cBody,cAtta,cSmtp,cFrom,cUid,cPwd)
IF !EMPTY(cErrMsg)
MESSAGEBOX(cErrMsg,64, "发送邮件出错消息 ")
ENDIF
ENDFOR