Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
How can I drag and drop mail message to text box as input in c# , so after that I can save it in a other path.
I try all the solution I found in internet and it did not work.
I want to be able to drag and drop file from explorer (it work now) or mail item,
or mail attachments ( it work)

What I have tried:

public class MyTextBox : TextBox
{
public Outlook.Application _Outlook = new Outlook.Application();
public MyTextBox()
{
AllowDrop = true;
Multiline = true;
DragDrop += new DragEventHandler(MyTextBox_DragDrop);
DragEnter += new DragEventHandler(MyTextBox_DragEnter);

}
...
..
..
private void MyTextBox_DragEnter(object sender, DragEventArgs e)
{
// for this program, we allow a file to be dropped from Explorer
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{ e.Effect = DragDropEffects.Copy; }
// or this tells us if it is an Outlook attachment drop
else if (e.Data.GetDataPresent("FileGroupDescriptor"))
{ e.Effect = DragDropEffects.Copy; }
// or none of the above
else

{ e.Effect = DragDropEffects.None;

}
// e.Effect = DragDropEffects.Copy;
}
..
..
private void MyTextBox_DragDrop(object sender, DragEventArgs e)
{

{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
this.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
}
else if (e.Data.GetDataPresent("FileGroupDescriptor"))
{

Stream theStream = (Stream)e.Data.GetData("FileGroupDescriptor");
byte[] fileGroupDescriptor = new byte[512];
theStream.Read(fileGroupDescriptor, 0, 512);
// used to build the filename from the FileGroupDescriptor block
StringBuilder fileName = new StringBuilder("");
// this trick gets the filename of the passed attached file
for (int i = 76; fileGroupDescriptor[i] != 0; i++)
{ fileName.Append(Convert.ToChar(fileGroupDescriptor[i])); }
theStream.Close();
string path = Path.GetTempPath();
// put the zip file into the temp directory
string theFile = path + fileName.ToString();
this.Text = theFile;
if (theFile.Contains(".msg"))
{

Outlook.Explorer oExplorer = _Outlook.ActiveExplorer();
Outlook.Selection oSelection = oExplorer.Selection;
Outlook.MailItem mi = (Outlook.MailItem)oSelection;
mi.SaveAs(Path.GetTempPath() + mi.Subject.ToString() + ".msg",Outlook.OlSaveAsType.olMSG);
theFile = Path.GetTempPath() + mi.Subject.ToString() + ".msg";

this.Text = theFile;
FileInfo tempFile = new FileInfo(theFile);
if (!tempFile.Exists == true)
{
MessageBox.Show("File was not created!" + Environment.NewLine + theFile);
}
}
else
{

MemoryStream ms = (MemoryStream)e.Data.GetData(
"FileContents", true);

byte[] fileBytes = new byte[ms.Length];

ms.Position = 0;
ms.Read(fileBytes, 0, (int)ms.Length);

FileStream fs = new FileStream(theFile, FileMode.Create);
fs.Write(fileBytes, 0, (int)fileBytes.Length);

fs.Close(); // close the file

FileInfo tempFile = new FileInfo(theFile);

// always good to make sure we actually created the file
if (!tempFile.Exists == true)
{
MessageBox.Show("File was not created!" + Environment.NewLine + theFile);
}
}
}
Posted
Updated 25-Aug-18 23:10pm

1 solution

You could start here Outlook Drag and Drop in C#[^]
 
Share this answer
 

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