Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


















Posted
Updated 9-May-16 2:14am
v5
Comments
Sinisa Hajnal 15-Apr-16 6:51am    
Not an expert on classic ASP, but what if the only option is to send the file to the server? You said you found the examples...can you adapt them to work without sending files?

Anyhow, you're doing server-side code any time you write <%..%> so generally speaking, you're sending mail from the server code. Why is it problematic to get the file there?
Richard Deeming 15-Apr-16 9:26am    
Why would anyone be writing new code in a technology which has been dead for 15 years?!
TarunShrivastav 18-Apr-16 6:44am    
yes Richard I agree with you but I am working in a project of classic ASP and i don't have any option so i have to do it although classic asp is like a german to me but i am trying to learn it.
TarunShrivastav 9-May-16 8:57am    
Hi Sinisa, I have updated the question please check if you can help me out in this

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.
 
Share this answer
 
Comments
TarunShrivastav 9-May-16 8:55am    
Hi Anvil i have updated the question please have a look if you can help ......
Sending a text e-mail with an Attachment:

HTML
<%
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
%>
 
Share this answer
 
v2
Comments
TarunShrivastav 15-Apr-16 8:02am    
Thank you very much
Mahesh but the above specified solution is applicable when we have the file on that server but what if we want to give user a option of select file as the attachment a user should be able select the multiple attachment with a browse button. I Believe this can be done with ASP input type as file but I don't know how to pass that path in objMyEmail.AddAttachment :?/????
Mahesh_Bhosale 15-Apr-16 9:04am    
See the demo for how to upload file
demo


You can also use jquery to upload / attach file
Jquery Upload demo
TarunShrivastav 9-May-16 8:56am    
Hi Mahesh,
I have updated the question please check if you could help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900