Attaching auto generated pdf to email in asp.net app - c#

I have a very specific requirement. In my web app, I have to generate a pdf invoice from the database values, and an email body. I can easily send this using SMTP which works perfect.
But, problem is we can't rely on system to always be perfect, and this is an invoice. So, we need to open the default mail client instead of using SMTP. Right now, I have following code
//Code to create the script for email
string emailJS = "";
emailJS += "window.open('mailto:testmail#gmail.com?body=Test Mail" + "&attachment=" + emailAttachment + "');";
//Register the script for post back
ClientScript.RegisterStartupScript(this.GetType(), "mailTo", emailJS, true);
This opens the email perfectly, but no attachment is working. Path is supposed to be /Web/Temp/123.pdf.
If I use the same path as normal url like below, it opens the file in new window properly.
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", "window.open('/Web/Temp/123.pdf');", true);
So, clearly file exists, but it exists on the server. Outlook on the other hand open on client machine. So, I can't use the full determined path like C:\Web\Temp\123.pdf. If I try that, it will try to find the file on client machine, where the folder itself might not exist.
I am trying to figure out what can I do here. If there is another method I should try.
P.S. No, I can't send the email directly. That will cause a hell lot more problem in future for me.
Edit:
I also found one weird problem. If I add a double quote to the file path in attachment, a \ is added automatically. #"&attachment=""" + Server.MapPath(emailAttachment) + #"""');" gives me output as &attachment=\"C:\Web\Temp\123.pdf\".
I am trying to escape that double quote and somehow it adds that slash. I know this is a completely different problem, but thought I should mention here, instead of creating a new question.
Edit:
I tried a fixed path on localhost. So, I am basically testing the app on the same machine where file is getting stored. still, no attachment at all.
string emailJS = "";
emailJS += #"window.open('mailto:jitendragarg#gmail.com?body=Test Mail" + emailAttachment + #"&attachment=";
emailJS += #"""D:\Dev\CSMS\CSMSWeb\Temp\635966781817446275.Pdf""');";
//emailJS += Server.MapPath(emailAttachment) + #"');";
//Register the script for post back
ClientScript.RegisterStartupScript(this.GetType(), "mailTo", emailJS, true);
Updated the path to make sure it is proper. Now, it just throws error saying command line argument not valid.
Edit:
Is there any other method I can try? I have the file path on the server side. Maybe I can download the file automatically to some default folder on client machine and open from there? Is that possible?
Edit: I tried one more option.
emailJS += #"mailto:testmail#gmail.com?body=Test Mail" + #"&attachment=";
emailJS += #"\\localhost\CSMSWeb\Temp\635966781817446275.Pdf";
//emailJS += Server.MapPath(emailAttachment) + #"');";
Process.Start(emailJS);
The Process.Start line works but it does nothing at all. There is no process that starts, no error either.
Edit:
yay. I finally got the user to approve using a separate form to display the subject and body, instead of opening the default mail client. although, I would still prefer to solve this problem as is.

So, the problem here is the fact that mailto only supports direct file path for attachment. That is, path has to be local to use machine, or intranet path within the network.
In other words, path like http://yourapp/Web/Temp/123.pdf won't work, and /Web/Temp/123.pdf being essentially the same won't work either. These are not paths, but links to files that has to be downloaded and stored locally before they can be used as attachments - mailto protocol has no support for that.
However, since your application is intranet, what you could do is make sure intended users have access to some network shared folder on your server, and then provide them with network path to the file, that is \\theserver\files\123.pdf

Related

How to open default email client with attachment

I'm working on an issue to my project that when I click the button, a default email client should pop out and if there's an attachment, it should be automatically attach to the default email client like this.
I already tried a lot of methods how to do this. First I used MAPI, but the MAPI cannot detect my Default Email Client even though I already set it in Control Panel, It shows this two message box
I already searched the internet about those error but there's no definite or clear answer to me. HERE'S the code I used in MAPI.
I used also the mail:to protocol to call the default email client who's handling to the aforementioned protocol with using this line of codes.
Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()
Dim filename = Convert.toChar(34) & "C:\USERS\JOSHUA~1.HER\DOWNLO~1\ASDPOR~1.PDF" & Convert.toChar(34)
Debug.Writeline(filename)
Dim asd As String = String.Format("mailto:someone#somewhere.com?subject=hello&body=love my body&Attach={0}", filename)
proc.StartInfo.FileName = asd
proc.Start()
But still, no luck. I read a thread that the mail:to don't handle attachment anymore, but this line of code opened my default email client with body and subject, but there's no attachment. In terms of the filename variable, I already tried every path format, I read that I should use 8.3 path format. But still doesn't work.
The last method I used is extending the System.Net.MailMessage.MailMessage() following THIS answer. This works in terms of opening the default email client and attaching an attachment to a mail, but this is not editable and there's no send button on the default email client because this line of code just generating an .eml file and opening it. I'm thinking of parsing the eml file but still I don't know how to open the default email client progmatically in a new message form. Here's the photo
You guys have any idea how to make this possible? Thanks!
I am afraid that this will not be possible to do using some generic method for any mail client. But you can easily create your own solution using System.Net.Mail.SmtpClient and some simple custom UI.

Microsoft Graph API email attachment

I currently have a slight issue with attaching a word document to an email message in the Graph API.
I can send the email absolutely fine if i point the path to the location on the server, however as theres the possibility this may change, i want to use a copy which is held within the project structure in /Content/Docs/File.docx. However if i use the path ~/Content/Docs/File.docx it looks for the file in the IIS directory of Program Files. If i remove the ~ then it looks for the content directory in the C:/ drive. Does anybody know how i can get around this and use a version of the file within the application?
My code is below:
using(var msg = new MailMessage())
{
msg.Subject = string.Format("This is the subject");
msg.Body = string.Format("This is the message body");
msg.Attachments.Add(new Attachment("~/Content/Docs/File.docx"));
await SendMessage(msg, emailAddress);
}
Many Thanks,
As Edgaras pointed out, using Server.MapPath to get a fully-qualified path will help you here.

Move/Rename a file using FtpWebRequest

I am trying to moving a file from one folder to another using FtpWebRequest but i keep getting error 550. This is my code;
var requestMove = (FtpWebRequest)WebRequest.Create(Helper.PathFtp + Helper.NewFolder + file);
requestMove.Method = WebRequestMethods.Ftp.Rename;
requestMove.Credentials = networkCredential;
requestMove.RenameTo = "../" + Helper.OldFolder + file;
requestMove.GetResponse();
I can list, upload, download and delete files but moving/renaming is hopeless. I have read several posts both on stackoverflow and other sites and have tried things like setting Proxy to null and adding special characters to paths but I cant find a solution that works.
The path I use in WebRequest.Create is correct as I can delete it so it must be the RenameTo I got an issue with. Any ideas?
Error 550 means access denied. If the ftp user has sufficient rights, a program (e.g. antivirus, windows thumbnail generator etc) could have the file opened and deny your move request.
You need to contact the server administrator to get around the problem.

Mapping A Filename To Paths on A Server Asp.net

I have a problem with my code. My code is using the fileupload control to browse for a filename when you add a filename it processes it and the code runs fine on when it lives on local host, but when I put the code on our prodution server it cannot find the filenames listed by user.
For example if I use the upload control to browse to
B:\MIS\CH Intive\RPTTOFL_3.csv and the code lives on my localhost which know what that file path means it works, but if the code is moved to a production server it may or maynot know what B:/ is or B:/ maybe mapped to something else.
Even if I am browsing to a file on my C drive it will work on if the code is on the machine that the C drive is on, but it will not work if the code is on another machine because obviously that file wouldnt be on that C drive.
Private Function CSV2DataTable(ByVal filename As String) As DataTable
Using MyReader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser(filename)
MyReader.TextFieldType = FileIO.FieldType.Delimited
.
.
.
What can I do in asp.net to make the filename work correctly?
Ok lets say I get the filename and save it as so
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
now I want to pass the filename to the function above for processing. Do I pass Server.MapPath("~/") + filename as the filename? Also when I am done what do I do to delete the file from the server?
It seems that you are mixing the client and server locations of the file. Before reading the uploaded file, the server-side code must save it on the server (client-side file location is mostly irrelevant at this point). From VS help on FileUpload class: "The code that you write to save the specified file should call the SaveAs method, which saves the contents of a file to a specified path on the server." The online help topic on FileUpload control has enough information (with examples) to achieve what you need.

Calling a vb script from a asp.net / C# webpage

I have written an ASP.NET web page with C# behind that runs an existing vb script.
The idea is that the user uploads an Excel spreadsheet (.xls) using the web page, the C# does a few basic checks on the file (file type, file name etc) then saves the .xls to a network location.
The C# then passes the network path of the .xls to the vb script, which gets the required information from the .xls to create a .csv file.
Finally the .csv is passed into a stored procedure and uploaded to a database table.
The problem is that all this runs perfectly when I run the webpage locally on my machine. However when I upload the page to the webserver it does not seem to execute the vb script; instead it just sits there waiting for the script to exit.
Some quick info:
Excel is installed on the web server
The website is set to execute scripts and executables
The script is currently set to 'run as' my personal domain login (this has to change) which has admin on the web server
If I run the script on the webserver using the cmd prompt it works
I'd really appreciate any ideas on what might be going wrong... seriously, I'm pulling my hair out over this one and will consider any idea, no matter how crazy... but, and it's a big one, despite the fact that that there are many other ways of achieving the same result, I'm afraid that for a number of reasons this is what I have to work with :)
Edit
Here is how I call the script
try
{
System.Security.SecureString password = new System.Security.SecureString();
string uspw = "mypassword";
foreach (char c in uspw)
{
password.AppendChar(c);
}
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = #"cscript";
scriptProc.StartInfo.Arguments = scriptPath + " //Nologo " + uploadPath + xlsFileName;
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
scriptProc.StartInfo.UserName = "myusername";
scriptProc.StartInfo.Password = password;
scriptProc.StartInfo.UseShellExecute = false;
scriptProc.Start();
scriptProc.WaitForExit();
scriptProc.Close();
}
catch...
All of the file paths are relative.
The code seems to fail when the script is called. You can clearly see the page waiting for the script to finish. However if you watch the task manager on the web server neither cscript or excel start to run.
I've also stuck a message box right at the start of the script which does not get displayed
Edit 2
It turns out that cscript is running, I just needed to tick the 'All Users' check box in the task manager... I'm still none the wiser though!
Thanks so much in advance
Sounds like you are using automation to control the Excel application itself?
Some quick info:
Excel is installed on the web server
That is generally a bad idea, because the Excel application is not an application that is intended to be automated by a server. Thing might hang because the application is waiting for user input in a dialog somewhere. And it's not scalable for handling operations from multiple users simultaneously.
If the final goal is to extract the data from the excel file and put it in an sql server, I would rather suggest that you use the Jet OLEDB provider to retrieve the data from the excel file, either from your web application, and letting that feed the data into sql server, or let the sql server do it directly. If there is a lot of data in the excel file, the latter might be the best choice
Without seeing the code this is a blind guess - I suggest you check how you are specifying the path to the vb script - make sure you are not using an absolute path, and that the file is in the same location relative to the C# page on the server as it is on your machine.

Categories