I am trying to get the sent folders to display but it shows that the folder has no children in it. All folders are empty except inbox. I am using the following code.
using (var client = new ImapClient())
{
client.Connect(credentials.incoming_host, (int)credentials.incoming_port, credentials.incoming_ssl); //for SSL
client.Authenticate(credentials.email, credentials.password);
client.Inbox.Open(FolderAccess.ReadOnly);
var sentFolder= client.GetFolder(MailKit.SpecialFolder.Sent);
var Folders = client.GetFolders(client.PersonalNamespaces[0]);
client.Disconnect(true);
}
I tried sending an email using the same folder, and then append it like:
var sentFolder = imapclient.GetFolder(SpecialFolder.Sent);
sentFolder.Append(message);
My outlook did detect it and added into the sent folder.
From the MailKit README:
If the IMAP server supports the SPECIAL-USE or the XLIST (GMail) extension, you can get ahold of the pre-defined All, Drafts, Flagged (aka Important), Junk, Sent, Trash, etc folders like this:
if ((client.Capabilities & (ImapCapabilities.SpecialUse | ImapCapabilities.XList)) != 0) {
var drafts = client.GetFolder (SpecialFolder.Drafts);
} else {
// maybe check the user's preferences for the Drafts folder?
}
In cases where the IMAP server does not support the SPECIAL-USE or XLIST extensions, you'll have to come up with your own heuristics for getting the Sent, Drafts, Trash, etc folders. For example, you might use logic similar to this:
static string[] CommonSentFolderNames = { "Sent Items", "Sent Mail", "Sent Messages", /* maybe add some translated names */ };
static IFolder GetSentFolder (ImapClient client, CancellationToken cancellationToken)
{
var personal = client.GetFolder (client.PersonalNamespaces[0]);
foreach (var folder in personal.GetSubfolders (false, cancellationToken)) {
foreach (var name in CommonSentFolderNames) {
if (folder.Name == name)
return folder;
}
}
return null;
}
Using LINQ, you could simplify this down to something more like this:
static string[] CommonSentFolderNames = { "Sent Items", "Sent Mail", "Sent Messages", /* maybe add some translated names */ };
static IFolder GetSentFolder (ImapClient client, CancellationToken cancellationToken)
{
var personal = client.GetFolder (client.PersonalNamespaces[0]);
return personal.GetSubfolders (false, cancellationToken).FirstOrDefault (x => CommonSentFolderNames.Contains (x.Name));
}
Another option might be to allow the user of your application to configure which folder he or she wants to use as their Sent folder, Drafts folder, Trash folder, etc.
How you handle this is up to you.
It is necessary to open the folder otherwise it will appear empty.
IMailFolder personal = client.GetFolder(client.PersonalNamespaces[0]);
foreach (IMailFolder folder in personal.GetSubfolders(false, cancellationToken))
{
folder.Open(FolderAccess.ReadOnly);
Console.WriteLine($"folder.Name = {folder.Name}");
Console.WriteLine($"{folder.Name} messages : {folder.Count}");
}
Related
I am using .Net Core along with Amazon's .net sdk to push and pull things from S3. I am using a folder structure in S3 that involves inserting an empty directory with several sub directories.
At a later time I insert files into those directories and move them around. Now I need to be able to remove the directory entirely.
I am able to delete all of the contents of the directory by using
await client.DeleteObjectAsync(bucketName, keyName, null).ConfigureAwait(false);
where I loop through all the files I want to delete in the given bucket. However, it always leaves me with the empty folder structure, in S3 I see that it has a content of 0 Bytes but I don't want to have to sort through thousands of empty folders to find the ones that actually have data.
Is there any way to remove an empty folder from S3 using AWS .NET SDK?
Update:
I am able to delete everything in the folder I want except for the folder itself.
using (IAmazonS3 client = new AmazonS3Client(awsCreds, Amazon.RegionEndpoint.USEast1))
{
try
{
DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest();
ListObjectsRequest listRequest = new ListObjectsRequest
{
BucketName = bucketName,
Prefix = prefix,
//Marker = prefix,
};
ListObjectsResponse response = await client.ListObjectsAsync(listRequest).ConfigureAwait(false);
// Process response
foreach (S3Object entry in response.S3Objects)
{
deleteRequest.AddKey(entry.Key);
}
deleteRequest.BucketName = bucketName;
var response2 = await client.DeleteObjectsAsync(deleteRequest).ConfigureAwait(false);
return true;
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null
&& (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId", StringComparison.Ordinal)
|| amazonS3Exception.ErrorCode.Equals("InvalidSecurity", StringComparison.Ordinal)))
{
logger.LogError("AwsS3Service.DeleteFileFromBucket Error - Check the provided AWS Credentials.");
}
else
{
logger.LogError($"AwsS3Service.DeleteFileFromBucket Error - Message: {amazonS3Exception.Message}");
}
}
}
This deletes the entire contents of the directory I choose along with all sub directories. But the main directory remains, is there any way to remove that main directory.
Your code is 99% of the way there. The only thing you need to do is add the prefix variable to your keys to be deleted as well. Technically, it is a 0-byte object that needs to be 'deleted' as well.
For example, after your loop through all the objects in the response, go ahead and add the prefix variable that was added to find all those things.
foreach (S3Object entry in response.S3Objects)
{
deleteRequest.AddKey(entry.Key);
}
// Add the folder itself to be deleted as well
deleteRequest.AddKey(prefix);
I have an input folder full of msg email files, within these there are inbox, outbox, sent items etc.
Is there any way I could use aspsoe to open the PST copy the files and structure to an output folder.
I have tried
PersonalStorage personalStorage = PersonalStorage.FromFile(InputFolder);
// Get the folders information
FolderInfoCollection folderInfoCollection = personalStorage.RootFolder.GetSubFolders();
Any ideas?
Inbox, Sent Items, Outbox, etc. are predefined folders and are referred to by StandardIpmFolder. You can use the following code sample to extract messages from these and save to disc.
PersonalStorage pst = PersonalStorage.FromFile("pstfile.pst");
//Get Default Inbox Folder
FolderInfo fiInbox = pst.GetPredefinedFolder(StandardIpmFolder.Inbox);
//the same way you can get StandardIpmFolder.Sent and others
//traverse and save to disc
MessageInfoCollection msgs = fiInbox.GetContents();
foreach (MessageInfo msg in msgs)
{
MapiMessage mapi = pst.ExtractMessage(msg);
mapi.Save(mapi.Subject + ".msg");
}
pst.Dispose();
Please try it and if you still face some issue, you can write to us on Aspose.Email forum along with your sample PST file for further assistance.
I work with Aspose as Developer Evangelist.
To export the entire Structure of the PST i used this recursive traversal:
internal static Dictionary<string, MapiMessage> ReadPstAspose(string pstPath)
{
PersonalStorage pst = PersonalStorage.FromFile(pstPath);
Dictionary<string, MapiMessage> allMessages = new Dictionary<string, MapiMessage>();
GetAllMessagesRecursive(pst, pst.RootFolder, allMessages);
pst.Dispose();
return allMessages;
}
private static void GetAllMessagesRecursive(PersonalStorage pst, FolderInfo folder, Dictionary<string, MapiMessage> allMessages)
{
foreach (var messageEntryId in folder.EnumerateMessagesEntryId())
{
try
{
MapiMessage message = pst.ExtractMessage(messageEntryId);
var key = $"{message.DeliveryTime:ddMMyyyy:hhmmss}\\{folder.RetrieveFullPath()}\\{message.Subject}";
allMessages.Add(key, message);
}
catch (Exception e)
{
throw e;
}
}
foreach (var subFolder in folder.GetSubFolders())
{
GetAllMessagesRecursive(pst, subFolder, allMessages);
}
}
The code ist mostly copied from the excellent documentation at aspose.com.
There's still the issue of re-creating the structure. The key of the dict has the structure in it and you can go from there to rebuild it anyway you like.
i am using ImapX 2 and i wanted to get messages and attachments from all folders
That is my code, i can get all folders, but messages not. How to get messages?
if (client.Login(login, pass))
{
// login successful
FolderCollection folders = client.Folders;
foreach (Folder myfolder in folders)
{
var messages = myfolder.Messages;
foreach (var message in messages)
{
MessageBox.Show(message.Subject);
var attachments = message.Attachments;
if (attachments.Count() > 0)
foreach (var attachment in attachments)
{
MessageBox.Show(attachment.FileName);
}
}
}
}
Before you can access message you need to download them
myfolder.Messages.Download();
foreach (var message in messages)
{//your code}
You can use parameter’s, here what documentation said
public void Download([string query = "ALL"], [ImapX.Enums.MessageFetchMode mode = ImapX.Enums.MessageFetchMode.ClientDefault], [int count = -1])
Member of ImapX.Collections.MessageCollection
Summary:
Downloads messages from server using default or given mode.
Parameters:
query: The search query to filter messages. ALL by default
mode: The message fetch mode, allows to select which parts of the message will be requested.
count: The maximum number of messages that will be requested. Set count to -1 will request all messages which match the given query.
I currently want to download all email messages (regardless in which folder they're in) to my SQL Server database.
Now while I know how to search for email messages or subscribe to streaming notifications, I've yet to learn on how to synchronize all messages from EWS to my database.
var emailMessages = GetItems<MSEmailMessage>(WellKnownFolderName.MsgFolderRoot);
foreach (var emailMessage in emailMessages)
{
Debug.WriteLine(emailMessage.Subject);
}
private IList<T> GetItems<T>(WellKnownFolderName wellKnownFolderName) where T : Item
{
IList<T> result = new List<T>();
Folder folder = Folder.Bind(_exchangeService, wellKnownFolderName);
if (folder.TotalCount > 0)
{
ItemView view = new ItemView(folder.TotalCount);
FindItemsResults<Item> items = _exchangeService.FindItems(wellKnownFolderName, view);
foreach (var resultItem in items.OfType<T>())
{
result.Add(resultItem);
}
}
return result;
}
This returns 0 email messages (it even threw an exception before checking for the folder.TotalCount before initializing a new ItemView...).
While checking for WellKnownFolderName.Inbox returns the email messages from the inbox, it does not allow me to query for sub folders to synchronize the entirety of the messages.
What am I missing?
You can build up a list of folders to search for mail in. Then iterate through each folder and get all the emails in that folder.
In the code snippet below we can create a folderSearchFilter with FolderTraversal set to Deep which will scan all sub folders of the target folder. We can then apply this filter to the two main well-known folders Inbox and SentItems
Once you have a list of folders to index, then you can use your own code to retrieve all the mails from that folder.
var view = new FolderView(int.MaxValue)
{
PropertySet = new PropertySet(BasePropertySet.FirstClassProperties) { FolderSchema.DisplayName }
};
SearchFilter foldersearchFilter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);
view.Traversal = FolderTraversal.Deep;
List<Folder> searchFolders;
try
{
searchFolders = new List<Folder>
{
Folder.Bind(ExchangeService, WellKnownFolderName.Inbox),
Folder.Bind(ExchangeService, WellKnownFolderName.SentItems)
};
}
catch (ServiceResponseException e) {}
searchFolders.AddRange(ExchangeService.FindFolders(WellKnownFolderName.Inbox, foldersearchFilter, view).Folders);
searchFolders.AddRange(ExchangeService.FindFolders(WellKnownFolderName.SentItems, foldersearchFilter, view).Folders);
var results = new List<Item>();
foreach (var searchFolder in searchFolders)
{
//Get all emails in this folder
}
Right, the root folder likely has 0 messages in it. When you do FindItems in a folder, the results don't bubble up from subfolders. You need to iterate over every folder if you want to get to their messages.
My application is supposed to send some emails to some destination. After that operation I would like to automatically move sent mails to specific folder ( based on the document type that is in the mail attachment ). If the folder doesn't exist then the program has to create it and then move the mail to the newly created folder. The issue is that after I create a new folder and succcesfully move the mail to it for the first time, then when i sent anothe mails that are supposed to be moved to the said folder the program doesn't see the folder. In fact the Folders method doesn't return any folders at all.
frankly, im out of ideas whats wrong.
when checking in the debugger it says that parentFolder.Folders "Enumeration yielded no results"
I am not sure if I should do anything more after creating the folder in the method createFolder ( ie. something like, update folders list... )
here is my code:
public void moveEmails(string itemType, Boolean itemSent, Outlook.MailItem objMail)
{
Outlook.MAPIFolder folderParent = objMail.Parent as Outlook.MAPIFolder;
Outlook.Folders folders;
Boolean notMoved = true;
objMail.UserProperties.Add("TransferredBy", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
objMail.UserProperties["TransferredBy"].Value = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
objMail.Save();
if (folderParent.Name != "Inbox")
folderParent = digForInbox(folderParent);
folders = folderParent.Folders;
if (!itemSent)
itemType = "NOT DELIVERED";
foreach (Outlook.MAPIFolder folder in folders)
{
if (folder.Name == itemType)
{
objMail.Move(folder);
notMoved = false;
}
}
if (notMoved)
createFolder(itemType,objMail, folderParent);
}
public void createFolder(string itemType, Outlook.MailItem objMail, Outlook.MAPIFolder folderParent)
{
Outlook.MAPIFolder folderNew;
folderNew = folderParent.Folders.Add( itemType, Outlook.OlDefaultFolders.olFolderInbox ) as Outlook.MAPIFolder;
objMail.Move(folderNew);
}
private Outlook.MAPIFolder digForInbox(Outlook.MAPIFolder folder)
{
Boolean isNotInbox = true;
while(isNotInbox)
{
if(folder.Name != "Inbox")
{
folder = folder.Parent as Outlook.MAPIFolder;
}
else
{
isNotInbox = false;
}
}
return folder;
}
I have found the answer to my question:
https://social.msdn.microsoft.com/forums/windows/en-us/180c000c-524a-45dd-88fe-88b470be3597/accessing-subfolders-within-shared-mailbox?forum=outlookdev
the issue was similar to the one in the link. I didnt imagine that because my mailboxes are mainly shared ones that would affect it in any other way than performance (due to connecting to the exchange server )
Posting this as an answer
I'd suggest using the SaveSentMessageFolder property of the MailItem class. It allows to set a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent. Also you may find the following articles helpful:
How To: Change an Outlook e-mail message before sending using C# or VB.NET
How To: Create a new folder in Outlook