How can I convert an Outlook MailItem object to a byte[] - c#

There are a few Stackoverflow questions about this but none of them really offer a solution.
The Scenario - I'm creating an Outlook AddIn in VS2013. The user selects emails, hits my AddIn button and the emails are sent to a webservice to be stored in a database and linked to a client. Anyone, in any location will be able to open the email to view it.
Currently I am using the MailItem.SaveAs(filePath) function, then using File.ReadAllBytes(filePath) to create a byte array that can be sent to the webservice.
I delete the file as soon as I create the byte[]:
for (int x = 0; x < Emails.Count; x++)
{
//TODO: RMc - is there a better way than saving to disk? unable to convert MailItem directly to stream...!
Guid g = Guid.NewGuid();
string filePath = "c:\\temp\\" + g.ToString() + ".msg";
Emails.ElementAt(x).SaveAs(filePath);
byte[] by = File.ReadAllBytes(filePath);
File.Delete(filePath);//no longer needed now we have byte array
//this is where I create a list of objects with all required data to send to web service
}
Writing the file to disk is slow - it creates a *.msg file that may never actually get used if no one wants to view it. So I would like to be able to save the MailItem object to a byte array directly - and then I could save that to the database and only create the *.msg file if a user requires it.
The MailItem object appears to by dynamic, and so I think this is the problem.
Can anyone offer a solution or an alternative way of achieving what I have described?

Messages in the Outlook Object Model and Extended MAPI are not streamable. You can save a message in a particular format, but it will not be an exact copy. MSG format will be the closest you can get.
Use MailItem.SaveAs(..., olMsg) to save a message as an MSG file.

Related

How to access attachment data programmatically in outlook if attachment is itself a mail

I am programmatically trying to get the attachment data in C# in following way :--
Microsoft.Office.Interop.Outlook.Attachment attachment = objMail.Attachments[attachmentIndex];
if (attachment.DisplayName.Equals("Test"))
{
const string PR_ATTACH_DATA = "http://schemas.microsoft.com/mapi/proptag/0x37010102";
byte[] attachmentData = attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA);
}
Now my code is working fine if attachment is text file or image file. But if attachment is itself a mail, it throws the exception that property is unknown or can not be found.
Please suggest in which cases / type of attachments, this property "http://schemas.microsoft.com/mapi/proptag/0x37010102" will not work and in those cases, what would be alternative property / method to get the attachment data in byte array ?
Thanks
PR_ATTACH_DATA_BIN is only present for the regular by-value attachments (PR_ATTACH_METHOD == ATTACH_BY_VALUE). Embedded message or OLE attachments do not expose that property - they use PR_ATTACH_DATA_OBJ which must be opened using IAttach::OpenProperty(IID_IStorage, ...) - take a look at the existing messages using OutlookSpy - I am its author - select the message, click IMessage button, go to GetAttachmentTable tab, double click on the attachment.
Also keep in mind that PropertyAccessor.GetProperty would only be able to retrieve that property for small attachments. For large attachments, PR_ATTACH_DATA_BIN must be opened as IStream using IAttach::OpenProperty(IID_IStorage, ...) - PropertyAccessor.GetProperty does not do that. You will need to use Extended MAPI (C++ or Delphi) or Redemption (I am its author) - it exposes RDOAttachment.AsArray / AsText / AsStream properties.
Microsoft Graph Rest API is an single end point and wrapper for most Microsoft Data including events, most office products including outlook. Best of all any language can make a request to the endpoint and retrieve the information. See the complete Docs HERE to get started.
See below code for a simple Get request for outlook attachments. Note there are other more complex implementations. Docs: https://learn.microsoft.com/en-us/graph/api/attachment-get?view=graph-rest-1.0&tabs=http Scroll through the link and you can find C#, Java, and JavaScript examples on how to implement this.
GET /me/messages/{id}/attachments/{id}
GET /users/{id | userPrincipalName}/messages/{id}/attachments/{id}
GET /me/messages/{id}/attachments/{id}/$value
GET /users/{id | userPrincipalName}/messages/{id}/attachments/{id}/$value

Programmatically insert OLE object into Outlook email with C#

I am trying to embed an Excel file into an Outlook email message. I am setting the attachment type to "OlAttachmentType.olOLE", however when the message is created, the Excel document arrives as an attachment.
Below is my code. It seems pretty straightforward, but it does not work as expected.
var application = new Microsoft.Office.Interop.Outlook.Application();
var message = (MailItem)application.CreateItem(OlItemType.olMailItem);
var path = #"C:\Excel\Workbook.xlsx";
var missing = System.Type.Missing;
message.Attachments.Add(path, OlAttachmentType.olOLE, 1, missing);
message.SaveAs(#"C:\Excel\Workbook.msg", OlSaveAsType.olMSG);
application.Quit();
Outlook Object Model would not let you insert embedded OLE objects - the best you can do is access existing ones. Inserting OLE attachments is non-trivial even on the Extended MAPI level - you will need to create a specially formatted IStorage for the attachment, then populate its data in the format that only the host that will handle it later can understand. You will also need to provide the bitmap with the preview and insert the appropriate placeholder in the RTF body.

How to read file in C# from POST data from web

Basically, I'm building a website that allows user to upload file.
From the front end (JavaScript), the user will browse a file, I can get the site to send POST data (the parameter "UploadInput" and it's value, which the value is the file)
In the backend (C#), I want to make a copy of the file and save it in a specific path.
Below is the way I did it.
var files = Request.Files;
file[0].SaveAs("\temp\\" + file[0].FileName);
The problem I ran into is that I got the error message saying index out of range. I tried Response.Write(files.Count) and it gives me 0 instead of 1.
I'm wondering where I did wrong and how to fix it, or if there's a better way of doing it.
Thanks!
Edit:
I am using HttpFox to debug. From HttpFox, I can see that under POST data, parameter is "UploadInput" and the value is "test.txt"
Edit 2:
So I tried the way Marc provides, and I have a different problem.
I am able to create a new file, however, the content is not copied over. I tried opening the new created file in notepad and all it says is "UploadInput = test.txt"
If they simply posted the file as the body content, then there will be zero "files" involved here, so file[0] will fail. Instead, you need to look at the input-stream, and simply read from that stream. For example:
using(var file = File.Create(somePath)) {
Request.InputStream.CopyTo(file);
}

Lotus Domino: How can I add a department's signature to an email sent through C# using Domino.dll

I'm writing a program where I can send mails (using domino.dll) from three different department mailboxes (each using its own mail server and nsf-file).
All three department mailboxes have a predefined mail signature such as
Regards
Department X
Since those can change anytime I don't want to hardcode the signature in my program but extract them from the mailbox/nsf-file instead and append it to the mail body (or something else if there are better approaches).
I've been looking around all day without finding a solution to this problem, so my question is: How is this achieved?
So far my code is similar to this:
public Boolean sendNotesMail(String messageText)
{
//Create new notes session
NotesSession _notesSession = new NotesSession();
//Initialize Notes Database to null;
NotesDatabase _notesDataBase = null;
//Initialize Notes Document to null;
NotesDocument _notesDocument = null;
string mailServer = #"Path/DDB";
string mailFile = #"Deparmentmail\number.nsf";
//required for send, since its byRef and not byVal, gets set later.
object oItemValue = null;
// Start the connection to Notes. Otherwise log the error and return false
try
{
//Initialize Notes Session
_notesSession.Initialize("");
}
catch
{
//Log
}
// Set database from the mailServer and mailFile
_notesDataBase = _notesSession.GetDatabase(mailServer, mailFile, false);
//If the database is not already open then open it.
if (!_notesDataBase.IsOpen)
{
_notesDataBase.Open();
}
//Create the notes document
_notesDocument = _notesDataBase.CreateDocument();
//Set document type
_notesDocument.ReplaceItemValue("Form", "Memo");
//sent notes memo fields (To and Subject)
_notesDocument.ReplaceItemValue("SendTo", emailAddress);
_notesDocument.ReplaceItemValue("Subject", subjectText);
// Needed in order to send from a departmental mailbox
_notesDocument.ReplaceItemValue("Principal", _notesDataBase.Title);
//Set the body of the email. This allows you to use the appendtext
NotesRichTextItem _richTextItem = _notesDocument.CreateRichTextItem("Body");
// Insert the text to the body
_richTextItem.AppendText(messageText);
try
{
_notesDocument.Send(false, ref oItemValue);
}
}
EDIT:
Thanks to Richard Schwartz my solution is:
object signature = _notesDataBase.GetProfileDocument("calendarprofile", "").GetItemValue("Signature");
String[] stringArray = ((IEnumerable)signature).Cast<object>().Select(x => x.ToString()).ToArray();
_richTextItem.AppendText(stringArray[0]);
The signature is stored in a profile document in the NSF file. You can use the method NotesDatabase.getProfileDocument() to access it. This method takes two arguments:
ProfileName: The profile document name that you need to find the signature is "calendarprofile". (Yes, that's right. It's actually a common profile for many functions, but the calendar developers got there first and named it. ;-))
UniqueKey: Leave this as an empty string. (It is traditionally used to store a username in profile documents in shared databases, but not used in the calendarprofile doc in the mail file.)
You access data in the profile document the same way that you access them in regular documents, e.g., using getItem(), getItemValue(), etc. For a simple text signature, the NotesItem that you are looking for is called "Signature". I notice, however, that there are also items called "Signature_1" and "Signature_2", and "SignatureOption".
If you look at the Preferences UI for setting signatures in Notes mail, you will see that there is a choice between simple text and HTML or graphic files. No doubt this choice will be reflected in the SignatureOption item, so you will probably want to check that first. I have not explored where the data goes if you use imported HTML or graphic files, so I can't say for sure whether it goes into Signature, Signature_1, Signature_2, or somewhere else. But you can explore that on your own by using NotesPeek. You can download it here. It presents a tree-style view of the NSF file. There's a branch of the tree for Profiles, and you can find the calendarprofile there. Then just play around with different settings in the Notes mail preferences and see what changes. (NotesPeek doesn't pick up changes on the fly. You have to close and re-open the profile in NotesPeek after saving changes in the Notes mail preferences dialog in order to see the changes.)
If this gets too difficult, and you want a standard solution for all mails, you might consider this product or a similar one.

Load attachments files names without attachments in c#

First, sorry for my English.
I would like do display a list of attachments names files on mailbox. Then from this list the users will can choose which attachments they want to download on hdd - for example such as in this program:
http://www.mailresender.com.ar/Read.html
I have already wrote this in Koolwired.Imap:
for (int i = 0; i < mbStructure.BodyParts.Count; i++) // i = number of parts of body
{
if (mbStructure.BodyParts[i].Attachment)
{
mbStructure = command.FetchBodyPart(mbStructure, i);
System.Console.WriteLine(mbStructure.BodyParts[i].Disposition); // Disposition contains file name
//System.Console.WriteLine(mbStructure.BodyParts[i].Data); // Data contains entire file
}
}
but the problem is that not only file name is loaded into memory but also data binary (entire attachment file is loaded by FetchBodyPart method) - so if files in attachment have long size (for instance 20 MB) all this 20 MB's also have to be loaded into memory, so displaying the list of file names will last very very long time.
Is there a way to load only attachments files names without files? Which library in c# could support this?
The answer to your question is ImapCommand.FetchBodyStructure, but as I'm browsing through the source I bet it will fail on some emails.
You may also take a look at Mail.dll IMAP component especially this blog post may be useful:
http://www.lesnikowski.com/blog/index.php/get-email-information-from-imap-fast/
Please note that Mail.dll is a commercial product I created.

Categories