简体   繁体   English

使用经典ASP的带有多个附件的邮件

[英]Mail with multiple attachment using classic ASP

I am new to classic ASP working on an application and I have a requirement of sending attachment with email. 我是在应用程序上使用经典ASP的新手,我需要通过电子邮件发送附件。 I have tried so many things but still not able to find a proper solution. 我已经尝试了很多事情,但仍然找不到合适的解决方案。 I am able to send the emails without attachment. 我可以发送不带附件的电子邮件。

Everything is working fine except attachments. 除了附件,其他所有东西都工作正常。 I have seen many solutions suggesting before sending attachment I need to upload files to the server. 我已经看到许多解决方案,建议在发送附件之前需要将文件上传到服务器。 I have called a page uploaddocument.asp (this page is in VBscript) in the form action that is uploading the documents on the server after clicking on send it is uploading the documents but it is not sending emails. 我在表单操作中调用了一个页面uploaddocument.asp (此页面在VBscript中),该表单操作在单击“发送”后正在服务器上上传文档,它正在上载文档,但未发送电子邮件。

Please suggest if there is any way to store those attachment in a temp folder so that they will be removed automatically after sending. 请建议是否有任何方法可以将这些附件存储在temp文件夹中,以便在发送后将其自动删除。

Is it possible to design the functionality of attachment like yahoomail, or gmail with the progress bar, option of removing the attachment using classic asp javascript/VBscript. 是否可以使用进度条设计附件的功能(例如yahoomail或gmail),以及使用经典的asp javascript / VBscript删除附件的选项。

Please find the dummy code. 请找到伪代码。

MailTest.asp MailTest.asp

<%@ Language=JavaScript%>
<%
Server.ScriptTimeout = 900;
var fso = new ActiveXObject("Scripting.FileSystemObject");
function mailObject() 
{
var mailObject = new ActiveXObject("CDO.Message");
mailObject.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;

mailObject.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtprelay.p3chem.net";
mailObject.Configuration.Fields.Update();


mailObject.From = "ABC@msg2.XYZ.com";

return (mailObject);
}


var mailMessage = new mailObject();
messageText = "Test";
emailID="tarun.0shrivastav@xyz.com";
AssetmailID ="vikrant.1mittal@XYZ.com";
messageSubject ="test";
var op = String(Request("op"));

%>

<%

Response.write("<form method ='Post' action ='UploadDocument.asp' enctype='multipart/form-data'> \n");

Response.write("\n\nRecipient(s):
<textarea name=to rows=3 >"+emailID+"</textarea>*
\n\
(list of email addresses, separated by semicolon)

\n");
Response.write("\n\nCc:
<textarea name=cc rows=2 >" + AssetmailID + "</textarea>
\n\
(list of email addresses, separated by semicolon)

\n");
Response.write("Subject:
<input type=text name=subject size=60 >*

\n");

Response.write("Message:
<textarea name=message rows=15 cols=100 >" + messageText + "</textarea>*

\n");

Response.write("<form id = form2 method ='Post' action ='UploadDocument.asp' enctype='multipart/form-data'> \n"); 
Response.write("Attachment 1 : <input type=file name=File1>") ;


Response.write( "
<Input Type=Button Value='Add a file' >
") ;

Response.write("</form>\n");

Response.write("<input type=submit name=send value='Send Message'>

\n");
Response.write("<input type=hidden name=op value=send>");

Response.write("</form>\n");

switch (op) {
case ("Select"):
case ("send"):


try {
mailMessage.to =emailID;
mailMessage.Cc= AssetmailID 
mailMessage.Bcc = "vikrant.mittalMNC.com";

mailMessage.From = "ABc@xyz.com";
mailMessage.Subject = String(Request("subject"));
mailMessage.HtmlBody = String(Request("message"));


mailMessage.Send();
Response.Write("<script language='javascript'>alert('Mail successfully sent')</script>");
Response.Write("\nMail has been sent successfully ");

}
catch (err) {
Response.Write(err.message);

mailMessage.Bcc = "";

mailMessage.From = "";
mailMessage.Subject = String(Request("subject"));
mailMessage.HtmlBody = "ERROR: Mail was not sent succesfully, Check whether All Mail ID's are Valid.";
mailMessage.Send();
Response.Write("<script language='javascript'>alert('Mail was not sent succesfully')</script>");

Response.Write("Mail was not sent succesfully, Check whether All Mail ID's are Valid.\n\n" + Assessors);
}
break;
default:
Response.write ("Done");
}


%>


<Script>
//Script To add a attachment file field 
var nfiles = 1;
function Expand()
{
nfiles++
var adh = '
Attachment '+nfiles+' : <input type="file" name="File'+nfiles+'" action ="UploadDocument.asp" enctype="multipart/form-data">';
files.insertAdjacentHTML('beforeend',adh);
return false;
}
</Script>

code of UploadDocument.asp


<% @ Language= VBScript%> 
<%Option Explicit%>
<html>
<body>
<!-- #include file="lib\lib_vbFunctions.asp" -->
<%
Server.ScriptTimeout = 900

Dim Uploader, File, startTime, endTime, markTime1, totalTime, uploadTime, encType
Set Uploader = New FileUploader
startTime = Time
' This starts the upload process
Uploader.Upload()
markTime1 = Time
' Check if any files were uploaded
If Uploader.Files.Count = 0 Then
Response.Write "File(s) not uploaded."
Else
' Loop through the uploaded files
For Each File In Uploader.Files.Items
encType = File.ContentType
If (Len(encType) >= 50) Then encType = "application/octet-stream"
File.ADOSaveToDisk "F:\\CSDATA\\My_attachment",File.FileName
endTime = Time
totalTime = (endTime - startTime) * 10000000
uploadTime = (markTime1 - startTime) * 10000000
' Output the file details to the browser
Response.Write "File Uploaded: " & File.FileName & "
"
Response.Write "Size: " & File.FileSize & " bytes
"

Next
END if
%>
</body>
</html>

How can I send the uploaded file as the attachment? 如何发送上传的文件作为附件?

This is the trick I used for uploading file on server in a WYSIWYG: 这是我在所见即所得的服务器上上传文件时使用的技巧:

  1. consider an input field named attachment in Email form. 考虑一个以电子邮件形式命名为附件的输入字段。
  2. put a fake button beside this input say Browse. 在此输入旁边放一个假按钮,说“浏览”。
  3. By clicking on the fake button, simulate a click on other hidden FORM in your document. 通过单击假按钮,模拟单击文档中其他隐藏的FORM。 This second form sends the file toward the uploader and receive the filename from uploader. 第二种形式将文件发送到上传器,并从上传器接收文件名。 To prevent the submission of second form and refreshing the page, use Jquery.form.js to upload the files and onSuccess pass the uploaded FileName to your real input named attachment. 为了防止提交第二个表单并刷新页面,请使用Jquery.form.js上载文件,然后onSuccess将上载的FileName传递给名为附件的真实输入。

Real Email Form: 真实的电子邮件表格:

<form action="sendEmail.asp" method="post">
    <input name="attachment"><a onclick="$('#hiddenFileField').click()">Browse</a>
    <!--other fields of email form-->
</form>

Hidden form which uploads the file on server: 将文件上传到服务器的隐藏表单:

<form id="hiddenForm" action="File-Uploader.asp" enctype="multipart/form-data" method="post" accept-charset="utf-8">
    <input name="file1" id="hiddenFileField" type="file">
</form>

and this is the scripts to handle the process: 这是处理该过程的脚本:

$(document).ready(function(){
     $('#hiddenForm').ajaxForm({

       beforeSubmit: function() {
       $("#stat").html('uploading...');
       },

       success: function(data) {
           $("#attachment").val(data);
           $("#stat").html("");
       }
    });

    // The code to submit hidden form as soon as file selected to upload:
    $("#hiddenFileFild").change(function(){$("#hiddenForm").submit();})

});

You may modify the code to prevent submission of main from before completing the upload using #status. 您可以使用#status修改代码,以防止提交main之前完成提交。

Finally you can delete the temp file after sending email: 最后,您可以在发送电子邮件后删除临时文件:

<%
dim fs
dim file
file=server.mappath(request.form("attachment")))
Set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists(file) then
  fs.DeleteFile(file)
end if
%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM