0

我有一段有效的代码:

读取数据库,读取模板 (template.htm),将数据放入基于模板 (evento.htm) 的新文件中,读取该文件并发送包含生成文件内容的电子邮件。下面的代码(我剪切了数据库部分):

<%
    NomeDoTemplate= "template.htm"
    CaminhoDoTemplate= Server.MapPath(NomeDoTemplate)
    CaminhoDoTemplateAjustado= Mid(CaminhoDoTemplate,1,InStrRev(CaminhoDoTemplate,"\"))
    NomeDoArquivo= "evento.htm"
    CaminhoDoArquivo= Server.MapPath(NomeDoArquivo)

    Set ManipulacaoDeArquivo= Server.CreateObject("Scripting.FileSystemObject")
    Set ObjetoArquivo= ManipulacaoDeArquivo.OpenTextFile(CaminhoDoTemplate, 1)
    DadosDoObjetoArquivo= ObjetoArquivo.ReadAll
    ObjetoArquivo.Close

    DadosDoObjetoArquivo= Replace(DadosDoObjetoArquivo, "[Cliente]", Um)

    Set ObjetoArquivo= ManipulacaoDeArquivo.CreateTextFile(CaminhoDoTemplateAjustado & NomeDoArquivo)
    ObjetoArquivo.Write(DadosDoObjetoArquivo)

    Set ObjetoArquivo= ManipulacaoDeArquivo.OpenTextFile(CaminhoDoTemplateAjustado & NomeDoArquivo, 1)
    DadosDoObjetoArquivo= ObjetoArquivo.ReadAll

    Dim objCDOSYSMail
    Dim objCDOSYSCon

    Set objCDOSYSMail = Server.CreateObject("CDO.Message")
    Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")

    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.server.com"
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user_id"
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
    objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
    objCDOSYSCon.Fields.update

    Set objCDOSYSMail.Configuration = objCDOSYSCon
    objCDOSYSMail.From = "ABC <abc@server.com>"
    objCDOSYSMail.To = "sender@gmail.com"
    objCDOSYSMail.Subject = "Contato"
    objCDOSYSMail.HTMLBody= DadosDoObjetoArquivo
    objCDOSYSMail.Send

    Set objCDOSYSMail = Nothing
    Set objCDOSYSCon = Nothing
%>

我想让这个简单,跳过在磁盘中生成文件的步骤。我想:

读取数据库,读取模板,将数据放入内存,发送带有内存中数据的邮件。

谢谢

4

2 回答 2

1

如果我没看错,你所要做的就是跳过保存文件的部分并重新阅读它……我已经重构了你的代码,给变量一些英文名称,这样我就可以看到发生了什么,并发表评论去掉你不需要的行:

<%
    Dim TemplateName     : TemplateName = "template.htm"
    Dim TemplateFullPath : TemplateFullPath = Server.MapPath(TemplateName)
    Dim TemplatePath     : TemplatePath = Mid(TemplateFullPath,1,InStrRev(TemplateFullPath,"\"))

    Dim ArchiveName      : ArchiveName = "evento.htm"
    Dim ArchiveFullPath  : ArchiveFullPath = Server.MapPath(ArchiveName)

    Dim FSO, TemplateFile, TemplateText
    Set FSO = Server.CreateObject("Scripting.FileSystemObject")
        Set TemplateFile = FSO.OpenTextFile(TemplateFullPath, 1)
            TemplateText = TemplateFile.ReadAll()
            TemplateText = Replace(TemplateText, "[Cliente]", Um)
            TemplateFile.Close()

'   Really simple - to do this in-memory, simply don't save and re-read the file....

'        Set TemplateFile = FSO.CreateTextFile(TemplatePath & ArchiveName)
'            TemplateFile.Write(TemplateText)

'        Set TemplateFile = FSO.OpenTextFile(TemplatePath & ArchiveName, 1)
'            TemplateText = TemplateFile.ReadAll


        Set TemplateFile = Nothing
    Set FSO = Nothing

    Dim objCDOSYSMail, objCDOSYSCon
    Set objCDOSYSMail = Server.CreateObject("CDO.Message")
        Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.server.com"
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user_id"
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
            objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
            objCDOSYSCon.Fields.update

            Set objCDOSYSMail.Configuration = objCDOSYSCon
                objCDOSYSMail.From = "ABC <abc@server.com>"
                objCDOSYSMail.To = "sender@gmail.com"
                objCDOSYSMail.Subject = "Contato"
                objCDOSYSMail.HTMLBody= TemplateText
                objCDOSYSMail.Send
            Set objCDOSYSMail.Configuration =  Nothing
        Set objCDOSYSMail = Nothing
    Set objCDOSYSCon = Nothing
%>

希望这可以帮助,

埃里克

于 2013-02-27T13:16:48.633 回答
1

你可以使用几种技术:

  • 编写自己的 stringbuilder 类
  • 使用 .net system.io.stringwriter 类(是的,你可以从经典的 asp 中使用它)
  • 使用 adodb.stream 对象

示例字符串编写器:

set sw = server.createObject("system.io.stringwriter")
sw.write_12( DadosDoObjetoArquivo )

objCDOSYSMail.HTMLBody = sw.getStringBuilder().toString()

示例(adodb.stream):

set stream = server.createobject("ADODB.Stream")
with stream 
    .Open
    .WriteText DadosDoObjetoArquivo
end with

objCDOSYSMail.HTMLBody = stream.ReadText
stream.Close
于 2013-02-27T13:17:09.180 回答