I want to add an attachment to use as a header image in an email. But when I try to attach the file the path is reading from windows and not from my solution directory location. How do I do that?
var msg = new MailMessage(fromMailAdress, toEmail) { Subject = subject, Body = emailBody };
msg.Attachments.Add(new Attachment("../images/logo.jpg"));
reads from c:\windows\system32\images\logo.jpg'
I want it from the solution level project/images/logo.jpg
Here's what you'll want to do:
Create a folder (say attachment) in your solution to store your attachment files.
Use ~/attachment/filename to access the file.
Since ~/attachment/filename is a virtual path, you'll have to convert it to a physical path using Control.ResolveUrl() or a more appropriate way. Check this out for more on it: ResolveUrl without an ASP.NET Page
I don't think you mentioned what type of solution you're doing this from.
In a traditional Windows app, you have a few options. You can make sure the logo file is copied to your output directory by right-clicking on it in the project explorer, choosing Properties, and setting the "Copy to Output Directory" feature to "Copy if newer". You will then need to modify your code above to load the image from the executing assembly's path...it's been my experience that Visual Studio will create the "images" project folder.
string logopath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images/logo.jpg");
A better option, as described by DJ KRAZE, is to add your logo as a resource. If you add it to your project as a resource, then you can write it to a temporary file when it doesn't exist. Then, attach it using the path that you created for the temporary file. You can then delete it when you're done, or keep it around and only recreate it when it doesn't exist.
The third option is to upload the logo to a website and then reference it in your email message by the URL, instead of including it as an attachment. If you are creating a HTML mail message, this is probably the best solution.
Related
I have a byte array and need to save it to a file.
I have tried the below code:
File.WriteAllBytes("form.txt", byteArray);
string filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "form.txt");
I have referred https://stackoverflow.com/a/19455387/15265496 for the implementation.
I am looking for the file in Android emulator. Where will the file get saved from first line?
Should I create a form.txt in the application local folder?
Is there any alternative way to do the same?
You can find the appropriate folder using Environment.SpecialFolder.LocalApplicationData
string fileExactLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "form.txt");
You can find more information at https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows
You cannot access the file system of an Android emulator instance via explorer or finder.
As per the Microsoft docs, if you want to store a file in Android, you can always use the application's files folder which is accessible through:
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
A file saved to that folder will go to
/data/user/0/com.companyname/files
I need save a MailMessage to .msg file. In this article have a solution but when I save as .msg file, it does not work in MS Outlook. It only great work when I save as.eml file.
How to save MailMessage object to disk as *.eml or *.msg file
Note that I get when open the .msg file:
Cannot open file: path. The file may not exist, you may not have permission to open it, or it may open in another program. Right-click the folder that contains the file, and then click Properties check your permisstions for the folder.
Thank all.
In Interop Outlook, this is how to locally save a mail as .msg.
mailItem.SaveAs(#"c:\path\to\save\mail.msg", Outlook.OlSaveAsType.olMSG);
How exactly are you creating the MSG file? It is completely different from an EML file - see https://stackoverflow.com/questions/16229591/difference-between-a-msg-file-and-a-eml-file/16230261#16230261
MSG file format is a binary IStorage file, and its format is documented. You can parse your EML (MIME) file and copy one property at a time to a programmatically created MSG file.
If using Redemption is an option (I am its author), you can use Session.CreateMessageFromMsgFile to create a new MSG file and RDOMail.Import method to import your existing EML file.
set Session = CreateObject("Redemption.RDOSession")
set Msg = Session.CreateMessageFromMsgFile("c:\temp\test.msg")
Msg.Sent = true '//since Import does not copy this property
Msg.Import("c:\temp\test.eml", 1024) ' //1024 is olRfc822
Msg.Save
I have an application in Visual Studio C# which includes saving into a text file, how can I have a .exe sent to another computer and not have an exception in saving?
I need to send a .exe file by email (Yes it's possible) and this application includes saving the state of the game. How can I send this .exe file and let the user be able to save in his computer please?
The problem is that when I send my application's executable file on another computer, I'm getting an exception in saving. Because on the other computer I don't have the text file which I'm saving the game.
I am saving here :
StreamWriter myFile = File.CreateText(Directory.GetCurrentDirectory()+"//ConnectFour.txt");
in the obj/Debug/ of the project..
Thanks for your help :)
Sending an executable should work just fine.
Make sure the other computer has the appropriate Microsoft .NET Framework installed.
Latest framework installer: MSDN
Also, make sure the path inwhich you're saving the file to exists on the remote computer. For example, if you're trying to save to the D:\ drive and it doesn't exist. You will get an exception.
Most likely current location is not writable by current user.
Using "current directory" is dangerous as you have no control over where application is launched from. It is very useful for command line utilities, but not so much for regular windowed applications. Use location that you know you can save files to - i.e. "My Documents".
var filePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\",
ConnectFour.txt");
using(var myFile = File.CreateText(filePAth))
{
// save data here.
}
The problem when sending executables by email are the anti-virus-scanners. Some of them refuse e-mails containing executables. Others accept the mails but delete the attachment.
The solution consists in hiding the executable. This can be done by zipping the executable and sending the zip-file.
Often this is enough to solve the problem but some anti-virus-scanners are very smart and even recognize executables within the zip-attachment. The solution here is to encrypt the zip-file with a password. I often just use the password "pwd" and mention it in the e-mail text. The anti-viruses are not (yet) smart enough to understand this text.
UPDATE
Now I understand your problem. It has nothing to do with sending the executable.
An application can determine from which directory it has been started like this
string dir = System.IO.Path.GetDirectoryName(
System.Windows.Forms.Application.ExecutablePath
);
An alternative is (if you don't have a reference to WinForms):
string dir = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetEntryAssembly().Location
);
You probably do not have sufficient privileges to save the file on the remote machine. If you give us more information on the exact exception that is being thrown (type of exception, message, stack trace, etc) you will get a more accurate answer.
How can I get the path of a file on my computer or on the local area network.
I would to show a window that allows me to browse the file system when I click a button, and I want to select a file and get the path to the file. How can I do this?
P.S. I'm not looking to upload the file; I just want to get the path.
The web application is running on the server, and you don't have access to the client file system at all. Can you imagine how much of a vulnerability that would be? I know I don't want the sites I visit inspecting my file system...
EDIT: Question is for a WinForms application
For a WinForms application, you can use the OpenFileDialog, and extract the path with something like this:
If you're looking for the file path:
string path = OpenFileDialog1.FileName; //output = c:\folder\file.txt
If you're looking for the directory path:
string path = Path.GetDirectoryName(OpenFileDialog1.FileName); //output = c:\folder
In general, the System.IO.Path class has a lot of useful features for retrieving and manipulating path information.
To do this in VB.NET use the FolderBrowserDialog.
Due to security restrictions browsers include for user safety, you can't manipulate the client file system directly. You can only use to let them pick a file, and even then it only sends the filename, not the whole path.
The FileSystem API in HTML5 allows for file manipulation, but only within a sandbox for your site specifically, not browsing across the network or other files on the client system.
Instead, provide your users easy steps on how they should use My Computer (or whatever equivalent on other OS's) to navigate to the file and copy & paste the path into a simple text input box.
Have you taken a look at the Path class from System.IO ?
I'm trying to keep track of the email created after I "send" it using SmtpClient.Send.
I have it configured to write to a directory by configuring my app.config to use specifiedPickupDirectory.
What I'd like to gain access to is the name of the file that was used, so that I can periodically check and make sure that my mail server has retrieved it and sent it along.
Any suggestions?
Perhaps try initially creating the file in a temporary directory. Make sure that it is the only file in the directory. Use Directory.GetFiles to find the file name and save it to a variable. Then move the file to the real directory.