Hi Guys,
I am reading inbox and downloading excel files from a specific e-mail address in a console application. I am saving those excel files by adding unique ids into the file names.
Here is the sample code:
Now I need to read those excel files one by one and insert the unique id and data inside into a database via a .net core console application. I should read the unique id of the excel file and if it is NOT already saved into the database, I will insert it. Any suggestions on how to do it in the .net core console application?
Best Regards.
I am reading inbox and downloading excel files from a specific e-mail address in a console application. I am saving those excel files by adding unique ids into the file names.
Here is the sample code:
Read Inbox:
static void Main(string[] args)
{
using var client = new ImapClient();
client.Connect("imap.gmail.com", 993, true);
client.Authenticate("test@gmail.com", "abc12345");
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadWrite);
var query = SearchQuery.FromContains("test@gmail.com")
.And(SearchQuery.SubjectContains("Test101")).And(SearchQuery.NotSeen);
foreach (var uid in inbox.Search(query))
{
var message = inbox.GetMessage(uid);
foreach (var attachment in message.Attachments)
{
if (attachment is MessagePart)
{
var fileName = attachment.ContentDisposition?.FileName;
var rfc822 = (MessagePart)attachment;
if (string.IsNullOrEmpty(fileName))
fileName = "attached-message.eml";
using var stream = File.Create(fileName);
rfc822.Message.WriteTo(stream);
}
else
{
var part = (MimePart)attachment;
var fileName = part.FileName;
var file = Path.GetFileNameWithoutExtension(fileName);
var newPath = fileName.Replace(file, file + "-"+ uid);
Console.WriteLine(newPath);
Console.WriteLine(uid);
using var stream = File.Create(newPath);
part.Content.DecodeTo(stream);
}
inbox.AddFlags(uid, MessageFlags.Seen, true);
}
}
client.Disconnect(true);
}
Now I need to read those excel files one by one and insert the unique id and data inside into a database via a .net core console application. I should read the unique id of the excel file and if it is NOT already saved into the database, I will insert it. Any suggestions on how to do it in the .net core console application?
Best Regards.