用ASP发送电子邮件。 [英] Email attahment with ASP.

查看:64
本文介绍了用ASP发送电子邮件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是处理应用程序的经典ASP的新手,我需要通过电子邮件发送附件。我尝试了很多东西,但仍然无法找到合适的解决方案。我可以发送没有附件的电子邮件。



一切都工作正常,除了附件。在发送附件之前,我已经看到许多解决方案,我需要将文件上传到服务器。所以我在表单动作中调用了一个页面uploaddocument.asp(这个页面在VBscript中),点击发送后正在服务器上传文件正在上传文件,但它没有发送电子邮件。



请建议是否有办法将这些附件存放在临时文件夹中,以便在发送后自动删除。



是否可以使用进度条设计附件功能,例如yahoomial或gmail,使用经典的asp javascript / VBscript删除附件的选项。



非常感谢任何帮助,感谢ADVANCE !!!



请找到虚拟代码。



MailTest.asp



我尝试过:



<%@ Language = JavaScript%>

<%

Server.ScriptTimeout = 900;

var fso = new ActiveXObject (Scripting.FileSystemObject);

函数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;



返回(mailObject);

}





var mailMessage = new mailObject();

messageText =Test;

emailID =tarun.0shrivastav@xyz。 com;

AssetmailID =vikrant.1mittal@XYZ.com;

messageSubject =test;

var op = String(要求(op));



%>



<%



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



Response.write(\ n \\\
\\ Recipient(s):
< textarea name = to rows = 3> ;+ emailID +< / textarea> *
\ n \

(list of list电子邮件地址,以分号分隔)

\ n);

Response.write(\ n\\\
Cc:
< textarea name = cc rows = 2>+ AssetmailID +< / textarea>
\ n \

(电子邮件地址列表,以分号分隔) )

\ n);

Response.write(Subject:
< input type = text name = subject size = 60> < font color =red> *

\ n);



Response.write(消息:
< 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(附件1:<输入类型=文件名=文件1>);





Response.write(

<输入类型=按钮值='添加文件'>

);



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



Response.write(< input type =提交名称=发送值='发送消息'>

\ n);

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



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



开关(op){

case(Select):

case(send):





尝试{

mailMessage.to = emailID;

mailMessage.Cc = AssetmailID

mailMessage.Bcc =vikrant.mittalMNC.com;



mailMessage.From =ABc@xyz.com;

mailMessage.Subject = String (要求(主题));
mailMessage.HtmlBody = String(Request(message));





mailMessage.Send();

Response.Write(< script language ='javascript'> alert('Mail successfully sent')< / script>);

Response.Write( \ n 邮件已成功发送);



}

catch(错误){

Response.Write(err.message);



mailMessage.Bcc =;



mailMessage.From =;

mailMessage.Subject = String(Request(subject));

mailMessage.HtmlBody =错误:邮件未成功发送,检查所有邮件ID是否有效。;

mailMessage.Send();

Response.Write(< script language ='javascript'> alert( '邮件未成功发送')< / script>);



Response.Write( M ail未成功发送,请检查所有邮件ID是否有效。 \ n \ n+评估员;

}

break;

默认值:

Response.write(完成);

}





%>





< Script>

//脚本添加附件文件字段

var nfiles = 1;

函数展开()

{

nfiles ++

var adh ='
附件'+ nfiles +':< input type =filename =File'+ nfiles +'action =UploadDocument.aspenctype =multipart / form -data>';

files.insertAdjacentHTML('beforeend',adh);

返回false;

}

< / Script>



UploadDocument.asp代码





<%@ Language = VBScript%>

<%Option明确%>

< html>

< body>

< ! - #include file =lib\lib_vbFunctions.asp - >

<%

Server.ScriptTimeout = 900



Dim Uploader,File,startTime,endTime,markTime1,totalTime,uploadTime,encType

设置Uploader = New FileUploader

startTime = Time

'这开始上传过程

Uploader.Upload()

markTime1 =时间

'检查是否有文件上传

如果Uploader.Files.Count = 0则

Response.Write文件未上传。

Else

'循环上传文件

每个文件在Uploader.Files.Items

encType = File.ContentType

If(Len(encType)> = 50)然后encType =application / octet-stream

File.ADOSaveToDiskF:\\CSDATA\\My_attachment,File.FileName

endTime =时间

totalTime =(endTime - startTime)* 10000000

uploadTime =(markTime1 - startTime)* 10000000

'将文件详细信息输出到浏览器

Response.WriteFile Uploaded:& File.FileName&


Response.WriteSize:& File.FileSize& bytes




下一页

END如果

%>

< / body>

< / html>



请尽快建议如何将上传的文件作为attachmnet发送< BR>搜索结果
的搜索结果,搜索结果,搜索结果,搜索结果,搜索结果,搜索结果<无线电通信>


解决方案

您需要记住的是,您必须将文件上传到服务器进行处理。由于代码在服务器上运行,因此无法访问本地计算机上的任何内容。它在沙箱中的浏览器中运行。有没有办法允许访问本地计算机上的资源?对于像ActiveX和Java小程序这样的东西是的,但在今天的世界中,每个用户都必须允许安装这些项目并涉及额外的配置。



你提到Gmail和雅虎邮件,他们将这些附件在后台上传到服务器上的临时位置。当你准备好发送那条消息时,它会从那个临时存储器中获取附件。



我建议你看看一些Ajax up加载器并使用相同的想法。让用户将附件上传到临时安全位置,将其附加到电子邮件中,并在发送电子邮件后删除上传。


发送文本带附件的电子邮件:



 <% 
设置 objMyEmail = CreateObject( CDO.Message
objMyEmail.Subject = 使用协作数据对象(COD)发送电子邮件
objMyEmail.From = objMyEmail@mydomain.com
objMyEmail。 To = someone@somedomain.com
objMyEmail.TextBody = 这是一条消息。
objMyEmail.AddAttachment c:\any_file.txt
objMyEmail.Send
set objMyEmail = nothing
%>


I am new to classic ASP working on an application and I have a requirement of sending attachment with email. 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. so 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.

Please suggest if there is any way to store those attachment in a temp folder so that they will be removed automatically after sending.

Is it possible to design the functionality of attachment like yahoomial, or gmail with the progress bar ,option of removing the attachment using classic asp javascript/VBscript.

Any help is very much appreciated and thanks in ADVANCE !!!

Please find the dummy code.

MailTest.asp

What I have tried:

<%@ 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>

please suggest ASAP how can i send the uploaded file as the attachmnet


















解决方案

What you need to remember is that you are going to have to upload the file to the server for processing. Since the code runs on the server it does not have access to anything on your local computer. It runs within the browser in a sandbox. Are there ways of allowing access to the resources on your local machine? Yes with things like ActiveX and Java applets, but in today's world each user would have to allows these items to be installed with extra configuration involved.

You mention Gmail and Yahoo mail, they are uploading these attachments in the background to a temp location on the server. And when you are ready to send that message it grabs the attachment from that temp storage.

I suggest that you look at some Ajax up loaders and use the same idea. Have the user upload the attachment to a temp secured location, attach it to the email, and after the email is sent delete the upload.


Sending a text e-mail with an Attachment:


<%
Set objMyEmail=CreateObject("CDO.Message")
objMyEmail.Subject="Sending email with Collaboration Data Objects(COD)"
objMyEmail.From="objMyEmail@mydomain.com"
objMyEmail.To="someone@somedomain.com"
objMyEmail.TextBody="This is a message."
objMyEmail.AddAttachment "c:\any_file.txt"
objMyEmail.Send
set objMyEmail=nothing
%>


这篇关于用ASP发送电子邮件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆