Searching Local Eml Files via MimeKit - c#

I am trying to read, show and search EML files with attachments downloaded on my computer. To do that; I am using MimeKit v2.1.0. It's okay when I read and show the files with using MimeMessage. However, I have many messages and need to be able to search with a few words. As i understand, MimeKit has no search option. What can be able to do that is MailKit but this time, I could not read local EML files. I have searched for couple days to find a solution but returned with empty hands.
So, to sum up, I am looking for a way either search with MimeKit or read local EML files with MailKit. Any help appreciated.

MailKit's search APIs are for IMAP. Granted, someone could implement the IMailFolder interface for local messages (in mbox or Maildir format?), but that has not been done by anyone afaik.
That said, you can do this:
static bool Search (string fileName, string text)
{
var message = MimeMessage.Load (fileName);
var body = message.TextBody;
return body != null && body.Contains (text);
}

Related

Detect file type uploaded in discord.net

I'm writing a Discord bot in C# using the Discord.Net library. I've got a working bot and it scans messages and deletes those containing the text "gif" "giphy" etc. This removes all links to gifs that Discord automatically embeds. However, a directly uploaded .gif file is not detected.
I'd like a method of, when a file is uploaded to a Discord text channel, receiving the type of that file, preferably in string format like "txt" or "png" etc.
Assuming you have the message as SocketMessage
You can do yourSocketMessage.Attachments to get the collection of attachments that exists in this message, nothing will be in the collection if nothing was attached.
You could iterate through that collection, which should return a Attachment. And Attachment has a Url property which you can use alongside with System.IO.Path.GetExtension(url)
(Suprisingly, the function has been proven that it works with url)

Get new incoming emails and write their flat file link to a file

I want to get all incoming mails for a certain mailbox and write their Subject , date and flat file link (e.g /-FlatUrlSpace-/5a194b8c1256794581cb898e6b93c34f-112ca9b4/68fcbe48ba7a604086372757c4cea3de-44e6af ) to a file.
How would i go about doing this codewise, i've never worked with the outlook object and know very little about exchange so i'm hoping for some help.
Is there an event for new incoming mails?
Is there a property where i can get the flaturlspace, cause i can't see anything related to it
thx in advance
The way that I've done this in the past is by using WEBDAV. I seem to recall you can request flaturlspace.
Check out this.

Looking for a pop3 reader class in C# with SSL support

I was looking around and there is couple of projects but they all seem to be outdated,
should i use those? or is there a new out the box pop3 class that I can't find in msdn.
anyhow i'm not doing a client that needs to send out so no SMTP is needed, more like a bot that sorts out the emails and reads them., any ideas?
Cheers!
Take a look at Mail.dll POP3 client. It supports SSL, is easy to use, and supports parsing complex MIME structures:
using(Pop3 pop3 = new Pop3())
{
pop3.ConnectSSL("pop3.server.com");
pop3.Login("user", "password");
foreach (string uid in pop3.GetAll())
{
IMail email = new MailBuilder()
.CreateFromEml(pop3.GetMessageByUID(uid));
Console.WriteLine(email.Subject);
}
pop3.Close(true);
}
Please note that this is a commercial product that I developed
You can download Mail.dll here:
http://www.lesnikowski.com/mail/
I discovered OpenPOP on another thread, which seems to be my library of choice at the moment for this very task.
I built one a few years back that's posted on code project and it is dated (http://www.codeproject.com/KB/IP/NetPopMimeClient.aspx). If you are interested I can send you a copy of the latest source code which was never posted to CP.
I ended up using Dart Mail as a replacement for the solution I developed posted on CP. The main reason I ended up using Dart Mail is for the Mime Parsing facilities that it has which really ended up being the main problem with the solution I developed. IIRC Dart Mail is pretty reasonable and may be worth a look if you need something that is robust.

Send eml files saved on disk

I am creating eml's and saving them to a directory using procedure mentioned over here.
I want to know how to send these eml files?
I tried using SMTPClient class's object but it takes MailMessage object as its parameter and I couldn't find and way to create an object of type MailMessage using these saved eml files.
Loading an EML file correctly is not as easy as it looks. You can write an implementation working in 95% cases within few days. Remaining 5% would take at least several months ;-). I know, becase I involved in developing one.
Consider following dificulities:
unicode emails
right-to-left languages
correcting malformed EML files caused by well known errors in popular mail clients and servers
dealing with S/MIME (encrypted and signed email messages)
dealing correctly with several methods of encoding attachments
dealing with inline images and stylesheets embedded into HTML emails
making sure that it parses correctly a MIME torture message from Mike Crispin (coauthor of Mime and IMAP RFCs)
making sure that malformed message will not result in buffer overun or other application crash
handling hierarchical messages (message with attached messages)
making sure that it handles correctly very big emails
Maturing of such parser takes years and continuous feedback for it's users. Right now is no such parser included in the .NET Framework. Until it changes I would sugest getting a thrid party MIME parser from an established vendor.
Following code uses our Rebex Secure Mail component, but I'm sure that similar task could be replicated easily with components from other vendors as well.
The code is based on Mail Message tutorial.
// create an instance of MailMessage
MailMessage message = new MailMessage();
// load the message from a local disk file
message.Load("c:\\message.eml");
// send message
Smtp.Send(message, "smtp.example.org");
Use EMLReader to retrieve data from .eml file. It contains all the data you need to create a MailMessage object like From, To, Subject, Body & a whole lot more.
FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite);
EMLReader reader = new EMLReader(fs);
fs.Close();
MailMessage message = new System.Net.Mail.MailMessage(reader.From, reader.To, reader.Subject, reader.Body);
If you're a Microsoft shop and have an Exchange server anyway, then there's another solution which is much, much easier than everything else suggested here:
Each Exchange server has a pickup directory configured out of the box.
By default, it's %ExchangeInstallPath%TransportRoles\Pickup.
You just copy the .eml files to that directory, and Exchange automatically will send the mails.
Read this TechNet article for more information:
Pickup directory and Replay directory
As others demonstrated, EML is just not a good way to serialize a mail message. You might be better off by saving your mails in another format. While there are several serialization engines in the .Net framework to serialize any object, you might also consider just saving the components of your mails, like addresses, body, files to be attached in base64, in an Xml file of your own design.
Below is an example to get you started:
<?xml version="1.0" encoding="utf-8"?>
<mail>
<to display="Thomas Edison" address="tedison#domain.com" />
<body>
Hi Thomas,
How are you doing?
Bye
</body>
<attachment name="MaryLamb.wav">
cmF0aWUgYWFuIGluIFBERi1mb3JtYWF0LiBEZSBmYWN0dXVyIGlzIGVlbiBvZmZpY2ll
ZWwgZ2VzaWduZWVyZA0KZG9jdW1lbnQgdmFuIEV1cm9maW5zIE9tZWdhbSBCVi4gRGUg
c2lnbmF0dXJlIGt1bnQgdSB2ZXJpZmnDq3Jlbi4NCg0KVm9vciBoZXQgdmVyaWZpw6ty
...
</attachment>
</mail>
Added advantage would be that, unlike with creating EML, you do not need the smtpClient to build the concept mail files.
Xml is extremely easy to create and parse in C#.
You did not tell the rationale of saving EML's. If long term archival would be a goal, xml might have an advantage.
Do What i did ... give up.
Building the MailMessage object seems to be the focus i have a similar questions outstanding on here too ...
How do i send an email when i already have it as a string?
From what i've seen the simplest way to do this is to use a raw socket to dump the entire .eml file contents up to the mail server as is and let the mail server figure out the hard stuff like from, to subject, ect by parsing the email using it's engine.
The only problem ... RFC 821 ... such a pain, i'm trying to figure out a clean way to do this and read mail already in the mailbox quickly too.
EDIT:
I found a clean solution and covered it in my thread :)
How do i send an email when i already have it as a string?
You can do this with Windows Server’s built-in SMTP server, the same way as in the previous answer using Exchange.
Drop the .eml file to C:\inetpub\mailroot\Pickup and the raw message will be sent (local or remote).
You can forward messages by simply inserting a line in the top:
To: email#address.com
You can manipulate the mail header further if you require.
For the records:
In Nuget Packager Console write:
Install-Package LumiSoft.Net.dll
Then in your Code:
using (FileStream fs = new FileStream( cacheFileName, FileMode.Open, FileAccess.Read ))
using (LumiSoft.Net.SMTP.Client.SMTP_Client client =
new LumiSoft.Net.SMTP.Client.SMTP_Client())
{
client.SendMessage( fs );
}

Wanting to email from C# with attachment like MS Word

This is really odd that I can't seem to find out how to do this, so I'm wondering if I'm missing something obvious.
I'm wanting to put a menu on our application like is found in Word and Excel, File -> Send To -> Mail Recipient (As Attachment)
Our requirements are to create and display the email with the attachment, just like Word and Excel do, not to send it automatically.
We used to be able to save the file to the temp folder and use:
Shell.Execute("mailto:my.email.com?subject=File&attachment="c:\temp.txt");
I've tried the &attach, &attachment in both VB.NET and C# with quotes, double quotes, etc. I've also tried System.Net.Mail but don't see anywhere that you can display the email, it only seems to be able to create and send.
We can't assume a default email client, it could be Outlook Express, Outlook version 2000, 2003, or 2007, or lotus notes, or ... Don't know. We have a commercial application so I don't think we can assume a specific application. Like MS Word, it needs to work for whatever is installed (or isn't installed).
I've done this using Outlook interop from Visual Studio Tools for Office:
using IntOut = Microsoft.Office.Interop.Outlook;
...
IntOut.Application app = new IntOut.Application();
IntOut.MailItem item = (IntOut.MailItem)app.CreateItem(
IntOut.OlItemType.olMailItem);
item.Subject = "Hello world";
item.Body = "Hello!";
item.Display(false); // set to true to make mail window modal
You can find some samples on MSDN here.
For the general case, there isn't a way to do this. Here's Microsoft's documentation: http://msdn.microsoft.com/en-us/library/aa767737(VS.85).aspx
If you can provide the mail client, you may get a better answer.

Categories

Resources