Ae.Net.Mail - IMAP - Get only unseen messages - c#

I have a Windows service using the AE.Net.Mail IMAP client with G-mail. The service runs every x minutes, retrieves inbox messages, applies some business logic, and marks them as deleted. This all works.
However, Gmail leaves them in the inbox so subsequent calls fetch them again. I can skip them by looking at the Seen or Deleted flags but I'd rather not get them at all. Unless someone goes in and cleans up the inbox frequently fetches will grow exponentially.
I've experimented with Gmails expunge options but they don't seem to have any effect.

I know this is a bit old post but just in case anybody is looking for a way to do this can try :
ImapClient ic = new ImapClient("imap.gmail.com", "name#gmail.com", "pass", AuthMethods.Login, 993, true);
var mailMessage = ic.SearchMessages(SearchCondition.Unseen(), false, true).ToList();
Now you can loop through the mailMessage for unread email.

I decided to just filter them out when I retrieved them. Not ideal, but it works
List<AE.Net.Mail.MailMessage> mm = _client.GetMessages(0, 10, false, false)
.Where(m => !m.Flags.HasFlag(Flags.Seen)
&& !m.Flags.HasFlag(Flags.Deleted)).ToList();

I know this is a bit old post but just in case anybody is looking for a way to do this can try move:
AE.Net.Mail.Imap.Mailbox[] mailBoxes = client.ListMailboxes(string.Empty, "*");
bool existeMailBoxArquivo = false;
const string ARQUIVO = "ARQUIVO";
foreach (var mailBox in mailBoxes)
{
if (mailBox.Name == ARQUIVO)
existeMailBoxArquivo = true;
}
if (!existeMailBoxArquivo)
client.CreateMailbox(ARQUIVO);
client.MoveMessage(msg.Uid, ARQUIVO);
So you move the messages to a file folder, and when you browse again, just select the main box to search

Related

how to access email subfolders? [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I am looking for a method of reading emails using Pop3 in C# 2.0. Currently, I am using code found in CodeProject. However, this solution is less than ideal. The biggest problem is that it doesn't support emails written in unicode.
I've successfully used OpenPop.NET to access emails via POP3.
downloading the email via the POP3 protocol is the easy part of the task. The protocol is quite simple and the only hard part could be advanced authentication methods if you don't want to send a clear text password over the network (and cannot use the SSL encrypted communication channel). See RFC 1939: Post Office Protocol - Version 3
and RFC 1734: POP3 AUTHentication command for details.
The hard part comes when you have to parse the received email, which means parsing MIME format in most cases. You can write quick&dirty MIME parser in a few hours or days and it will handle 95+% of all incoming messages. Improving the parser so it can parse almost any email means:
getting email samples sent from the most popular mail clients and improve the parser in order to fix errors and RFC misinterpretations generated by them.
Making sure that messages violating RFC for message headers and content will not crash your parser and that you will be able to read every readable or guessable value from the mangled email
correct handling of internationalization issues (e.g. languages written from righ to left, correct encoding for specific language etc)
UNICODE
Attachments and hierarchical message item tree as seen in "Mime torture email sample"
S/MIME (signed and encrypted emails).
and so on
Debugging a robust MIME parser takes months of work. I know, because I was watching my friend writing one such parser for the component mentioned below and was writing a few unit tests for it too ;-)
Back to the original question.
Following code taken from our POP3 Tutorial page and links would help you:
//
// create client, connect and log in
Pop3 client = new Pop3();
client.Connect("pop3.example.org");
client.Login("username", "password");
// get message list
Pop3MessageCollection list = client.GetMessageList();
if (list.Count == 0)
{
Console.WriteLine("There are no messages in the mailbox.");
}
else
{
// download the first message
MailMessage message = client.GetMailMessage(list[0].SequenceNumber);
...
}
client.Disconnect();
HOWTO: Download emails from a GMail account in C# (blogpost)
Rebex Mail for .NET (POP3/IMAP client component for .NET)
Rebex Secure Mail for .NET (POP3/IMAP client component for .NET - SSL enabled)
My open source application BugTracker.NET includes a POP3 client that can parse MIME. Both the POP3 code and the MIME code are from other authors, but you can see how it all fits together in my app.
For the MIME parsing, I use http://anmar.eu.org/projects/sharpmimetools/.
See the file POP3Main.cs, POP3Client.cs, and insert_bug.aspx
You can also try Mail.dll mail component, it has SSL support, unicode, and multi-national email support:
using(Pop3 pop3 = new Pop3())
{
pop3.Connect("mail.host.com"); // Connect to server and login
pop3.Login("user", "password");
foreach(string uid in pop3.GetAll())
{
IMail email = new MailBuilder()
.CreateFromEml(pop3.GetMessageByUID(uid));
Console.WriteLine( email.Subject );
}
pop3.Close(false);
}
You can download it here at https://www.limilabs.com/mail
Please note that this is a commercial product I've created.
call me old fashion but why use a 3rd party library for a simple protocol. I've implemented POP3 readers in web based ASP.NET application with System.Net.Sockets.TCPClient and System.Net.Security.SslStream for the encryption and authentication. As far as protocols go, once you open up communication with the POP3 server, there are only a handful of commands that you have to deal with. It is a very easy protocol to work with.
I wouldn't recommend OpenPOP. I just spent a few hours debugging an issue - OpenPOP's POPClient.GetMessage() was mysteriously returning null. I debugged this and found it was a string index bug - see the patch I submitted here: http://sourceforge.net/tracker/?func=detail&aid=2833334&group_id=92166&atid=599778. It was difficult to find the cause since there are empty catch{} blocks that swallow exceptions.
Also, the project is mostly dormant... the last release was in 2004.
For now we're still using OpenPOP, but I'll take a look at some of the other projects people have recommended here.
HigLabo.Mail is easy to use. Here is a sample usage:
using (Pop3Client cl = new Pop3Client())
{
cl.UserName = "MyUserName";
cl.Password = "MyPassword";
cl.ServerName = "MyServer";
cl.AuthenticateMode = Pop3AuthenticateMode.Pop;
cl.Ssl = false;
cl.Authenticate();
///Get first mail of my mailbox
Pop3Message mg = cl.GetMessage(1);
String MyText = mg.BodyText;
///If the message have one attachment
Pop3Content ct = mg.Contents[0];
///you can save it to local disk
ct.DecodeData("your file path");
}
you can get it from https://github.com/higty/higlabo or Nuget [HigLabo]
I just tried SMTPop and it worked.
I downloaded this.
Added smtpop.dll reference to my C# .NET project
Wrote the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmtPop;
namespace SMT_POP3 {
class Program {
static void Main(string[] args) {
SmtPop.POP3Client pop = new SmtPop.POP3Client();
pop.Open("<hostURL>", 110, "<username>", "<password>");
// Get message list from POP server
SmtPop.POPMessageId[] messages = pop.GetMailList();
if (messages != null) {
// Walk attachment list
foreach(SmtPop.POPMessageId id in messages) {
SmtPop.POPReader reader= pop.GetMailReader(id);
SmtPop.MimeMessage msg = new SmtPop.MimeMessage();
// Read message
msg.Read(reader);
if (msg.AddressFrom != null) {
String from= msg.AddressFrom[0].Name;
Console.WriteLine("from: " + from);
}
if (msg.Subject != null) {
String subject = msg.Subject;
Console.WriteLine("subject: "+ subject);
}
if (msg.Body != null) {
String body = msg.Body;
Console.WriteLine("body: " + body);
}
if (msg.Attachments != null && false) {
// Do something with first attachment
SmtPop.MimeAttachment attach = msg.Attachments[0];
if (attach.Filename == "data") {
// Read data from attachment
Byte[] b = Convert.FromBase64String(attach.Body);
System.IO.MemoryStream mem = new System.IO.MemoryStream(b, false);
//BinaryFormatter f = new BinaryFormatter();
// DataClass data= (DataClass)f.Deserialize(mem);
mem.Close();
}
// Delete message
// pop.Dele(id.Id);
}
}
}
pop.Quit();
}
}
}

Retrieve documents printed from printer using c#

This days I've been developing a service that sends a PDF file to a network printer and effectively print it using C# and the GhostscriptProcessor library.
But now I'm really stuck with the next (and last) step I want to make. I need to know wether or not the file was really printed. I tried everything I could (for instance I tried implementing this powershell script but I'm not familiar at all with powershell and I got depressed since I got too many errors I can't solve) but I can't find the answer.
Is there ANY way using C# (any library) to retrieve if a document has been printed. Or retrieve the whole log of documents printed? Any script I can call through C# (or not, I am able to circumvent that) that tells me the information I need?
I'd like to add that I have access to the printer using the System.Drawing.Printing library as follows:
var printServer = new PrintServer();
var myPrintQueues = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
foreach (PrintQueue pq in myPrintQueues)
{
pq.Refresh();
string printerName = "ES7470 MFP(PCL)";
if (!pq.Name.ToUpper().Contains(printerName.ToUpper())) break;
PrintJobInfoCollection jobs = pq.GetPrintJobInfoCollection();
//And here I can use pq or jobs but I can't retrieve the log at all.
}
I think the problem is the PrintServer, I'm not in your environment so I cant tell your setup but LocalPrintServer.GetPrintQueue should do the trick.
string printerName = "ES7470 MFP(PCL)";
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueueCollection printQueues = localPrintServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
if (printQueues == null) return false;
PrintQueue queue = printQueues.Where(x => x.Name.Equals(printerName)).FirstOrDefault();
I'm guessing your application will know if someone printed something, otherwise you'll have to poll the Printer Queue each second or two to find out if a job went to the printer...

C# - Mail confirmation Outlook

I am developing an app that checks an Outlook mail account, finds all the attachments and then print them out. At this point, the mails analyzed are moved to another folder.
I have only one problem: sometimes, I receive some mails with reading confirmation. The app checks the attachment, and when it has to move the mail, it freezes. Then a popup appear in Outlook, about sending or not sending the reading confirmation.
Now, I want to make this programmatically, I always want to send a reading confirmation when it is requested.
I found a property (ReadReceiptRequested), set to true if there is a reading confirmation to send, but I don't know how to send it.
Here a piece of the code I use:
//I store all the emails in a List<Outlook.MailItem> named emails
Outlook.Application myApp = new Outlook.Application();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
//Check if the mail has read confirmation
if (emails[right_index].ReadReceiptRequested)
{
//How to send read confirmation?
}
//I read the mail, then I move it to another folder
emails[indice_scelto].UnRead = false;
emails[indice_scelto].Move(mapiNameSpace.Folders["New folder"]);
Could you help me?
Thanks in advance!
You can do that on the Extended MAPI level (C++ or Delphi) - call IMessage::SetReadFlag() - pass 0 to send read receipts or SUPPRESS_RECEIPT otherwise.
If Redemption is an option (I am its author), it exposes the RDOMail.MarkRead method that takes a SuppressReceipt Boolean parameter.
The Outlook object model doesn't provide any property or method. All you can is to set the UnRead property to false and Save the item.
Then you can begin synchronizing a user's folders using the specified Send\Receive group using the Start method of the SyncObject class. The Namespace class provides the SyncObjects property which returns a SyncObjects collection containing all Send\Receive groups. For example:
Public Sub Sync()
Dim nsp As Outlook.NameSpace
Dim sycs As Outlook.SyncObjects
Dim syc As Outlook.SyncObject
Dim i As Integer
Dim strPrompt As Integer
Set nsp = Application.GetNamespace("MAPI")
Set sycs = nsp.SyncObjects
For i = 1 To sycs.Count
Set syc = sycs.Item(i)
strPrompt = MsgBox( _
"Do you wish to synchronize " & syc.Name &"?", vbYesNo)
If strPrompt = vbYes Then
syc.Start
End If
Next
End Sub
I suppose then you can move the item wherever you need.

Get sent message in Outlook Addin

I am facing some problems trying to get the sent message from an Outlook plugin.
In onItemSend event, I open a dialog where it shows some fields, with message information such as recipients, subject, etc and a button that will save these information into our DB. Another requirement is to save a copy of the sent message and this is where I got stuck...
I could save the message using the SaveAs method but the problem is when I open the message, it shows:
This message has not been sent. This message will be sent via
Microsoft Exchange
causing some problems with users, making them think that the message was not sent.
During my searches, I found this thread where another person had the same problem and the solution was to use the message as PostItem instead of MailItem, once the PostItem is created in sent state. Also, we should set the MessageClass property to IPM.Note and delete PR_ICON_INDEX
Here is the code that I am using to do the steps above. I found this code here and changed a little bit:
PostItem postItem = this._email.Application.CreateItem(OlItemType.olPostItem);
postItem.MessageClass = "IPM.Note";
PropertyAccessor pa = postItem.PropertyAccessor;
pa.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003", -1);
postItem.Save();
NameSpace session = postItem.Session;
string postItemEntryID = postItem.EntryID;
Marshal.ReleaseComObject(postItem);
Marshal.ReleaseComObject(pa);
MailItem newMessage = session.GetItemFromID(postItemEntryID) as MailItem;
newMessage.BCC = this._email.BCC;
newMessage.Body = this._email.Body;
newMessage.BodyFormat = this._email.BodyFormat;
newMessage.CC = this._email.CC;
newMessage.HTMLBody = this._email.HTMLBody;
//Hard coded path just for illustration
newMessage.SaveAs("C:\\Temp\\MSG\test.msg", OlSaveAsType.olMSG);
The code above creates a postitem object, set some of the properties and save to the path correctly, but it has the following problems:
After executing postItem.save, to create the postitem message, it creates a read message in inbox folder
After saving the messages, I have compared the files and the size where significant, the original message size was 580kb and the postitem saved message was 52kb. It seems it did not make a copy of the message
It lost some of the of the images embedded in message, such as signature images, showing a red X in place.
How can I get/create a message, with the exact message content, recipients, attachments, properties, etc (clone kind) with sent state, without creating another message inside inbox folder?
Thank you
I would not do this to a message that Outlook is trying to send. You can
Process the Items.ItemAdd event on the Sent Items folder. By that time the message is sent and all the sender related properties are set.
You can "fix" the created MSG file by removing the unsent flag. You can do that using Redemption (I am its author) - call RDOSession.GetMessageFromMsgFile / RDOMail.Sent = true / RDOMail.Save. Keep in mind that the sender information might not yet be set.
i would not go that way with the "postitem" further, somehow it does not look the perfect way for me.
the Problem is that you are copying the item bevor it is sent. Therefore the copy says it has not been sent.
If you do not need the "normal" copy which is saved in the "sent items"-Folder, you could just Change the Folder where the item is saved with
Set mailitem.SaveSentMessageFolder = someother Folder '(which is defined as Outlook.folder)
if that is not possible, then I would make an inspection (in ThisOutlookSession) of the "sent items" Folder and make the copy-action for every new item in there. If you don't know how to let me know, then I copy you some code to bring you on the way.
another question just because Iam curious: why are you opening the form and waiting for someone to hit the ok-button, instead of saving the data into your db straight away?

Equals sign magically appears in message sending to pager

I am sending a mail to an email address which forwards messages to a pager. The message I am sending is
"Oth info; Thankyou testing now complete just be aware that 34a door
will open and shut when pager messages are sent with the CFSRES lt1".
but client receives it on his pager as
"Oth info; Thankyou testing now complete just be aware that 34a door
will op= en and shut when pager messages are sent with the CFSRES
lt1".
Does anyone know why the equals sign shows up in open changed to op= en. I know that special characters may some times be changed, like spaces can become %20, but open does not contain any special character, so as far as I know, nothing should happen to it.
This is a sign of Quoted-printable encoding
QP works by using the equals sign "=" as an escape character. It also limits line length to 76, as some software has limits on line length.
So you may try to split the message into multiple lines in an attempt to prevent escape characters being added by the forwarder.
I search for net and found the solution, this links solve my problem. Here is some userfull code, i need to provide the CreateAlternateViewFromString.
MailMessage emailmsg = new MailMessage("from#address.co.za", "to#address.co.za")
emailmsg.Subject = "Subject";
emailmsg.IsBodyHtml = false;
emailmsg.ReplyToList.Add("from#address.co.za");
emailmsg.BodyEncoding = System.Text.Encoding.UTF8;
emailmsg.HeadersEncoding = System.Text.Encoding.UTF8;
emailmsg.SubjectEncoding = System.Text.Encoding.UTF8;
emailmsg.Body = null;
var plainView = AlternateView.CreateAlternateViewFromString(EmailBody, emailmsg.BodyEncoding, "text/plain");
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);
SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);

Categories

Resources