A component of ASP.NET Core for creating RESTful web services that support HTTP-based communication between clients and servers.
Hi @Ian Hockaday,
but I cannot get the email details from the 'dropped' file
You cannot drag and drop emails directly from the desktop application to the page. You may need to save the email and then upload it so that the web application receives the file and parses it on the server.
If you need to parse mail using Microsoft.Office.Interop.Outlook, you can try this sample code (in web form):
aspx:
<head runat="server">
<title>DND File upload</title>
<style>
#dropZone {
width: 300px;
height: 200px;
border: 2px dashed #ccc;
text-align: center;
line-height: 200px;
font-family: Arial, sans-serif;
color: #ccc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="dropZone">Drag and drop files here</div>
<asp:FileUpload ID="fileUpload" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<br />
<br />
<asp:Label runat="server" ID="StatusLabel" Text="Upload status: " />
</form>
<script>
var dropZone = document.getElementById('dropZone');
var fileUpload = document.getElementById('<%= fileUpload.ClientID %>');
dropZone.addEventListener('dragover', function (e) {
e.preventDefault();
dropZone.style.borderColor = '#000';
});
dropZone.addEventListener('dragleave', function (e) {
e.preventDefault();
dropZone.style.borderColor = '#ccc';
});
dropZone.addEventListener('drop', function (e) {
e.preventDefault();
dropZone.style.borderColor = '#ccc';
var files = e.dataTransfer.files;
if (files.length > 0) {
fileUpload.files = files;
}
});
</script>
</body>
aspx.cs:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile)
{
try
{
// get uploaded file
HttpPostedFile postedFile = fileUpload.PostedFile;
string fileName = Path.GetFileName(postedFile.FileName);
if (Path.GetExtension(fileName).ToLower() == ".msg")
{
// save file in temp path
string tempFilePath = Path.Combine(Server.MapPath("~/"), fileName);
postedFile.SaveAs(tempFilePath);
// Parsing .msg files
Application outlookApp = new Application();
NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");
MailItem mailItem = outlookApp.Session.OpenSharedItem(tempFilePath) as MailItem;
if (mailItem != null)
{
// Extract email information
string senderEmail = mailItem.SenderEmailAddress;
string senderName = mailItem.SenderName;
string subject = mailItem.Subject;
string body = mailItem.Body;
// show info for test
Response.Write($"sender: {senderName} <{senderEmail}><br/>");
Response.Write($"Object: {subject}<br/>");
Response.Write($"Body: {body}<br/>");
// Release COM obj
Marshal.ReleaseComObject(mailItem);
}
else
{
Response.Write("Unable to parse .msg file.");
}
Marshal.ReleaseComObject(outlookNamespace);
Marshal.ReleaseComObject(outlookApp);
// Delete temp file
File.Delete(tempFilePath);
}
else
{
Response.Write("Please upload a valid .msg file.");
}
}
catch (System.Exception ex)
{
Response.Write("Parsing error: " + ex.Message);
}
}
}
Best regards,
Xudong Peng
If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.