Repository navigation
Expand file tree
/
Copy pathService1.cs
More file actions
1746 lines (1068 loc) · 87.7 KB
/
Copy pathService1.cs
File metadata and controls
1746 lines (1068 loc) · 87.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Graph;
using Microsoft.Identity.Client;
using Microsoft.IdentityModel.Protocols;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Text.RegularExpressions;
using Azure.Core;
using System.Net;
using Newtonsoft.Json.Linq;
namespace Simplisys_OAuth2_Service
{
public partial class Service1 : ServiceBase
{
Timer timer = new Timer(); // name space(using System.Timers;)
public Service1()
{
InitializeComponent();
}
[Obsolete]
protected override void OnStart(string[] args)
{
//WriteToFile("Service is started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval =30000; //Convert.ToDouble ( ConfigurationManager.AppSettings["EmailReadingIntervals"]) ; // 300000; //number in milisecinds //EmailReadingIntervals
timer.Enabled = true;
}
[Obsolete]
public void onDebug()
{
_ = processEmailAsync();
//OnStart(null);
}
[Obsolete]
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
_ = processEmailAsync();
}
protected override void OnStop()
{
//WriteToFile("Service is stopped at " + DateTime.Now);
}
[Obsolete]
public async Task processEmailAsync()
{
string strSubjectFirstPart, strSubjectSecondPart;
string strFrom, strCC, strSubject, strBody, strHTMLBody, strAttachments, strMessageID, strSentDate, strReceivedDate, strDisplayName, strError;
string strClientID, strClientDatabase, strClientConnectionString;
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
string strAttchmentPath = ConfigurationManager.AppSettings["IncomingAttachmentPath"];
string strQuery = "select acctmID , acctmAccountName , acctmConnectionString from tblmAccount where acctmAccountName ='LiveJepson' ";
string strResult;
string clientID, clientPassword, clientTenant, mailBoxID, strEmail = "", strEmailPassword = "";
SqlConnection sqlConnection = new SqlConnection(connectionString);
SqlCommand sqlCommand = new SqlCommand();
strSubjectFirstPart = ConfigurationManager.AppSettings["SubjectFirstPart"];
strSubjectSecondPart = ConfigurationManager.AppSettings["SubjectSecondPart"];
//Read the databases one by one
//BELOW PART OF THE CODE IS USED TO READ EMAILS
try
{
sqlConnection.Open();
sqlCommand = new SqlCommand(strQuery, sqlConnection);
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
strClientID = reader["acctmID"].ToString();
strClientDatabase = reader["acctmAccountName"].ToString();
strClientConnectionString = reader["acctmConnectionString"].ToString();
SqlCommand sqlClientDBCommand = new SqlCommand();
if (strClientID.Length > 0)
{
if (strClientDatabase.Length > 0)
{
if (strClientConnectionString.Length > 0)
{
try
{
SqlConnection sqlClientDBConnection = new SqlConnection(strClientConnectionString);
sqlClientDBConnection.Open();
strQuery = "SELECT [mbOAuth2MboxID]";
strQuery = strQuery + ",[mbOAuth2Name]";
strQuery = strQuery + ",[mbOAuth2EmailAddress]";
strQuery = strQuery + ",[mbOAuth2ClientID]";
strQuery = strQuery + ",[mbOAuth2TenantID]";
strQuery = strQuery + ",[mbOAuth2Password]";
strQuery = strQuery + ",[mbOAuth2isDelete]";
strQuery = strQuery + ",[mbOAuth2IncomingEnabled] ";
strQuery = strQuery + ",[mbOAuth2OutgoingEnabled] ";
strQuery = strQuery + ",[mbOAuth2AutoReplyEnabldEnabled] ";
strQuery = strQuery + ",[mbOAuth2ErrorID] ";
strQuery = strQuery + " ,(select mbIncomingPassword from tblMailbox where mbid=[mbOAuth2MboxID]) as mbPassword";
strQuery = strQuery + " FROM [tblMailboxMSoftOAuthDetails] where mbOAuth2isDelete=0 and mbOAuth2IncomingEnabled = 1";
sqlClientDBCommand.CommandText = strQuery;
sqlClientDBCommand.Connection = sqlClientDBConnection;
SqlDataReader clientDBreader = sqlClientDBCommand.ExecuteReader();
while (clientDBreader.Read())
{
try
{
clientID = clientDBreader["mbOAuth2ClientID"].ToString();
clientPassword = clientDBreader["mbOAuth2Password"].ToString();
clientTenant = clientDBreader["mbOAuth2TenantID"].ToString();
mailBoxID = clientDBreader["mbOAuth2MboxID"].ToString();
strEmail = clientDBreader["mbOAuth2EmailAddress"].ToString();
strEmailPassword = clientDBreader["mbPassword"].ToString();
//Test - https://graph.microsoft.com/v1.0/users/research@simplisysltd.onmicrosoft.com/mailFolders/Inbox/messages
GraphServiceClient client = GetAuthenticatedClient(clientID, clientPassword, clientTenant);
//var users = client.Users.Request().GetAsync().Result;
//int userCount = users.Count;
//try
//{
SecureString emailPassword = ConvertToSecureString(strEmailPassword);
MGraphServices graphServices = new MGraphServices();
GraphServiceClient graphServiceClient = await graphServices.connectToMailBox(clientID,
clientTenant,
strEmail,
strEmailPassword);
string testString="";
//var _MailList = await graphServiceClient.Me.MailFolders.Inbox.Messages
// .Request().Expand("attachments").GetAsync();
//var _MailList2 = await graphServiceClient.Me.MailFolders[strEmail].Messages.Request().GetAsync();
//var _MailList = await graphServiceClient.Users[strEmail].Messages.Request().Expand("attachments").GetAsync();
//var messages = await graphServiceClient.Me.MailFolders.Inbox.Messages
// .Request().Expand("attachments").GetAsync();
var messages = await graphServiceClient.Me.MailFolders.Inbox.Messages
.Request().Filter("isRead eq false").Expand("attachments").GetAsync();
//var authResult = await pca.AcquireTokenByUsernamePassword(new string[] { "https://graph.microsoft.com/.default" }, strEmail, emailPassword).ExecuteAsync();
//}
//catch (Exception ex)
//{
// strError = ex.Message.ToString();
//}
//Below line commented on 13 th Sep 2022
//var messages1 = await client.Users[strEmail].Messages
// .Request().Expand("attachments")
// .GetAsync();
//Above line commented on 13 th Sep 2022
string strattachmentName, strsubject = "";
Guid strIncID;
Guid strIncidentEmailID;
Guid inEmailID;
string strServerAddress, strIncomingUsername, strIncomingEmailPassword, strMailBoxID;
string strDateString, strDate, strYear, strMonth, strDay, strAttachmentName = "";
SqlConnection sqlMessageIDDBConnection = new SqlConnection(strClientConnectionString);
SqlCommand sqlMessageIDDBCommand = new SqlCommand();
for (int i = 0; i < messages.CurrentPage.Count; i++)
{
string strCurrentPage = messages.CurrentPage.ToString();
//READ IF THE MESSAGEID IS IN TBLINCOMINH EMAIL TABLE
int countMessageID = 0;
sqlMessageIDDBConnection.Open();
try
{
strQuery = "select count(inemlSubject ) from tblIncomingEmail where inemlMessageID ='" + messages.CurrentPage[i].InternetMessageId.ToString() + "' and inemlDateAdded >getdate()-1";
sqlMessageIDDBCommand.CommandText = strQuery;
sqlClientDBCommand.Connection = sqlMessageIDDBConnection;
SqlDataReader messageCountreader = sqlClientDBCommand.ExecuteReader();
while (messageCountreader.Read())
{
countMessageID = Convert.ToInt32( messageCountreader[0].ToString());
}
messageCountreader.Close();
}
catch (Exception exc)
{
strError = exc.Message.ToString();
}
finally
{
sqlMessageIDDBConnection.Close();
}
if (strEmail == messages.CurrentPage[i].From.EmailAddress.Address.ToString())
{
await client.Users[strEmail].Messages[messages.CurrentPage[i].Id.ToString()].Request().DeleteAsync();
}
//IF THE EMAIL IS ALREADY READ AND INSERTED IN THE TBLINCOMING EMAIL TABLE
else if(countMessageID>0)
{
//already read
}
else //(strEmail != messages.CurrentPage[i].From.EmailAddress.Address.ToString())
{
if (messages[i].IsRead == false)
{
if (messages[i].IsRead is false)
{
//BELOW CODE ADDED ON 31 MARCH 2023
try
{
await client.Users[strEmail].Messages[messages.CurrentPage[i].Id.ToString()].Request().Select("IsRead").UpdateAsync(messages[i]);
}
catch (Exception exc)
{
}
try
{
messages.CurrentPage[i].IsRead = true;
}
catch (Exception exc)
{
}
//ABOVE CODE ADDED ON 31 MARCH 2023
try
{
// await client.Users[strEmail].Messages[messages.CurrentPage[i].Id.ToString()].Request().DeleteAsync();
//var msg = await client.Users[strEmail].Messages[messages.CurrentPage[i].Id.ToString()].Request().GetAsync();
//BELOW LINE ADDED ON 31 MARCH 2023
//ABOVE LINE ADDED ON 31 MARCH 2023
strFrom = messages.CurrentPage[i].From.EmailAddress.Address.ToString();
strCC = messages.CurrentPage[i].CcRecipients.ToString();
strSubject = messages.CurrentPage[i].Subject.ToString();
strHTMLBody = messages.CurrentPage[i].Body.Content.ToString();
strBody = messages.CurrentPage[i].BodyPreview.ToString();
strMessageID = messages.CurrentPage[i].InternetMessageId.ToString();
strSentDate = messages.CurrentPage[i].SentDateTime.ToString();
strReceivedDate = messages.CurrentPage[i].ReceivedDateTime.ToString();
strDisplayName = messages.CurrentPage[i].From.EmailAddress.Name.ToString();
//if (messages.CurrentPage[i].HasAttachments == true)
//{
strDate = messages.CurrentPage[i].SentDateTime.ToString();
// strDate = messages.CurrentPage[i].CreatedDateTime.ToString();
//try
//{
// DateTime DT = new DateTime();
// DT = Convert.ToDateTime(strDate);
// DT = Convert.ToDateTime(DT.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"));
// strDate = DT.ToString();
//}
//catch (Exception ex)
//{
// strError = ex.Message.ToString();
//}
//strDate = email.Date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
strIncID = Guid.NewGuid();
strIncidentEmailID = Guid.NewGuid();
strMessageID = strMessageID.Replace('<', ' ');
strMessageID = strMessageID.Replace('>', ' ');
inEmailID = new Guid(mailBoxID);
string strFilePath = strAttchmentPath + "\\" + strClientDatabase + "\\Simplisys Incident Support\\" + strMessageID.Trim(); // Your code goes here
strFilePath = strFilePath.Trim();
try
{
System.IO.Directory.CreateDirectory(strFilePath);
}
catch (Exception ex)
{
strError = ex.Message.ToString();
}
//strResult = mynumber;
////Prefix set in tblsystemsettings
///
try
{
SqlConnection sqlConnectionChkInc = new SqlConnection(strClientConnectionString);
SqlCommand sqlCommandChkInc = new SqlCommand();
sqlConnectionChkInc.Open();
string strQueryChkIncident = "select top 1 ssValue from tblSystemSettings where ssKey='IncUpdateFrmEmailPrefix' and ssIsDeleted=0";
sqlCommandChkInc = new SqlCommand(strQueryChkIncident, sqlConnectionChkInc);
SqlDataReader readerChkInc = sqlCommandChkInc.ExecuteReader();
while (readerChkInc.Read())
{
strSubjectFirstPart = readerChkInc["ssValue"].ToString();
}
readerChkInc.Close();
sqlConnectionChkInc.Close();
}
catch (Exception exc)
{
string err = exc.Message.ToString();
}
/// Suffux set in tblsystemsettings
///
try
{
SqlConnection sqlConnectionChkInc = new SqlConnection(strClientConnectionString);
SqlCommand sqlCommandChkInc = new SqlCommand();
sqlConnectionChkInc.Open();
string strQueryChkIncident = "select top 1 ssValue from tblSystemSettings where ssKey='IncUpdateFrmEmailSuffix' and ssIsDeleted=0";
sqlCommandChkInc = new SqlCommand(strQueryChkIncident, sqlConnectionChkInc);
SqlDataReader readerChkInc = sqlCommandChkInc.ExecuteReader();
while (readerChkInc.Read())
{
strSubjectSecondPart = readerChkInc["ssValue"].ToString();
}
readerChkInc.Close();
sqlConnectionChkInc.Close();
}
catch (Exception exc)
{
string err = exc.Message.ToString();
}
strResult = Between(strSubject, strSubjectFirstPart, strSubjectSecondPart);
string var = strResult;
string mystr = Regex.Replace(var, @"\d", "");
string mynumber = Regex.Replace(var, @"\D", "");
// This is to remove I from the Incident number
string strIncidentNumber = "INCNUMBER";
if (strResult != "NoMatchFound")
{
try
{
strResult = strResult.Trim();
strResult = strResult.Remove(0, 1);
SqlConnection sqlConnectionChkInc = new SqlConnection(strClientConnectionString);
SqlCommand sqlCommandChkInc = new SqlCommand();
sqlConnectionChkInc.Open();
string strQueryChkIncident = "select incID, incNumber from tblincident where incNumber='" + strResult + "'";
sqlCommandChkInc = new SqlCommand(strQueryChkIncident, sqlConnectionChkInc);
SqlDataReader readerChkInc = sqlCommandChkInc.ExecuteReader();
while (readerChkInc.Read())
{
strIncID = Guid.Parse(readerChkInc["incID"].ToString());
strIncidentNumber = readerChkInc["IncNumber"].ToString();
}
readerChkInc.Close();
sqlConnectionChkInc.Close();
}
catch (Exception exc)
{
string err = exc.Message.ToString();
}
}
string strSQLProcedure = "";
if (strIncidentNumber == strResult)
{
strSQLProcedure = "SPUpdateIncFrmEmail"; // "SPUpdateIncFrmEmail";
}
else
{
strSQLProcedure = "SPInsertToIncomingEmail";
}
if (strBody.Length > 50) // if (strBody.Length > 254)
{
try
{
strBody = HTMLToText(strHTMLBody);
}
catch (Exception ex)
{
strError = ex.Message.ToString();
}
}
connectionString = ConfigurationManager.AppSettings["ConnectionString"];
using (SqlConnection con = new SqlConnection(strClientConnectionString))
{
using (SqlCommand cmd = new SqlCommand(strSQLProcedure, con))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@incID", strIncID);
cmd.Parameters.AddWithValue("@inemlID", strIncidentEmailID);
cmd.Parameters.AddWithValue("@inemlMailbox", inEmailID);
cmd.Parameters.AddWithValue("@inemlSubject", strSubject);
cmd.Parameters.AddWithValue("@inemlIsBodyHTML", 1); //@inemlIsBodyHTML bit,
cmd.Parameters.AddWithValue("@inemlHTMLBody", strHTMLBody); //@inemlHTMLBody nvarchar(max),
cmd.Parameters.AddWithValue("@inemlTextBody", strBody);//@inemlTextBody nvarchar(max),
strDateString = strSentDate;
cmd.Parameters.AddWithValue("@inemlDateSent", Convert.ToDateTime(strDate)); //@inemlDateAdded datetime,
cmd.Parameters.AddWithValue("@inemlImportance", "Normal");//@inemlImportance nvarchar(50),
cmd.Parameters.AddWithValue("@inemlMessageID", strMessageID); //@inemlMessageID nvarchar(250),
cmd.Parameters.AddWithValue("@inemlFromAddress", strFrom);//@inemlFromAddress nvarchar(320),
cmd.Parameters.AddWithValue("@inemlFromDisplayName", strDisplayName); //@inemlFromDisplayName nvarchar(260),
cmd.Parameters.AddWithValue("@inemlBlockedBySizeLimit", 0); //@inemlBlockedBySizeLimit bit,
cmd.Parameters.AddWithValue("@inemlBlockedByBlacklist", 0);//@inemlBlockedByBlacklist bit,
cmd.Parameters.AddWithValue("@inemlBlockedBySpam", 0); //@inemlBlockedBySpam bit,
cmd.Parameters.AddWithValue("@inemlFailedDueToError", 0);//@inemlFailedDueToError bit,
cmd.Parameters.AddWithValue("@inemlFailedDueToErrorBeforeDeletion", 0);//@inemlFailedDueToErrorBeforeDeletion bit,
cmd.Parameters.AddWithValue("@inemlIsDeleted", 0);//@inemlIsDeleted bit
cmd.Parameters.AddWithValue("@inemlFilePath", strFilePath + "\\email.eml");
con.Open();
cmd.ExecuteNonQuery();
// con.Close();
}
catch (Exception ex)
{
strError = ex.Message.ToString();
}
}
}
string strfile = "";
string strEmbeddedFilename, strEmbeddedContentID, strEmbeddedFileFullPath = "";
// ATTACHMENTS
//------------------------------
try
{
if (messages.CurrentPage[i].Attachments != null)
{
string strAtt = messages.CurrentPage[i].Attachments.ToString();
for (int j = 0; j < messages.CurrentPage[i].Attachments.Count; j++)
{
strattachmentName = messages.CurrentPage[i].Attachments[j].Name;
if (messages.CurrentPage[i].Attachments[j].IsInline == true)
{
strEmbeddedFilename = strattachmentName;
strEmbeddedFileFullPath = strFilePath + "\\" + strEmbeddedFilename;
var item = (FileAttachment)messages.CurrentPage[i].Attachments[j]; // Cast from Attachment
//var folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//var filePath = Path.Combine(strFilePath, item.Name);
System.IO.File.WriteAllBytes(strEmbeddedFileFullPath, item.ContentBytes);
strEmbeddedContentID = item.ContentId; // messages.CurrentPage[i].Attachments[j].Id;
strEmbeddedContentID = strEmbeddedContentID.Replace('<', ' ');
strEmbeddedContentID = strEmbeddedContentID.Replace('>', ' ');
strEmbeddedContentID = strEmbeddedContentID.Trim();
try
{
using (SqlConnection con = new SqlConnection(strClientConnectionString))
{
//SPInsertEmbeddedInToAttachments
using (SqlCommand cmd = new SqlCommand("SPInsertEmbeddedInToAttachments", con))
// using (SqlCommand cmd = new SqlCommand("SPInsertInToAttachments", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@incID", strIncID); ////// @incID uniqueidentifier,
cmd.Parameters.AddWithValue("@inemlID", strIncidentEmailID);
cmd.Parameters.AddWithValue("@attDiskPath", strEmbeddedFileFullPath); //////@attDiskPath varchar(500),
cmd.Parameters.AddWithValue("@attFileName", strEmbeddedFilename); //////@attFileName varchar(150),
cmd.Parameters.AddWithValue("@inemlattContentID", strEmbeddedContentID);
cmd.Parameters.AddWithValue("@attNotes", "File added by incoming email from " + strFrom); //////@attNotes varchar(250),
cmd.Parameters.AddWithValue("@attSize", messages.CurrentPage[i].Attachments[j].Size); //////@attSize bigint
cmd.Parameters.AddWithValue("@inemlattEmbeddedImage", 1);
con.Open();
cmd.ExecuteNonQuery();
}
}
}
catch (Exception exc)
{
string err = exc.Message.ToString();
}
}
else
{
strEmbeddedFileFullPath = strFilePath + "\\" + strattachmentName;
var item = (FileAttachment)messages.CurrentPage[i].Attachments[j];
System.IO.File.WriteAllBytes(strEmbeddedFileFullPath, item.ContentBytes);
using (SqlConnection con = new SqlConnection(strClientConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SPInsertInToAttachments", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@incID", strIncID); ////// @incID uniqueidentifier,
cmd.Parameters.AddWithValue("@inemlID", strIncidentEmailID);
cmd.Parameters.AddWithValue("@attDiskPath", strFilePath + "\\" + strattachmentName); //////@attDiskPath varchar(500),
cmd.Parameters.AddWithValue("@attFileName", strattachmentName); //////@attFileName varchar(150),
cmd.Parameters.AddWithValue("@inemlattContentID", "");
cmd.Parameters.AddWithValue("@attNotes", "File added by incoming email from " + strFrom); //////@attNotes varchar(250),
cmd.Parameters.AddWithValue("@attSize", messages.CurrentPage[i].Attachments[j].Size); //////@attSize bigint
cmd.Parameters.AddWithValue("@inemlattEmbeddedImage", 0);
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}
}
}
catch (Exception eee)
{
string strerr = eee.Message.ToString();
}
// }
// below code to insert into ToAddress
int toRecepientsCount = messages.CurrentPage[i].ToRecipients.Count();
for (int torcpt = 0; torcpt < toRecepientsCount; torcpt++)
{
try
{
using (SqlConnection con = new SqlConnection(strClientConnectionString))
{
using (SqlCommand cmd = new SqlCommand("spInsertIncomingEmailToAddress", con))
{
cmd.CommandType = CommandType.StoredProcedure;
//@inemlID uniqueidentifier,
//@inemlEmailAddress varchar(500),
//@inemlDisplayName varchar(500)
cmd.Parameters.AddWithValue("@inemlID", strIncidentEmailID);
cmd.Parameters.AddWithValue("@inemlEmailAddress", messages.CurrentPage[i].ToRecipients.ElementAt(torcpt).EmailAddress.Address.ToString());
cmd.Parameters.AddWithValue("@inemlDisplayName", messages.CurrentPage[i].ToRecipients.ElementAt(torcpt).EmailAddress.Name.ToString());
con.Open();
cmd.ExecuteNonQuery();
}
}
}
catch (Exception eee)
{
string strerr = eee.Message.ToString();
//Insert record in to tblerror
}
}
// above code to insert into ToAddress
//-----------
// below code to insert into cc addres
int ccRecepientsCount = messages.CurrentPage[i].CcRecipients.Count();
for (int ccrcpt = 0; ccrcpt < ccRecepientsCount; ccrcpt++)
{
try
{
using (SqlConnection con = new SqlConnection(strClientConnectionString))
{
using (SqlCommand cmd = new SqlCommand("spInsertIncomingEmailCCAddress", con))
{
cmd.CommandType = CommandType.StoredProcedure;
//@inemlID uniqueidentifier,
//@inemlEmailAddress varchar(500),
//@inemlDisplayName varchar(500)
cmd.Parameters.AddWithValue("@inemlID", strIncidentEmailID);
cmd.Parameters.AddWithValue("@inemlEmailAddress", messages.CurrentPage[i].CcRecipients.ElementAt(ccrcpt).EmailAddress.Address.ToString());
cmd.Parameters.AddWithValue("@inemlDisplayName", messages.CurrentPage[i].CcRecipients.ElementAt(ccrcpt).EmailAddress.Name.ToString());
con.Open();
cmd.ExecuteNonQuery();
}
}
}
catch (Exception eee)
{
string strerr = eee.Message.ToString();
//Insert record in to tblerror
}
}
// above code to insert into cc Address
if (strIncidentNumber != strResult)
{
using (SqlConnection con = new SqlConnection(strClientConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SPUpdateObjectHstory", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@incID", strIncID);
cmd.Parameters.AddWithValue("@inemlID", strIncidentEmailID);
con.Open();
cmd.ExecuteNonQuery();
}
}
}
messages.CurrentPage[i].IsRead = true;
//messages.RemoveAt(Convert.ToInt32( messages.CurrentPage[i].Id));
string strTestSybject = messages.CurrentPage[i].Subject.ToString();
await graphServiceClient.Users[strEmail].Messages[messages.CurrentPage[i].Id.ToString()].Request().DeleteAsync();
System.Threading.Thread.Sleep(1000);
//Below lines commented on 13th SEP 2022
//GraphServiceClient myClient = GetAuthenticatedClient(clientID, clientPassword, clientTenant);
//await client.Users[strEmail].Messages[messages.CurrentPage[i].Id.ToString()].Request().DeleteAsync();
//Above lines commented on 13th Sep 2022
// await graphServiceClient.Me.MailFolders.Inbox.Messages.Request().Expand("attachments").GetAsync();
// await client.Users[strEmail].Messages[messages.CurrentPage[i].InternetMessageId.ToString()].Request().DeleteAsync();
// graphClient.Users[clientID]
}
catch (Exception eee)
{
string strerr = eee.Message.ToString();
}
}
}
}
_ = messages[i].IsRead == true;
strFrom = "";
strCC = "";
strSubject = "";
strHTMLBody = "";
strBody = "";
strMessageID = "";
strSentDate = "";
strReceivedDate = "";
strDisplayName = "";
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " in the email :" + strEmail);
}
}
clientDBreader.Close();