|
-
Nov 5th, 2008, 04:00 PM
#13
New Member
Re: [2005] Downloading file
Thanks for the welcome. Ask and ye shall receive. I figured it out, though. I had several different variables defined - one for URL of the file, one of the file name, and a Uri variable. I think I had just mixed up which one was supposed to go where in the ContentDispostion section. I decided to just use strings instead of Uris, though... too prone to those "Uri format not supported" issues, as I saw. This code works, for the benefit of whomever needs this in the future... Sanitized for our protection :-).
Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Security.Principal;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
protected void Page_Load(object sender, EventArgs e)
{
// Decode and parse the received URL for the attachment
string docUrl= (string)Request.QueryString["docUrl"];
docUrl= docUrl.Replace("%2F", "/");
docUrl= docUrl.Replace("+", " ");
docUrl= docUrl.Replace("%3A", ":");
docUrl= docUrl.Replace("%2D", "-");
// Use a system thread to allow the web application to remain responsive
Thread sendingThread = new Thread(SendMailMessage);
// Send the URI to the Thread.Start method
sendingThread.Start(docUrl);
Response.Write("Your request has been submitted. Thank you.");
}
public void SendMailMessage(object url)
{
// Set the variables
string docUrl = (string)url;
String tempFolderPath = Path.GetTempPath();
MailMessage message = new MailMessage();
SmtpClient mailClient = new SmtpClient("localhost");
Stream contentStream;
string toEmail;
string localComputer = System.Environment.MachineName;
string userId = WindowsIdentity.GetCurrent().Name.ToString();
string[] userName = new string[1];
char[] splitter = { '\\' };
// Set the e-mail address
userName = userId.Split(splitter);
userId = userName[1];
toEmail = userId + "@ourdomain.com";
// Set the parentUrl and msgId
string parentUrl = "";
string[] urlArray = new String[7];
urlArray = msgUrl.Split('/');
for (int i = 0; i < 7; i++)
{
parentUrl += urlArray[i] + "/";
}
parentUrl = parentUrl.Remove((parentUrl.Length - 1), 1);
string msgId = urlArray[7];
try
{
// Establish the message
message.From = new MailAddress("[email protected]");
message.Sender = new MailAddress("[email protected]");
message.To.Add(toEmail);
message.Subject = "Your Message Result";
message.IsBodyHtml = true;
message.Body = "<p>Attached you should find the message you requested. Have a nice day! :-)</p>";
// Get the file name for this attachment
// (done in case it's necessary to have the name)
string[] fileName = docUrl.Split('/');
string file = fileName[fileName.Length - 1];
// Use Integrated Authentication to check for a matching message directory
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(docUrl);
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.ContentLength > 0) // if the file is found
{
contentStream = response.GetResponseStream();
// Uses the file name found by splitting on the URL
Attachment data = new Attachment(contentStream, file, response.ContentType);
// Add the information for the file
ContentDisposition disposition = data.ContentDisposition;
disposition.FileName = file;
disposition.CreationDate = File.GetCreationTime(file);
disposition.ModificationDate = File.GetLastWriteTime(file);
disposition.ReadDate = File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
// Set up the SMTP Request
try
{
// Send the message
// Add credentials if the SMTP server requires them.
mailClient.Credentials = CredentialCache.DefaultNetworkCredentials;
mailClient.Send(message);
}
catch (SmtpException mailException)
{
// SMTP may not be enabled, relaying permissions may be denied,
// or the network went down
EventLog ev = new EventLog("Application");
ev.Source = "E-mailFwd";
ev.Log = mailException.Message;
}
finally
{
contentStream.Dispose();
response.Close();
}
}
else
{
// The remote file server didn't like our request, or was busy.
// Log it and send an e-mail to the sender it didn't work.
EventLog ev = new EventLog("Application");
ev.Source = "E-mailFwd";
ev.Log = "Request made by " + userId + " for " + file + " failed.";
// Establish the message
message.From = new MailAddress("[email protected]");
message.Sender = new MailAddress("[email protected]");
toEmail = userId + "@ourdomain.com";
message.To.Add(toEmail);
message.Subject = "Your Message Result - FAILURE";
message.IsBodyHtml = true;
message.Body = "<p>Your message could not be retrieved from the archive. Please inform the Systems Administrator.</p><p>Respectfully,</p><p>- Us</p>";
// Set up the SMTP Request
try
{
// Send the failure message
// Add credentials if the SMTP server requires them.
mailClient.Credentials = CredentialCache.DefaultNetworkCredentials;
mailClient.Send(message);
}
catch (SmtpException mailException)
{
// SMTP may not be enabled, relaying permissions may be denied,
// or the network went down
ev.Source = "E-mailFwd";
ev.Log = mailException.Message;
}
finally
{
response.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Hope this helps someone.
-Tom
Last edited by navyjax2; Nov 5th, 2008 at 05:21 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|