-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mail.vb
839 lines (838 loc) · 36.3 KB
/
Mail.vb
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
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Web
Imports System.Net.Mail
Imports System.Net.Mail.MailMessage
Imports System.Diagnostics.Process
Imports System.Data.OleDb
Imports System.Data
Imports System.Data.SqlClient
Imports System.Security.Cryptography
Namespace FDFApp
Public Class Mail
Public Class SMTPServer
Implements IDisposable
Dim cdoSendUsingPort As Integer = 2
Private _SMTPServer As New Net.Mail.SmtpClient
Private _SMTPErrors As New FDFErrors
Private _SMTPCredentials As New System.Net.NetworkCredential
'Enum FDFType
' FDF = 1
' xFDF = 2
' XML = 3
' PDF = 4
' XDP = 5
'End Enum
Enum SMTPAuthenticate
cdoNone = 0
cdoBasic = 1
cdoNTLM = 2
End Enum
Public Function SMTPHasErrors() As Boolean
Return _SMTPErrors.FDFHasErrors
End Function
Public Sub ResetErrors()
_SMTPErrors.ResetErrors()
End Sub
Private Property SMTPErrors() As FDFErrors
Get
Return _SMTPErrors
End Get
Set(ByVal Value As FDFErrors)
_SMTPErrors = Value
End Set
End Property
Public Function SMTPErrorsStr(Optional ByVal HTMLFormat As Boolean = False) As String
Dim FDFErrors As FDFErrors
Dim FDFError As FDFErrors.FDFError
FDFErrors = _SMTPErrors
Dim retString As String
retString = CStr(IIf(HTMLFormat, "<br>", vbNewLine)) & "FDFDoc Errors:"
For Each FDFError In FDFErrors.FDFErrors
retString = retString & CStr(IIf(HTMLFormat, "<br>", vbNewLine)) & vbTab & "Error: " & FDFError.FDFError & CStr(IIf(HTMLFormat, "<br>", vbNewLine)) & vbTab & "#: " & FDFError.FDFError_Number & CStr(IIf(HTMLFormat, "<br>", vbNewLine)) & vbTab & "Module: " & FDFError.FDFError_Module & CStr(IIf(HTMLFormat, "<br>", vbNewLine)) & vbTab & "Message: " & FDFError.FDFError_Msg & CStr(IIf(HTMLFormat, "<br>", vbNewLine))
Next
Return retString
End Function
Public Sub New()
_SMTPServer = New Net.Mail.SmtpClient
_SMTPServer.Port = 25
_SMTPServer.Host = "localhost"
_SMTPServer.Timeout = 30
_SMTPCredentials = New System.Net.NetworkCredential
End Sub
Public Property SMTPConnectionTimeOut() As Integer
Get
Return _SMTPServer.Timeout + 0
End Get
Set(ByVal Value As Integer)
_SMTPServer.Timeout = Value + 0
End Set
End Property
Public Property SMTPServerPort() As Integer
Get
Return _SMTPServer.Port + 0
End Get
Set(ByVal Value As Integer)
_SMTPServer.Port = Value + 0
End Set
End Property
Public Property SMTPServerName() As String
Get
Return _SMTPServer.Host & ""
End Get
Set(ByVal Value As String)
_SMTPServer.Host = Value & ""
End Set
End Property
Public Property SMTPPassword() As String
Get
Return _SMTPCredentials.Password & ""
End Get
Set(ByVal Value As String)
_SMTPCredentials.Password = Value & ""
End Set
End Property
Public Property SMTPUserName() As String
Get
Return _SMTPCredentials.UserName & ""
End Get
Set(ByVal Value As String)
_SMTPCredentials.UserName = Value & ""
End Set
End Property
Public Property SMTPCredentials() As Net.NetworkCredential
Get
Return _SMTPCredentials
End Get
Set(ByVal value As Net.NetworkCredential)
_SMTPCredentials = value
End Set
End Property
Public Sub NewNetworkCredentials(ByVal Username As String, ByVal Password As String, Optional ByVal Domain As String = "")
If Domain = "" Then
_SMTPCredentials = New System.Net.NetworkCredential(Username, Password)
Else
_SMTPCredentials = New System.Net.NetworkCredential(Username, Password, Domain)
End If
End Sub
Public Function SendMail(ByVal MailMsg As Net.Mail.MailMessage, ByVal AttachFName As String, ByVal streamAttachment As Stream) As Boolean
Dim _results As Boolean = True
Dim cSmtp As New Net.Mail.SmtpClient
cSmtp = _SMTPServer
If Not streamAttachment Is Nothing Then
Dim MediaType As String = ""
streamAttachment.Position = 0
Dim att As New Net.Mail.Attachment(streamAttachment, AttachFName)
MailMsg.Attachments.Add(att)
End If
Try
If Not _SMTPCredentials Is Nothing Then
If _SMTPCredentials.UserName Is Nothing Or String_IsNullOrEmpty(_SMTPCredentials.UserName) Then
cSmtp.UseDefaultCredentials = True
Else
cSmtp.UseDefaultCredentials = False
cSmtp.Credentials = _SMTPCredentials
End If
Else
cSmtp.UseDefaultCredentials = True
End If
MailMsg.DeliveryNotificationOptions = Net.Mail.DeliveryNotificationOptions.OnFailure
cSmtp.Send(MailMsg)
_results = True
streamAttachment.Dispose()
MailMsg.Dispose()
Catch ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcInternalError, "Error: " & ex.Message, "FDFApp.SMTPServer.SendMail", 1)
_results = False
End Try
Return _results
End Function
Protected Function String_IsNullOrEmpty(ByVal s As Object) As Boolean
Try
Return String.IsNullOrEmpty(CStr(s))
Catch ex As Exception
Return String.IsNullOrEmpty(CStr(s))
End Try
End Function
Public Property FDFSMTPClient() As Net.Mail.SmtpClient
Get
Return _SMTPServer
End Get
Set(ByVal value As Net.Mail.SmtpClient)
_SMTPServer = value
End Set
End Property
Public Function SendMail(ByVal MailMsg As Net.Mail.MailMessage, ByVal FileAttachment As String) As Boolean
Dim _results As Boolean = True
Dim cSmtp As New Net.Mail.SmtpClient
cSmtp = _SMTPServer
If Not FileAttachment Is Nothing Then
Dim att As New Net.Mail.Attachment(FileAttachment)
MailMsg.Attachments.Add(att)
End If
Try
If Not _SMTPCredentials Is Nothing Then
If _SMTPCredentials.UserName Is Nothing Or String_IsNullOrEmpty(_SMTPCredentials.UserName) Then
cSmtp.UseDefaultCredentials = True
Else
cSmtp.UseDefaultCredentials = False
cSmtp.Credentials = _SMTPCredentials
End If
Else
cSmtp.UseDefaultCredentials = True
End If
MailMsg.DeliveryNotificationOptions = Net.Mail.DeliveryNotificationOptions.OnFailure
cSmtp.Send(MailMsg)
MailMsg.Dispose()
_results = True
Catch ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcInternalError, "Error: " & ex.Message, "FDFApp.SMTPServer.SendMail", 1)
_results = False
End Try
Return _results
End Function
Public Function SendMail(ByVal MailMsg As Net.Mail.MailMessage, ByVal AttachFName As String, ByVal bytAttachment() As Byte) As Boolean
Dim _results As Boolean = True
Dim cSmtp As New Net.Mail.SmtpClient
cSmtp = _SMTPServer
Dim xmem As New MemoryStream
If Not bytAttachment Is Nothing Then
xmem = New MemoryStream(bytAttachment)
Dim att As New Net.Mail.Attachment(xmem, AttachFName)
MailMsg.Attachments.Add(att)
End If
Try
If Not _SMTPCredentials Is Nothing Then
If _SMTPCredentials.UserName Is Nothing Or String_IsNullOrEmpty(_SMTPCredentials.UserName) Then
cSmtp.UseDefaultCredentials = True
Else
cSmtp.UseDefaultCredentials = False
cSmtp.Credentials = _SMTPCredentials
End If
Else
cSmtp.UseDefaultCredentials = True
End If
MailMsg.DeliveryNotificationOptions = Net.Mail.DeliveryNotificationOptions.OnFailure
cSmtp.Send(MailMsg)
_results = True
xmem.Dispose()
MailMsg.Dispose()
Catch ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcInternalError, "Error: " & ex.Message, "FDFApp.SMTPServer.SendMail", 1)
_results = False
End Try
Return _results
End Function
Public Function PDFSendEmail(ByVal MailMsg As Net.Mail.MailMessage, ByVal PDFData As Byte(), Optional ByVal FileType As FDFDoc_Class.FDFType = FDFDoc_Class.FDFType.PDF, Optional ByVal Flatten As Boolean = False) As Boolean
Dim _result As Boolean
Try
_result = SendMail(MailMsg, "PDFFORMDATA.pdf", PDFData)
Return _result
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.SMTPServer.PDFSendEmail", 3)
Return False
End Try
End Function
Public Function PDFSendEmail(ByVal MailMsg As Net.Mail.MailMessage, ByVal PDFData As Byte(), ByVal PDF_ATTACHMENT_NAME As String, Optional ByVal FileType As FDFDoc_Class.FDFType = FDFDoc_Class.FDFType.PDF, Optional ByVal Flatten As Boolean = False) As Boolean
Dim _result As Boolean
Try
_result = SendMail(MailMsg, PDF_ATTACHMENT_NAME, PDFData)
Return _result
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.SMTPServer.PDFSendEmail", 3)
Return False
End Try
End Function
Public Function FDFSendEmail(ByVal MailMsg As Net.Mail.MailMessage, Optional ByVal FDFDocument As FDFDoc_Class = Nothing, Optional ByVal FileType As FDFApp.FDFDoc_Class.FDFType = FDFDoc_Class.FDFType.FDF) As Boolean
Dim _result As Boolean
Dim xToday As String
xToday = DateTime.Now.ToString
Try
FDFDocument.FDFData = FDFDocument.FDFSavetoStr(FileType, True)
Dim AttachX As Byte()
AttachX = FDFDocument.FDFSavetoBuf(FileType)
Dim FType As String = "fdf"
Select Case FileType
Case FDFDoc_Class.FDFType.FDF
FType = "fdf"
Case FDFDoc_Class.FDFType.xFDF
FType = "xfdf"
Case FDFDoc_Class.FDFType.XML
FType = "xml"
Case FDFDoc_Class.FDFType.XDP
FType = "xdp"
Case Else
FType = "fdf"
End Select
_result = SendMail(MailMsg, "PDFFORMDATA." & FType, AttachX)
Return _result
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.SMTPServer.FDFSendEmail", 3)
Return False
End Try
End Function
Public Function FDFSendEmail(ByVal MailMsg As Net.Mail.MailMessage, ByVal FDFDocument As FDFDoc_Class, ByVal FileName As String, Optional ByVal FileType As FDFApp.FDFDoc_Class.FDFType = FDFDoc_Class.FDFType.FDF) As Boolean
Dim _result As Boolean
Dim xToday As String
xToday = DateTime.Now.ToString
Try
FDFDocument.FDFData = FDFDocument.FDFSavetoStr(FileType, True)
Dim AttachX As Byte()
AttachX = FDFDocument.FDFSavetoBuf(FileType)
Dim FType As String
Dim tmpFilename As String = ""
If FileName.LastIndexOf("."c) > 0 Then
tmpFilename = FileName.Substring(0, FileName.LastIndexOf("."c))
tmpFilename = tmpFilename.TrimEnd("."c)
Else
tmpFilename = FileName
End If
If Not tmpFilename.Length > 0 Then
tmpFilename = FileName
Else
tmpFilename = tmpFilename
End If
If tmpFilename.Length <= 0 Then
tmpFilename = "PDFAttachment"
End If
Select Case FileType
Case FDFDoc_Class.FDFType.FDF
FType = "fdf"
Case FDFDoc_Class.FDFType.xFDF
FType = "xfdf"
Case FDFDoc_Class.FDFType.XML
FType = "xml"
Case FDFDoc_Class.FDFType.XDP
FType = "xdp"
Case Else
FType = "fdf"
End Select
_result = SendMail(MailMsg, tmpFilename & "." & FType, AttachX)
Return _result
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.SMTPServer.FDFSendEmail", 3)
Return False
End Try
End Function
Private Function Determine_FDFType(ByVal FDFData As String) As FDFApp.FDFDoc_Class.FDFType
If FDFData.StartsWith("%FDF") Then
Return FDFDoc_Class.FDFType.FDF
ElseIf FDFData.StartsWith("<?xml version=""1.0""") Then
If InStrRev(FDFData, "<xfdf", , CompareMethod.Text) > 0 Then
Return FDFDoc_Class.FDFType.xFDF
Else
Return FDFDoc_Class.FDFType.XML
End If
ElseIf FDFData.StartsWith("%PDF") Then
Return FDFDoc_Class.FDFType.PDF
Else
Me._SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcBadFDF, "Error: Bad FDF or Unknown FDF Type", "Mail.FDFType", 1)
Return FDFDoc_Class.FDFType.FDF
End If
End Function
Private Function CreateTempFileName(ByVal cFDFDoc As FDFDoc_Class, ByVal PathTempDirectory As String, Optional ByVal FileType As FDFApp.FDFDoc_Class.FDFType = FDFDoc_Class.FDFType.FDF) As String
Dim strFN As String = ""
Dim xRan As Integer = 0
Randomize()
xRan = CInt(Rnd() * 9999) + 1
Try
Select Case FileType
Case FDFDoc_Class.FDFType.FDF
strFN = "FDFAttachment_" & xRan
strFN = strFN & "." & "fdf"
Case FDFDoc_Class.FDFType.PDF
FileType = FDFDoc_Class.FDFType.PDF
strFN = "PDFAttachment_" & xRan
strFN = strFN & "." & "pdf"
Case FDFDoc_Class.FDFType.xFDF
If FileType = FDFDoc_Class.FDFType.PDF Then
FileType = FDFDoc_Class.FDFType.xFDF
End If
strFN = "xFDFAttachment_" & xRan
strFN = strFN & "." & "xfdf"
Case FDFDoc_Class.FDFType.XML
If FileType = FDFDoc_Class.FDFType.PDF Then
FileType = FDFDoc_Class.FDFType.XML
End If
strFN = "XMLAttachment_" & xRan
strFN = strFN & "." & "xml"
End Select
Dim strFDFPath As String = "", cFDFApp As New FDFApp_Class
strFDFPath = PathTempDirectory & strFN
If File.Exists(strFDFPath) Then
File.Delete(strFDFPath)
End If
Dim binarywrite As New BinaryWriter(File.Create(strFDFPath))
If FileType = FDFDoc_Class.FDFType.PDF Then
binarywrite.Write(cFDFDoc.PDFMergeFDF2Buf(cFDFDoc.FDFGetFile, True))
Else
binarywrite.Write(cFDFDoc.FDFSavetoBuf(FileType, True))
End If
binarywrite.Flush()
binarywrite.Close()
Return strFDFPath
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.FDFCreateTempFileName", 1)
Return ""
End Try
End Function
Private Function CreateTempPDFFileName(ByVal PDFPath As String, ByVal PathTempDirectory As String, Optional ByVal FileType As FDFDoc_Class.FDFType = FDFDoc_Class.FDFType.PDF) As String
Dim strFN As String
Dim xRan As Integer
Randomize()
xRan = CInt(Rnd() * 9999) + 1
strFN = "PDFAttachment_" & xRan
Try
FileType = FDFDoc_Class.FDFType.PDF
strFN = strFN & "." & "pdf"
Dim strFDFPath As String, cFDFApp As New FDFApp_Class
strFDFPath = PathTempDirectory & strFN
If File.Exists(strFDFPath) Then
File.Delete(strFDFPath)
End If
Dim binarywrite As New BinaryWriter(File.OpenWrite(strFDFPath))
If FileType = FDFDoc_Class.FDFType.PDF Then
binarywrite.Write(cFDFApp.PDFSavetoBuf(PDFPath))
End If
binarywrite.Flush()
binarywrite.Close()
Return strFDFPath
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.FDFCreateTempFileName", 1)
Return ""
End Try
End Function
Private Function CreateTempPDFFileName(ByVal PDFData As Byte(), ByVal PathTempDirectory As String, Optional ByVal FileType As FDFDoc_Class.FDFType = FDFDoc_Class.FDFType.PDF) As String
Dim strFN As String
Dim xRan As Integer
Randomize()
xRan = CInt(Rnd() * 9999) + 1
strFN = "PDFAttachment_" & xRan
Try
FileType = FDFDoc_Class.FDFType.PDF
strFN = strFN & "." & "pdf"
Dim strFDFPath As String, cFDFApp As New FDFApp_Class
strFDFPath = PathTempDirectory & strFN
If File.Exists(strFDFPath) Then
File.Delete(strFDFPath)
End If
Dim binarywrite As New BinaryWriter(File.OpenWrite(strFDFPath))
If FileType = FDFDoc_Class.FDFType.PDF Then
binarywrite.Write(PDFData)
End If
binarywrite.Flush()
binarywrite.Close()
Return strFDFPath
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.FDFCreateTempFileName", 1)
Return ""
End Try
End Function
Private Sub Add_Attachments(ByVal email As System.Net.Mail.MailMessage, ByVal strAttachments() As String)
Dim sAttach_Array() As String, sAttach As String
Dim nFileLen As Integer = 0, strFileName As String = "", attach As Net.Mail.Attachment
Try
If Not strAttachments Is Nothing Then
sAttach_Array = strAttachments
Else
sAttach_Array = Nothing
End If
If Not sAttach_Array Is Nothing Then
For Each sAttach In sAttach_Array
If sAttach.Length > 0 Then
strFileName = sAttach
If File.Exists(strFileName) Then
attach = New Net.Mail.Attachment(strFileName)
email.Attachments.Add(attach)
End If
End If
Next
End If
Catch ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & ex.Message, "FDFApp.SMTPServer.Add_Attachment", 1)
End Try
End Sub
Private Sub Delete_Attachments(ByVal strAttachments() As String, Optional ByVal DeleteFile As Boolean = False)
Dim sAttach_Array() As String, sAttach As String
Dim nFileLen As Integer = 0, strFileName As String
Try
If Not strAttachments Is Nothing Then
sAttach_Array = strAttachments
Else
sAttach_Array = Nothing
End If
If Not sAttach_Array Is Nothing Then
For Each sAttach In sAttach_Array
If sAttach.Length > 0 Then
strFileName = sAttach
If File.Exists(strFileName) Then
If DeleteFile = True Then
File.Delete(strFileName)
End If
End If
End If
Next
End If
Catch ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & ex.Message, "FDFApp.SMTPServer.Delete_Attachments", 1)
End Try
End Sub
Public Function FDFSendEmail(ByVal MailMsg As Net.Mail.MailMessage, ByVal FDFDocument As FDFDoc_Class, ByVal FileType As FDFApp.FDFDoc_Class.FDFType, ByVal EncryptionKey As String, Optional ByVal EncryptionProvider As Encryption.Symmetric.Provider = Encryption.Symmetric.Provider.Rijndael) As Boolean
Dim _result As Boolean
Dim xToday As String
xToday = DateTime.Now.ToString
Try
FDFDocument.FDFData = FDFDocument.FDFSavetoStr(FileType, True)
Dim AttachX As Byte()
AttachX = FDFDocument.FDFSavetoBuf(FileType)
Dim clsEncrypt As New Encryption.Asymmetric()
Dim sym As New Encryption.Symmetric(EncryptionProvider)
sym.Key = New Encryption.Data(EncryptionKey)
Dim encryptedData As New Encryption.Data
encryptedData = sym.Encrypt(FDFDocument.FDFSavetoStream(FileType, True))
Dim FType As String = "fdf"
Select Case FileType
Case FDFDoc_Class.FDFType.FDF
FType = "fdf"
Case FDFDoc_Class.FDFType.xFDF
FType = "xfdf"
Case FDFDoc_Class.FDFType.XML
FType = "xml"
Case FDFDoc_Class.FDFType.XDP
FType = "xdp"
Case Else
FType = "fdf"
End Select
_result = SendMail(MailMsg, "PDFFORMDATA." & FType, encryptedData.Bytes)
Return _result
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.SMTPServer.FDFSendEmail", 3)
Return False
End Try
End Function
Public Function FDFSendEmail(ByVal MailMsg As Net.Mail.MailMessage, ByVal DecodedFDFDocument() As Byte, ByVal FileType As FDFDoc_Class.FDFType, ByVal EncryptionKey As String, Optional ByVal EncryptionProvider As Encryption.Symmetric.Provider = Encryption.Symmetric.Provider.Rijndael) As Boolean
Dim _result As Boolean
Dim xToday As String
xToday = DateTime.Now.ToString
Try
Dim memstream As New MemoryStream(DecodedFDFDocument)
Dim clsEncrypt As New Encryption.Asymmetric()
Dim sym As New Encryption.Symmetric(EncryptionProvider)
sym.Key = New Encryption.Data(EncryptionKey)
Dim encryptedData As New Encryption.Data
encryptedData = sym.Encrypt(memstream)
Dim FType As String = "fdf"
Select Case FileType
Case FDFDoc_Class.FDFType.FDF
FType = "fdf"
Case FDFDoc_Class.FDFType.xFDF
FType = "xfdf"
Case FDFDoc_Class.FDFType.XML
FType = "xml"
Case FDFDoc_Class.FDFType.XDP
FType = "xdp"
Case Else
FType = "fdf"
End Select
_result = SendMail(MailMsg, "PDFFORMDATA." & FType, encryptedData.Bytes)
Return _result
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.SMTPServer.FDFSendEmail", 3)
Return False
End Try
End Function
Public Function FDFSendEmail(ByVal MailMsg As Net.Mail.MailMessage, ByVal EncodedFDFDocument() As Byte, ByVal FileType As FDFDoc_Class.FDFType) As Boolean
Dim _result As Boolean
Dim xToday As String
xToday = DateTime.Now.ToString
Try
Dim memstream As New MemoryStream(EncodedFDFDocument)
Dim FType As String = "fdf"
Select Case FileType
Case FDFDoc_Class.FDFType.FDF
FType = "fdf"
Case FDFDoc_Class.FDFType.xFDF
FType = "xfdf"
Case FDFDoc_Class.FDFType.XML
FType = "xml"
Case FDFDoc_Class.FDFType.XDP
FType = "xdp"
Case Else
FType = "fdf"
End Select
_result = SendMail(MailMsg, "PDFFORMDATA." & FType, EncodedFDFDocument)
Return _result
Catch Ex As Exception
_SMTPErrors.FDFAddError(FDFErrors.FDFErc.FDFErcFileSysErr, "Error: " & Ex.Message, "FDFApp.SMTPServer.FDFSendEmail", 3)
Return False
End Try
End Function
Private disposedValue As Boolean = False ' To detect redundant calls
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
Try
_SMTPCredentials = Nothing
_SMTPErrors = Nothing
_SMTPServer = Nothing
Catch ex As Exception
End Try
End If
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
Public Class MailMessage
Implements IDisposable
Private _MailMessage As New System.Net.Mail.MailMessage
Public Property msgMailMessage() As Net.Mail.MailMessage
Get
Return _MailMessage
End Get
Set(ByVal value As Net.Mail.MailMessage)
_MailMessage = value
End Set
End Property
Public Sub New()
_MailMessage = New System.Net.Mail.MailMessage
End Sub
Public Sub New(ByVal msg As Net.Mail.MailMessage)
_MailMessage = New System.Net.Mail.MailMessage
_MailMessage = msg
End Sub
Public Enum MailFormat
Text = CInt(False)
HTML = CInt(True)
End Enum
Private Structure strucEmailAddress
Dim RecName As String
Dim RecEmail As String
End Structure
Public Sub Add_Recipient(ByVal Email As String, ByVal Name As String)
Dim Addr As Net.Mail.MailAddress
If Name.Length = 0 And Email.Length > 0 Then
Addr = New Net.Mail.MailAddress(Email)
_MailMessage.To.Add(Addr)
ElseIf Email.Length > 0 And Name.Length > 0 Then
Addr = New Net.Mail.MailAddress(Email & "", Name & "")
_MailMessage.To.Add(Addr)
End If
End Sub
Public Sub Add_Recipient(ByVal Email As String)
_MailMessage.To.Add(Email)
End Sub
Public Sub Add_Recipients(ByVal EmailList As String)
_MailMessage.To.Add(EmailList)
End Sub
Private Function Return_Recipients(ByVal EmailAddresses As MailAddressCollection) As String
Dim strEmailList As String = ""
If EmailAddresses.Count <= 0 Then
Return ""
Exit Function
End If
Dim EmailAddress As Net.Mail.MailAddress
Try
For Each EmailAddress In EmailAddresses
Select Case EmailAddress.DisplayName
Case ""
If Not EmailAddress.Address = "" Then
strEmailList = strEmailList & CStr(IIf(strEmailList = "", "", "; ")) & EmailAddress.Address
End If
Case Nothing
If Not EmailAddress.Address = "" Then
strEmailList = strEmailList & CStr(IIf(strEmailList = "", "", "; ")) & EmailAddress.Address
End If
Case Else
If Not EmailAddress.Address = "" Then
strEmailList = strEmailList & CStr(IIf(strEmailList = "", "", "; ")) & EmailAddress.DisplayName & " (" & EmailAddress.Address & ")"
End If
End Select
Next
Return strEmailList
Catch ex As Exception
Return ""
Exit Function
End Try
End Function
Public Sub Add_CcRecipient(ByVal Email As String, Optional ByVal Name As String = "")
Dim Addr As Net.Mail.MailAddress
If Name.Length = 0 And Email.Length > 0 Then
Addr = New Net.Mail.MailAddress(Email)
_MailMessage.CC.Add(Addr)
ElseIf Email.Length > 0 And Name.Length > 0 Then
Addr = New Net.Mail.MailAddress(Email & "", Name & "")
_MailMessage.CC.Add(Addr)
End If
End Sub
Public Sub Add_BccRecipient(ByVal Email As String, Optional ByVal Name As String = "")
Dim Addr As Net.Mail.MailAddress
If Name.Length = 0 And Email.Length > 0 Then
Addr = New Net.Mail.MailAddress(Email)
_MailMessage.Bcc.Add(Addr)
ElseIf Email.Length > 0 And Name.Length > 0 Then
Addr = New Net.Mail.MailAddress(Email & "", Name & "")
_MailMessage.Bcc.Add(Addr)
End If
End Sub
Public Property msgFromAddress() As MailAddress
Get
Return _MailMessage.From
End Get
Set(ByVal value As MailAddress)
_MailMessage.From = value
End Set
End Property
Public Property msgReplyToAddress() As MailAddress
Get
Return _MailMessage.ReplyTo
End Get
Set(ByVal value As MailAddress)
_MailMessage.ReplyTo = value
End Set
End Property
Public Property msg() As System.Net.Mail.MailMessage
Get
Return _MailMessage
End Get
Set(ByVal value As System.Net.Mail.MailMessage)
_MailMessage = value
End Set
End Property
Public Sub Add_FromAddress(ByVal Email As String, Optional ByVal Name As String = "")
Dim Addr As Net.Mail.MailAddress
If Name.Length = 0 And Email.Length > 0 Then
Addr = New Net.Mail.MailAddress(Email)
_MailMessage.From = Addr
_MailMessage.Sender = Addr
ElseIf Email.Length > 0 And Name.Length > 0 Then
Addr = New Net.Mail.MailAddress(Email, Name)
_MailMessage.From = Addr
_MailMessage.Sender = Addr
End If
End Sub
Public Property msgTo() As Net.Mail.MailAddressCollection
Get
Return _MailMessage.To
End Get
Set(ByVal Value As Net.Mail.MailAddressCollection)
_MailMessage.To.Clear()
For Each x As Net.Mail.MailAddress In Value
_MailMessage.To.Add(x)
Next
End Set
End Property
Public Property msgSubject() As String
Get
Return _MailMessage.Subject & ""
End Get
Set(ByVal Value As String)
_MailMessage.Subject = Value & ""
End Set
End Property
Public Property msgBody() As String
Get
Return _MailMessage.Body & ""
End Get
Set(ByVal Value As String)
_MailMessage.Body = Value & ""
End Set
End Property
Public Property msgCc() As Net.Mail.MailAddressCollection
Get
Return _MailMessage.CC
End Get
Set(ByVal Value As Net.Mail.MailAddressCollection)
_MailMessage.CC.Clear()
For Each x As Net.Mail.MailAddress In Value
_MailMessage.CC.Add(x)
Next
End Set
End Property
Public Property msgBcc() As Net.Mail.MailAddressCollection
Get
Return _MailMessage.Bcc
End Get
Set(ByVal Value As Net.Mail.MailAddressCollection)
_MailMessage.Bcc.Clear()
For Each x As Net.Mail.MailAddress In Value
_MailMessage.Bcc.Add(x)
Next
End Set
End Property
Public Property msgBodyFormatIsHTML() As Boolean
Get
Return _MailMessage.IsBodyHtml
End Get
Set(ByVal isHTML As Boolean)
_MailMessage.IsBodyHtml = isHTML
End Set
End Property
Public Sub Add_Attachment(ByVal streamAttachment As Stream, ByVal FileName As String, ByVal MimeType As String)
Dim nFileLen As Integer = 0, strFileName As String = "", attach As Net.Mail.Attachment
Try
If Not streamAttachment Is Nothing Then
attach = New Net.Mail.Attachment(streamAttachment, FileName, MimeType)
_MailMessage.Attachments.Add(attach)
End If
Catch ex As Exception
End Try
End Sub
Public Sub Add_Attachment(ByVal bytAttachment() As Byte, ByVal FileName As String, ByVal MimeType As String)
Dim nFileLen As Integer = 0, strFileName As String = "", attach As Net.Mail.Attachment
Try
If Not bytAttachment Is Nothing Then
Dim streamAttachment As New MemoryStream(bytAttachment)
attach = New Net.Mail.Attachment(streamAttachment, FileName, MimeType)
_MailMessage.Attachments.Add(attach)
End If
Catch ex As Exception
End Try
End Sub
Public Sub Add_Attachment(ByVal FileName As String, ByVal MimeType As String)
Dim nFileLen As Integer = 0, strFileName As String = "", attach As Net.Mail.Attachment
Try
If File.Exists(FileName) Then
attach = New Net.Mail.Attachment(FileName, MimeType)
_MailMessage.Attachments.Add(attach)
End If
Catch ex As Exception
End Try
End Sub
Public Sub Add_Attachment(ByVal FileName As String)
Dim nFileLen As Integer = 0, strFileName As String = "", attach As Net.Mail.Attachment
Try
If File.Exists(FileName) Then
attach = New Net.Mail.Attachment(FileName)
_MailMessage.Attachments.Add(attach)
End If
Catch ex As Exception
End Try
End Sub
Private disposedValue As Boolean = False ' To detect redundant calls
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
Try
_MailMessage = Nothing
Catch ex As Exception
End Try
End If
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
End Class
End Namespace