如何将邮件消息拖放到文本框中作为C#中的输入 [英] How I drag and drop mail message to text box as input in C#
问题描述
你好,
我如何将邮件拖放到文本框中作为c#中的输入,所以之后我可以将其保存在其他路径中。
我尝试我在互联网上找到的所有解决方案,但它没有用。
我希望能够从资源管理器(现在可以正常工作)或邮件项目拖放文件,
或邮件附件(它可以工作)
我尝试过:
公共类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)
{
//对于这个程序,我们允许从资源管理器中删除一个文件
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{e.Eff ect = DragDropEffects.Copy; }
//或者告诉我们它是否是一个Outlook附件下降
else if(e.Data.GetDataPresent(FileGroupDescriptor))
{e.Effect = DragDropEffects.Copy; }
//或以上都不是
其他
{e.Effect = DragDropEffects.None;
}
// e.Effect = DragDropEffects.Copy;
}
..
..
private void MyTextBox_DragDrop(object sender,DragEventArgs e)
{
<如果(e.Data.GetDataPresent(DataFormats.FileDrop,false)== true)
{
this.Text =((string [])e.Data.GetData(DataFormats.FileDrop))[0];
}
else if(e.Data。 GetDataPresent(FileGroupDescriptor))
{
流theStream =(流)e.Data.GetData(FileGroupDescriptor);
byte [] fileGroupDescripto r = new byte [512];
theStream.Read(fileGroupDescriptor,0,512);
//用于从FileGroupDescriptor块构建文件名
StringBuilder fileName = new StringBuilder();
//这个技巧获取传递的附件的文件名
for(int i = 76; fileGroupDescriptor [i]!= 0; i ++)
{fileName.Append(Convert.ToChar(fileGroupDescriptor [i])); } $ / $
theStream.Close();
string path = Path.GetTempPath();
//将zip文件放入临时目录
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(文件未创建!+ Environment.NewLine + theFile);
}
}
其他
{
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(); //关闭文件
FileInfo tempFile = new FileInfo(theFile);
//总是好的确保我们实际创建了文件
if(!tempFile.Exists == true)
{
MessageBox.Show(File is没创建!+ Environment.NewLine + theFile);
}
}
}
你可以从这里开始使用C#进行Outlook拖放 [ ^
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);
}
}
}
You could start here Outlook Drag and Drop in C#[^]
这篇关于如何将邮件消息拖放到文本框中作为C#中的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!