-
Notifications
You must be signed in to change notification settings - Fork 1
/
modMail.vb
545 lines (487 loc) · 25.8 KB
/
modMail.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
Imports System.ComponentModel
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Security
Imports System.Net.Sockets
Imports System.Security.Authentication
Imports System.Text
Module modMail
' Credit for the POP3 part of the mail module goes to evry1falls from CodeProject: http://www.codeproject.com/Tips/441809/Receiving-response-from-POP3-mail-server
' Credit for the IMAP part of the mail module goes to Sridhar Rajan Venkataramani: https://code.msdn.microsoft.com/windowsdesktop/Simple-IMAP-CLIENT-b249d2e6
'SMTP Client
Private oClient As SmtpClient = New SmtpClient
Private smtpLock As New Object
'POP3/IMAP Client Shared
Private pClient As TcpClient = New TcpClient
Private m_sslStream As SslStream
Private m_buffer As Byte()
'IMAP Client
Private m_dummy As Byte()
'POP3 Client
Private Read_Stream As StreamReader
Private NetworkS_tream As NetworkStream
Private ret_Val As Integer
Private Response As String
Private Parts() As String
Private StatResp As String
Private server_Stat(2) As String
Dim tmrMailCheckTimer As System.Timers.Timer
Function AddMailkey(Optional ByVal strNickname As String = "", Optional ByVal strCmdAllowlist As String = "", Optional ByVal strCmdKey As String = "") As String
If strNickname = "" Then
strNickname = InputBox("Enter the nickname of the user whose mailkey you are adding", "Add Mailkey")
End If
If strCmdAllowlist = "" Then
strCmdAllowlist = InputBox("Enter an email header which is allowed to issue commands to this system.", "Add Mailkey")
End If
If strCmdKey = "" Then
strCmdKey = InputBox("Enter a random alphanumeric key which is required to submit commands to this system. It should be used as the display name for the home automation controller's mail service account.", "Add mailkey")
End If
If modPersons.CheckDbForPerson(strNickname) = 0 Then
My.Application.Log.WriteEntry("Nonexistent username provided", TraceEventType.Warning)
Return "Nonexistent username provided"
ElseIf strCmdKey = "" OrElse strCmdAllowlist = "" OrElse strCmdKey = "" Then
My.Application.Log.WriteEntry("Inadequate mailkey setup information provided", TraceEventType.Warning)
Return "Inadequate mailkey setup information provided"
Else
modDatabase.Execute("INSERT INTO MAILKEYS (Nickname, CmdAllowlist, CmdKey) VALUES('" + strNickname + "', '" + strCmdAllowlist + "', '" + strCmdKey + "')")
Return "Mailkey added"
End If
End Function
Sub CheckMail()
If My.Settings.Mail_Enable = True Then
If modGlobal.IsOnline = True AndAlso My.Settings.Mail_IMAPMode = False Then
Try
If pClient.Connected = True Then
CloseServer()
pClient = New TcpClient(My.Settings.Mail_POPHost, My.Settings.Mail_POPPort)
ret_Val = 0
Exit Sub
Else
pClient = New TcpClient(My.Settings.Mail_POPHost, My.Settings.Mail_POPPort)
NetworkS_tream = pClient.GetStream 'Read the stream
m_sslStream = New SslStream(NetworkS_tream) 'Read SSL stream
m_sslStream.AuthenticateAsClient(My.Settings.Mail_POPHost) 'Auth
Read_Stream = New StreamReader(m_sslStream) 'Read the stream
StatResp = Read_Stream.ReadLine()
StatResp = Login(m_sslStream, "USER " & My.Settings.Mail_Username)
My.Application.Log.WriteEntry("POP3: " & StatResp)
StatResp = Login(m_sslStream, "PASS " & My.Settings.Mail_Password)
My.Application.Log.WriteEntry("POP3: " & StatResp)
StatResp = Login(m_sslStream, "STAT ")
My.Application.Log.WriteEntry("POP3: " & StatResp)
'Get Messages count
server_Stat = StatResp.Split(" ")
My.Application.Log.WriteEntry("POP3 Message count: " & server_Stat(1))
ret_Val = 1
End If
If IsNumeric(server_Stat(1)) = True Then 'Apparently POP3 returned 'bad' during a "Temporary system problem" and 'Command' when an invalid password is used, which I want to mitigate.
GetMessages(server_Stat(1))
Else
My.Application.Log.WriteEntry("Mail server returned a bad message count", TraceEventType.Warning)
End If
CloseServer()
Catch SocketEx As System.Net.Sockets.SocketException
My.Application.Log.WriteException(SocketEx, TraceEventType.Warning, "usually caused by a mail connection timeout")
Catch NullRefEx As System.NullReferenceException
My.Application.Log.WriteException(NullRefEx, TraceEventType.Warning, "usually caused by an empty POP3 response")
End Try
End If
Else
My.Application.Log.WriteEntry("Mail module is disabled")
End If
End Sub
Sub CheckMailImap()
If My.Settings.Mail_Enable = True Then
If modGlobal.IsOnline = True AndAlso My.Settings.Mail_IMAPMode = True Then
Try
pClient = New TcpClient(My.Settings.Mail_IMAPHost, My.Settings.Mail_IMAPPort)
m_sslStream = New SslStream(pClient.GetStream())
m_sslStream.AuthenticateAsClient(My.Settings.Mail_IMAPHost)
ReceiveResponse("")
ReceiveResponse("$ LOGIN " & My.Settings.Mail_Username & " " & My.Settings.Mail_Password & vbCrLf)
ReceiveResponse("$ SELECT INBOX" & vbCrLf)
ReceiveResponse("$ STATUS INBOX (MESSAGES)" & vbCrLf)
ReceiveResponse("$ FETCH 1 body[header]" & vbCrLf)
ReceiveResponse("$ STORE 1 +FLAGS (\Deleted)" & vbCrLf)
ReceiveResponse("$ EXPUNGE" & vbCrLf)
ReceiveResponse("$ LOGOUT" & vbCrLf)
Catch SocketEx As System.Net.Sockets.SocketException
My.Application.Log.WriteException(SocketEx, TraceEventType.Warning, "usually caused by a mail connection timeout")
Catch AuthEx As System.Security.Authentication.AuthenticationException
My.Application.Log.WriteException(AuthEx)
Catch NullRefEx As System.NullReferenceException
My.Application.Log.WriteException(NullRefEx, TraceEventType.Warning, "usually caused by an empty IMAP response")
End Try
End If
Else
My.Application.Log.WriteEntry("Mail module is disabled")
End If
End Sub
Sub CheckMailHandler(source As Object, e As System.Timers.ElapsedEventArgs)
If My.Settings.Mail_IMAPMode = True Then
CheckMailImap()
Else
CheckMail()
End If
End Sub
Sub CloseServer()
StatResp = Login(m_sslStream, "QUIT ")
My.Application.Log.WriteEntry("POP3: " & StatResp)
pClient.Close()
ret_Val = 0
End Sub
Sub CreateMailkeysDb()
modDatabase.Execute("CREATE TABLE IF NOT EXISTS MAILKEYS(Id INTEGER PRIMARY KEY, Nickname TEXT UNIQUE, CmdAllowlist TEXT, CmdKey TEXT)")
modDatabase.Execute("ALTER TABLE MAILKEYS RENAME COLUMN CmdWhitelist TO CmdAllowlist")
If My.Settings.Mail_CmdWhitelist <> "" AndAlso My.Settings.Mail_CmdWhitelist <> "(deprecated)" Then
AddMailkey("me", My.Settings.Mail_CmdWhitelist, My.Settings.Mail_CmdKey)
My.Settings.Mail_CmdWhitelist = "(deprecated)"
My.Settings.Mail_CmdKey = "(deprecated)"
End If
End Sub
Function Disable() As String
Unload()
My.Settings.Mail_Enable = False
My.Application.Log.WriteEntry("Mail module is disabled")
Return "Mail module disabled"
End Function
Function Enable() As String
My.Settings.Mail_Enable = True
My.Application.Log.WriteEntry("Mail module is enabled")
Load()
Return "Mail module enabled"
End Function
''' <summary>
''' This function returns the CmdKey of a CmdAllowlist email.
''' </summary>
''' <param name="strCmdAllowlist">Allowlist entry to look for</param>
''' <returns>Matching key for allowlist</returns>
Function GetCmdKeyFromAllowlist(ByVal strCmdAllowlist) As String
Dim result As String = ""
modDatabase.ExecuteReader("SELECT CmdKey FROM MAILKEYS WHERE CmdAllowlist = '" & strCmdAllowlist & "'", result)
Return result
End Function
''' <summary>
''' This function returns the nickname of an inbound mail.
''' </summary>
''' <param name="strCmdAllowlist">Allowlist entry to look for</param>
''' <param name="strCmdKey">Command key to look for</param>
''' <returns>Matching nickname for inbound mail</returns>
Function GetNicknameFromKey(ByVal strCmdAllowlist, ByVal strCmdKey) As String
Dim result As String = ""
modDatabase.ExecuteReader("SELECT Nickname FROM MAILKEYS WHERE CmdAllowlist = '" & strCmdAllowlist & "' AND CmdKey = '" & strCmdKey & "'", result)
Return result
End Function
Sub GetEmails(ByVal Server_Command As String)
Dim m_buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(Server_Command.ToCharArray())
Dim stream_Reader As StreamReader
Dim TxtLine, CmdRec, ReFrom, ReSubj As String
Dim CmdTo As String = "", CmdFrom As String = "", CmdSubj As String = "", CmdID As String = "", CmdKeyLookup As String = "", CmdNickLookup As String = ""
Try
m_sslStream.Write(m_buffer, 0, m_buffer.Length)
stream_Reader = New StreamReader(m_sslStream)
Do While stream_Reader.Peek() <> -1
TxtLine = stream_Reader.ReadLine()
If TxtLine.StartsWith("Received: ") Then
CmdRec = String.Copy(TxtLine)
My.Application.Log.WriteEntry("Command " & CmdRec)
End If
If TxtLine.StartsWith("Message-ID: ") Then
CmdID = String.Copy(TxtLine)
My.Application.Log.WriteEntry("Command " & CmdID)
End If
If TxtLine.StartsWith("Subject: ") Then
CmdSubj = String.Copy(TxtLine)
My.Application.Log.WriteEntry("Command " & CmdSubj)
End If
If TxtLine.StartsWith("From: ") Then
CmdFrom = String.Copy(TxtLine)
My.Application.Log.WriteEntry("Command " & CmdFrom)
End If
If TxtLine.StartsWith("To: ") Then
CmdTo = String.Copy(TxtLine)
My.Application.Log.WriteEntry("Command " & CmdTo)
End If
If CmdSubj <> "" AndAlso CmdFrom <> "" AndAlso CmdTo <> "" AndAlso CmdID <> "" Then
Exit Do
End If
Loop
If CmdSubj <> "" AndAlso CmdFrom <> "" AndAlso CmdTo <> "" AndAlso CmdID <> "" Then
ReFrom = CmdFrom.Replace("From: ", "")
CmdKeyLookup = GetCmdKeyFromAllowlist(ReFrom)
If CmdKeyLookup <> "" Then
If CmdTo = "To: " & CmdKeyLookup & " <" & My.Settings.Mail_From & ">" Then
CmdNickLookup = GetNicknameFromKey(ReFrom, CmdKeyLookup)
My.Application.Log.WriteEntry("Received email from " + CmdNickLookup + ", command key validated")
ReSubj = CmdSubj.Replace("Subject: ", "")
modConverse.Interpret(ReSubj, True, False, CmdNickLookup)
Else
My.Application.Log.WriteEntry("Received email from authorized user, but command key was not valid")
End If
Else
My.Application.Log.WriteEntry("Received email from unauthorized user, ignoring")
End If
End If
Catch ex As Exception
My.Application.Log.WriteException(ex)
End Try
End Sub
Sub GetMessages(ByVal Num_Emails As Integer)
Dim List_Resp As String
Dim StrRetr As String
Dim I As Integer
For I = 1 To Num_Emails
List_Resp = Login(m_sslStream, "LIST " & I.ToString)
My.Application.Log.WriteEntry("POP3: " & List_Resp)
StrRetr = ("RETR " & I & vbCrLf)
GetEmails(StrRetr)
Next I
End Sub
Function Load() As String
My.Application.Log.WriteEntry("Loading mail module")
If My.Settings.Mail_Enable = True Then
If My.Settings.Mail_IMAPMode = False AndAlso My.Settings.Mail_POPHost = "" Then
My.Application.Log.WriteEntry("No mail POP host set, asking for it")
My.Settings.Mail_POPHost = InputBox("Enter mail POP host.", "Mail POP Host")
End If
If My.Settings.Mail_IMAPMode = False AndAlso My.Settings.Mail_POPPort = "" Then
My.Application.Log.WriteEntry("No mail POP port set, asking for it")
My.Settings.Mail_POPPort = InputBox("Enter mail POP port.", "Mail POP Port", "995")
End If
If My.Settings.Mail_IMAPMode = True AndAlso My.Settings.Mail_IMAPHost = "" Then
My.Application.Log.WriteEntry("No mail IMAP host set, asking for it")
My.Settings.Mail_IMAPHost = InputBox("Enter mail IMAP host.", "Mail IMAP Host")
End If
If My.Settings.Mail_IMAPMode = True AndAlso My.Settings.Mail_IMAPPort = "" Then
My.Application.Log.WriteEntry("No mail IMAP port set, asking for it")
My.Settings.Mail_IMAPPort = InputBox("Enter mail IMAP port.", "Mail IMAP Port", "993")
End If
If My.Settings.Mail_SMTPHost = "" Then
My.Application.Log.WriteEntry("No mail SMTP host set, asking for it")
My.Settings.Mail_SMTPHost = InputBox("Enter mail SMTP host.", "Mail SMTP Host")
End If
If My.Settings.Mail_SMTPPort = "" Then
My.Application.Log.WriteEntry("No mail SMTP port set, asking for it")
My.Settings.Mail_SMTPPort = InputBox("Enter mail SMTP port.", "Mail SMTP Port", "587")
End If
If My.Settings.Mail_Username = "" OrElse My.Settings.Mail_From = "" Then
My.Application.Log.WriteEntry("No mail username set, asking for it")
My.Settings.Mail_Username = InputBox("Enter the full email address of the home automation controller's mail service account.", "Mail Username")
My.Settings.Mail_From = My.Settings.Mail_Username
End If
If My.Settings.Mail_Password = "" Then
My.Application.Log.WriteEntry("No mail password set, asking for it")
SetMailPassword()
End If
If My.Settings.Mail_To = "" Then
My.Application.Log.WriteEntry("No mail to address set, asking for it")
My.Settings.Mail_To = InputBox("Enter the email account you want to send notifications to.", "Mail To")
End If
' TODO: Upgrade These
If My.Settings.Mail_CmdWhitelist = "" Then
My.Application.Log.WriteEntry("No command allowlist set, asking for it")
My.Settings.Mail_CmdWhitelist = InputBox("Enter an email header which is allowed to issue commands to this system.", "Mail Command Allowlist")
End If
If My.Settings.Mail_CmdKey = "" Then
My.Application.Log.WriteEntry("No command key set, asking for it")
My.Settings.Mail_CmdKey = InputBox("Enter a random alphanumeric key which is required to submit commands to this system. It should be used as the display name for the home automation controller's mail service account.", "Mail Command Key")
End If
CreateMailkeysDb()
oClient.Host = My.Settings.Mail_SMTPHost
oClient.Port = My.Settings.Mail_SMTPPort
If oClient.Port = 465 OrElse oClient.Port = 587 Then
oClient.EnableSsl = True
Else
oClient.EnableSsl = False
End If
oClient.UseDefaultCredentials = False
oClient.Credentials = New System.Net.NetworkCredential(My.Settings.Mail_Username, My.Settings.Mail_Password)
oClient.DeliveryMethod = SmtpDeliveryMethod.Network
AddHandler oClient.SendCompleted, AddressOf oClient_SendCompleted
tmrMailCheckTimer = New System.Timers.Timer
My.Application.Log.WriteEntry("Scheduling automatic mail checks")
AddHandler tmrMailCheckTimer.Elapsed, AddressOf CheckMailHandler
tmrMailCheckTimer.Interval = 120000 ' 2min
tmrMailCheckTimer.Enabled = True
Return "Mail module loaded"
Else
My.Application.Log.WriteEntry("Mail module is disabled, module not loaded")
Return "Mail module is disabled, module not loaded"
End If
End Function
Sub oClient_SendCompleted(sender As Object, e As AsyncCompletedEventArgs)
Dim token As String = CStr(e.UserState)
If e.Cancelled Then
My.Application.Log.WriteEntry(token & " Send cancelled")
End If
If e.Error IsNot Nothing Then
My.Application.Log.WriteException(e.Error)
Else
My.Application.Log.WriteEntry("Notification mail sent to " + My.Settings.Mail_To)
End If
End Sub
Sub ReceiveResponse(ByVal Server_Command As String)
Try
If Server_Command <> "" Then
If pClient.Connected = True Then
m_dummy = Encoding.ASCII.GetBytes(Server_Command.ToCharArray())
m_sslStream.Write(m_dummy, 0, m_dummy.Length)
Else
My.Application.Log.WriteEntry("IMAP: TCP Connection is disconnected", TraceEventType.Error)
End If
End If
m_sslStream.Flush()
Dim stream_Reader As StreamReader = New StreamReader(m_sslStream)
Dim TxtLine, CmdRec, ReFrom, ReSubj As String
Dim CmdTo As String = "", CmdFrom As String = "", CmdSubj As String = "", CmdID As String = "", CmdKeyLookup As String = "", CmdNickLookup As String = ""
Do While stream_Reader.Peek() <> -1
TxtLine = stream_Reader.ReadLine()
If TxtLine.StartsWith("*") Then
My.Application.Log.WriteEntry("IMAP: " & TxtLine)
Else
If TxtLine.StartsWith("Received: ") Then
CmdRec = String.Copy(TxtLine)
My.Application.Log.WriteEntry("Command " & CmdRec)
End If
If TxtLine.StartsWith("Message-ID: ") Then
CmdID = String.Copy(TxtLine)
My.Application.Log.WriteEntry("Command " & CmdID)
End If
If TxtLine.StartsWith("Subject: ") Then
CmdSubj = String.Copy(TxtLine)
My.Application.Log.WriteEntry("Command " & CmdSubj)
End If
If TxtLine.StartsWith("From: ") Then
CmdFrom = String.Copy(TxtLine)
CmdFrom = CmdFrom.Replace("""", "")
My.Application.Log.WriteEntry("Command " & CmdFrom)
End If
If TxtLine.StartsWith("To: ") Then
CmdTo = String.Copy(TxtLine)
My.Application.Log.WriteEntry("Command " & CmdTo)
End If
If CmdSubj <> "" AndAlso CmdFrom <> "" AndAlso CmdTo <> "" Then
Exit Do
End If
End If
Loop
If CmdSubj <> "" AndAlso CmdFrom <> "" AndAlso CmdTo <> "" Then
ReFrom = CmdFrom.Replace("From: ", "")
CmdKeyLookup = GetCmdKeyFromAllowlist(ReFrom)
If CmdKeyLookup <> "" Then
If CmdTo = "To: " & CmdKeyLookup & " <" & My.Settings.Mail_From & ">" Then
CmdNickLookup = GetNicknameFromKey(ReFrom, CmdKeyLookup)
My.Application.Log.WriteEntry("Received email from " + CmdNickLookup + ", command key validated")
ReSubj = CmdSubj.Replace("Subject: ", "")
modConverse.Interpret(ReSubj, True, False, CmdNickLookup)
Else
My.Application.Log.WriteEntry("Received email from authorized user, but command key was not valid")
End If
Else
My.Application.Log.WriteEntry("Received email from unauthorized user, ignoring")
End If
End If
Catch ex As Exception
My.Application.Log.WriteException(ex)
End Try
End Sub
Sub Send(oSubj As String, oBody As String, Optional ByVal oAttachFileName As String = "", Optional ByVal strToNickname As String = "")
If My.Settings.Mail_Enable = True Then
Dim strToAddress As String = ""
If strToNickname <> "" Then
strToAddress = GetEmailForPerson(strToNickname)
End If
If strToAddress = "" Then
strToAddress = My.Settings.Mail_To
End If
Dim oMsg As New MailMessage()
oMsg.From = New MailAddress(My.Settings.Mail_From, My.Settings.Converse_BotName & " (HAController)")
oMsg.To.Add(New MailAddress(strToAddress))
oMsg.Subject = oSubj
oMsg.Priority = MailPriority.High
oMsg.IsBodyHtml = False
oMsg.Body = oBody & System.Environment.NewLine & System.Environment.NewLine & " -- This bot does not care about your replies and will discard them."
If oAttachFileName <> "" Then
My.Application.Log.WriteEntry("Attaching file " & oAttachFileName & " to email")
Dim oAttachment As New Attachment(oAttachFileName, GetMimeType(oAttachFileName))
oAttachment.ContentDisposition.CreationDate = System.IO.File.GetCreationTime(oAttachFileName)
oAttachment.ContentDisposition.ModificationDate = System.IO.File.GetLastWriteTime(oAttachFileName)
oAttachment.ContentDisposition.ReadDate = System.IO.File.GetLastAccessTime(oAttachFileName)
oMsg.Attachments.Add(oAttachment)
End If
SyncLock smtpLock
Try
oClient.Send(oMsg)
Catch SmtpEx As SmtpException
My.Application.Log.WriteException(SmtpEx)
Catch AuthEx As AuthenticationException
My.Application.Log.WriteException(AuthEx)
End Try
End SyncLock
Else
My.Application.Log.WriteEntry("Mail module is disabled")
End If
End Sub
Function GetMimeType(ByVal strFileName As String) As String
Dim strExtension As String = System.IO.Path.GetExtension(strFileName)
Select Case strExtension
Case ".7z"
Return "application/x-7z-compressed"
Case ".cbr"
Return "application/vnd.comicbook-rar"
Case ".cbz"
Return "application/vnd.comicbook+zip"
Case ".epub"
Return "application/epub+zip"
Case ".mobi", ".prc"
Return "application/x-mobipocket-ebook"
Case ".pdf"
Return System.Net.Mime.MediaTypeNames.Application.Pdf
Case ".rar"
Return "application/x-rar-compressed"
Case ".zip"
Return System.Net.Mime.MediaTypeNames.Application.Zip
Case Else
Return System.Net.Mime.MediaTypeNames.Application.Octet
End Select
End Function
Function Login(ByVal SslStrem As SslStream, ByVal Server_Command As String) As String
Dim justExit As Boolean = False
Dim Read_Stream2 = New StreamReader(SslStrem)
Server_Command = Server_Command + vbCrLf
m_buffer = System.Text.Encoding.ASCII.GetBytes(Server_Command.ToCharArray())
Try
m_sslStream.Write(m_buffer, 0, m_buffer.Length)
Catch IOExcep As System.IO.IOException
My.Application.Log.WriteException(IOExcep)
modMail.Send("Mail crash averted", "Mail crash averted") ' Remove this line later if this retry method actually works
justExit = True
Threading.Thread.Sleep(600000)
End Try
If justExit = False Then
Dim Server_Response As String
Server_Response = Read_Stream2.ReadLine()
Return Server_Response
Else
Return "Dumped"
End If
End Function
''' <summary>
''' Asks for an updated mail password.
''' </summary>
''' <returns>The fact that the mail password has been set</returns>
Function SetMailPassword() As String
My.Settings.Mail_Password = InputBox("Enter mail password. This is not a very secure password storage, do not store credentials to sensitive mail accounts here.", "Mail Password")
Return "Mail password set"
End Function
Function Unload() As String
My.Application.Log.WriteEntry("Unloading mail module")
If tmrMailCheckTimer IsNot Nothing Then
tmrMailCheckTimer.Enabled = False
RemoveHandler tmrMailCheckTimer.Elapsed, AddressOf CheckMailHandler
' CloseServer() - Wasn't used before, causes errors, commenting out this line.
End If
Return "Mail module unloaded"
End Function
End Module