Advertisement
Guest User

Untitled

a guest
May 31st, 2017
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.99 KB | None | 0 0
  1. Option Explicit
  2.  
  3. '==================================================================
  4. 'Created by SCINER: lenar2003@mail.ru
  5. '20/04/2005
  6. 'Контрол для отправки писем по SMTP протоколу
  7. 'Через сервер требующий SMTP-авторизацию
  8. '==================================================================
  9.  
  10. Private Const MB_PRECOMPOSED As Long = &H1 ' use precomposed chars
  11. Private Const MB_COMPOSITE As Long = &H2 ' use composite chars
  12. Private Const MB_USEGLYPHCHARS As Long = &H4 ' use glyph chars, not ctrl chars
  13.  
  14. Event Sent() 'ГЕНЕРИРУЕТСЯ ПОСЛЕ ОТПРАВКИ ПИСЬМА
  15. Event ErrorSend(ByVal Description As String) 'ОШИБКА
  16. Event StatusChange(ByVal Description As String) 'ИЗМЕНЕНИЕ СТАТУСА ОТПРАВКИ ПИСЬМА
  17.  
  18. Public Enum CodePages
  19. Win = 1251
  20. Dos = 866
  21. Iso = 28595
  22. Koi8r = 20866
  23. End Enum
  24.  
  25. 'СТАНДАРТНАЯ КОДИРОВКА ЛЯ ТЕЛА ПИСЬМА
  26. Private Const CHARSET As String = "windows-1251" 'iso-8859-1
  27. 'МЕТОД КОДИРОВАНИЯ ВЛОЖЕННЫХ ФАЙЛОВ
  28. Private Const FILE_CODE As String = "base64" 'binary, uuCode
  29. Dim bQuit As Boolean
  30. Dim T As Long
  31. Private Declare Function GetTickCount Lib "kernel32" () As Long
  32. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  33.  
  34. Dim Base64Tab(63) As Byte
  35. Dim DecodeTable(233) As Byte
  36.  
  37. 'КОНСТАНТЫ СОСТОЯНИЙ
  38. Private Const SMTP_Connecting = "[0/9] Соединение"
  39. Private Const SMTP_Identifying = "[1/9] Идентификация"
  40. Private Const SMTP_AuthIdentify = "[2/9] Авторизация"
  41. Private Const SMTP_AuthUsername = "[3/9] Авторизация логина"
  42. Private Const SMTP_AuthPassword = "[4/9] Авторизация пароля"
  43. Private Const SMTP_MailFrom = "[5/9] Установка отправителя"
  44. Private Const SMTP_RcptTo = "[6/9] Установка получателя"
  45. Private Const SMTP_BeginBody = "[7/9] Создание письма"
  46. Private Const SMTP_SendBody = "[8/9] Отправка письма"
  47. Private Const SMTP_Closing = "[9/9] Закрытие соединения"
  48. Private Const SMTP_Closed = "Соединение закрыто"
  49.  
  50. Dim sS As String
  51. Dim ENC As Object
  52. Dim Tmr As Single
  53. Dim Reply As Integer
  54. Dim Start As Single
  55. Dim Response As String
  56. Dim LastState As String
  57. Dim blnSendOK As Boolean
  58. Dim sComplemento As String
  59. Dim MailToSeparated As Collection
  60. Public Busy As Boolean
  61.  
  62. 'ДАННЫЕ ПИСЬМА
  63. Public AuthLogin As String 'SMTP-ЛОГИН ОТПРАВИТЕЛЯ
  64. Public AuthPassword As String 'SMTP-ПАРОЛЬ ОТПРАВИТЕЛЯ
  65. Public Attachments As Collection 'ФАЙЛЫ ПРИСОЕДИНЕННЫЕ К ПИСЬМУ
  66. Public MailTo As String 'АДРЕС(Ы) ПОЛУЧАТЕЛЯ(ЕЙ)
  67. Public MailFrom As String 'АДРЕС ОТПРАВИТЕЛЯ
  68. Public MailFromName As String 'ИМЯ ОТПРАВИТЕЛЯ
  69. Public MailToName As String 'ИМЯ ПОЛУЧАТЕЛЯ
  70. Public MailSubject As String 'ТЕМА ПИСЬМА
  71. Public MailBody As String 'ТЕКСТ СООБЩЕНИЯ
  72. Public MailServer As String 'SMTP-СЕРВЕР
  73. Public MailServerPort As Long 'SMTP-ПОРТ СЕРВЕРА
  74. Public MailPriority As Long
  75.  
  76. 'ФУНКЦИИ ДЛЯ ПЕРЕКОДИРОВКИ ТЕКСТА
  77. Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As String, ByVal cchMultiByte As Long, ByVal lpWideCharStr As String, ByVal cchWideChar As Long) As Long
  78. Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As String, ByVal cchWideChar As Long, ByVal lpMultiByteStr As String, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
  79.  
  80. 'BASE64 КОДЕР/ДЕКОДЕР
  81. Private Sub EncodeB64(ByRef FileIn() As Byte, ByRef Out() As Byte)
  82. 'declarations
  83. Dim Bin(2) As Byte
  84. Dim iTemp As Long
  85. Dim i As Long
  86. Dim Lenght As Long
  87. Dim Remaining As Byte
  88. Dim BytesOut As Long
  89. Lenght = UBound(FileIn) + 1 'lenght of the string
  90. Remaining = ((Lenght) Mod 3)
  91. If Remaining = 0 Then
  92. BytesOut = ((Lenght / 3) * 4) ' how many bytes will the encoded string have
  93. Else
  94. BytesOut = (((Lenght + (3 - Remaining)) / 3) * 4) ' how many bytes will the encoded string have
  95. End If
  96. ReDim Out(BytesOut - 1)
  97. For i = 0 To Lenght - Remaining - 1 Step 3
  98. '3 bytes in
  99. Bin(0) = FileIn(i)
  100. Bin(1) = FileIn(i + 1)
  101. Bin(2) = FileIn(i + 2)
  102. '4 bytes out
  103. Out(iTemp) = Base64Tab((Bin(0) \ 4) And &H3F)
  104. Out(iTemp + 1) = Base64Tab((Bin(0) And &H3) * 16 Or (Bin(1) \ 16) And &HF)
  105. Out(iTemp + 2) = Base64Tab((Bin(1) And &HF) * 4 Or (Bin(2) \ 64) And &H3)
  106. Out(iTemp + 3) = Base64Tab(Bin(2) And &H3F)
  107. iTemp = iTemp + 4
  108. Next
  109. If Remaining = 1 Then ' if there is 1 byte remaining
  110. 'read 1 byte, the second in 0
  111. Bin(0) = FileIn(UBound(FileIn))
  112. Bin(1) = 0
  113. Out(UBound(Out) - 3) = Base64Tab((Bin(0) \ 4) And &H3F)
  114. Out(UBound(Out) - 2) = Base64Tab((Bin(0) And &H3) * 16 Or (Bin(1) \ 16) And &HF)
  115. Out(UBound(Out) - 1) = 61
  116. Out(UBound(Out)) = 61
  117. ElseIf Remaining = 2 Then 'if there are 2 bytes remaining
  118. 'read 2 bytes, the third is 0
  119. Bin(0) = FileIn(UBound(FileIn) - 1)
  120. Bin(1) = FileIn(UBound(FileIn))
  121. Bin(2) = 0
  122. Out(UBound(Out) - 3) = Base64Tab((Bin(0) \ 4) And &H3F)
  123. Out(UBound(Out) - 2) = Base64Tab((Bin(0) And &H3) * 16 Or (Bin(1) \ 16) And &HF)
  124. Out(UBound(Out) - 1) = Base64Tab((Bin(1) And &HF) * 4 Or (Bin(2) \ 64) And &H3)
  125. Out(UBound(Out)) = 61
  126. End If
  127. End Sub
  128. Private Sub Str2ByteArray(StringIn As String, ByteArray() As Byte)
  129. ByteArray = StrConv(StringIn, vbFromUnicode)
  130. End Sub
  131. Private Sub Span(CharsPerLine As Long, InArray() As Byte, OutArray() As Byte)
  132. Dim Lines As Long
  133. Dim i2 As Long
  134. Dim i As Long
  135. Dim TempI As Long
  136. Lines = ((UBound(InArray) + 1) + (UBound(InArray) + 1) Mod CharsPerLine) / CharsPerLine
  137. ReDim OutArray(LBound(InArray) To UBound(InArray) + (Lines * 2))
  138. TempI = 0
  139. While Not TempI > UBound(InArray)
  140. For i = TempI To TempI + CharsPerLine - 1
  141. If i2 > UBound(OutArray) Or i > UBound(InArray) Then Exit Sub
  142. OutArray(i2) = InArray(i)
  143. i2 = i2 + 1
  144. Next
  145. If i2 > UBound(OutArray) Then Exit Sub
  146. OutArray(i2) = 13
  147. OutArray(i2 + 1) = 10
  148. TempI = TempI + CharsPerLine
  149. i2 = i2 + 2
  150. Wend
  151. End Sub
  152. Private Sub Unspan(ArrayIn() As Byte, ArrayOut() As Byte)
  153. Dim sTemp As String
  154. sTemp = StrConv(ArrayIn, vbUnicode)
  155. sTemp = Replace(sTemp, vbCrLf, "")
  156. ArrayOut = StrConv(sTemp, vbFromUnicode)
  157. End Sub
  158. 'СЛУЖЕБНАЯ ПРОЦЕДУРА РАЗДЕЛЕНИЯ СТРОКИ НА СТРОКИ ДЛИНОЙ 76 СИМВОЛОВ
  159. Private Function Do76(ByVal slpz As String) As String
  160. Dim i As Long
  161. Dim B() As Byte
  162. Dim C() As Byte
  163. Dim Pos As Long
  164. B = StrConv(slpz, vbFromUnicode)
  165. ReDim C(UBound(B) * 2)
  166. For i = 0 To UBound(B)
  167. C(Pos) = B(i): Pos = Pos + 1
  168. If ((i + 1) Mod 76 = 0) Then
  169. C(Pos) = 13: Pos = Pos + 1
  170. C(Pos) = 10: Pos = Pos + 1
  171. End If
  172. Next
  173. ReDim Preserve C(Pos - 1)
  174. Do76 = StrConv(C, vbUnicode)
  175. End Function
  176. 'ПЕРЕКОДИРОВКА ФАЙЛА С ИСПОЛЬЗОВАНИЕМ АЛГОРИТМА Base64
  177. Private Function EncodeFile(ByVal FilePath As String) As String
  178. Dim FF As Long
  179. Dim B() As Byte
  180. Dim C() As Byte
  181. FF = FreeFile
  182. Open FilePath For Binary As #FF
  183. ReDim B(LOF(FF) - 1)
  184. Get #FF, , B
  185. Close #FF
  186. EncodeB64 B, C
  187. EncodeFile = StrConv(C, vbUnicode)
  188. End Function
  189. 'ПЕРЕКОДИРОВАНИЕ ИЗ Base64
  190. Private Sub DecodeB64(ByRef FileIn() As Byte, ByRef Out() As Byte)
  191. 'declarations
  192. Dim inp(3) As Byte
  193. Dim iTemp As Long
  194. Dim i As Long
  195. Dim Lenght As Long
  196. Dim Remaining As Byte
  197. Dim BytesOut As Long
  198. Dim lTemp2 As Long
  199. If FileIn(UBound(FileIn)) = 61 Then
  200. Remaining = 1
  201. If FileIn(UBound(FileIn) - 1) = 61 Then
  202. Remaining = 2
  203. End If
  204. End If
  205. Lenght = UBound(FileIn) + 1 'lenght of the string
  206. BytesOut = ((Lenght / 4) * 3) - Remaining ' how many bytes will the decoded string have
  207. ReDim Out(BytesOut - 1)
  208. For i = 0 To Lenght Step 4
  209. inp(0) = DecodeTable(FileIn(i))
  210. inp(1) = DecodeTable(FileIn(i + 1))
  211. inp(2) = DecodeTable(FileIn(i + 2))
  212. inp(3) = DecodeTable(FileIn(i + 3))
  213. If inp(3) = 64 Or inp(2) = 64 Then
  214. If inp(3) = 64 And Not (inp(2) = 64) Then
  215. inp(0) = DecodeTable(FileIn(i))
  216. inp(1) = DecodeTable(FileIn(i + 1))
  217. inp(2) = DecodeTable(FileIn(i + 2))
  218. '2 bytes out
  219. Out(iTemp) = (inp(0) * 4) Or ((inp(1) \ 16) And &H3)
  220. Out(iTemp + 1) = ((inp(1) And &HF) * 16) Or ((inp(2) \ 4) And &HF)
  221. Exit Sub
  222. ElseIf inp(2) = 64 Then
  223. inp(0) = DecodeTable(FileIn(i))
  224. inp(1) = DecodeTable(FileIn(i + 1))
  225. '1 byte out
  226. Out(iTemp) = (inp(0) * 4) Or ((inp(1) \ 16) And &H3)
  227. Exit Sub
  228. End If
  229. End If
  230. '3 bytes out
  231. Out(iTemp) = (inp(0) * 4) Or ((inp(1) \ 16) And &H3)
  232. Out(iTemp + 1) = ((inp(1) And &HF) * 16) Or ((inp(2) \ 4) And &HF)
  233. Out(iTemp + 2) = ((inp(2) And &H3) * 64) Or inp(3)
  234. iTemp = iTemp + 3
  235. Next
  236. End Sub
  237.  
  238.  
  239.  
  240. Private Sub Msg(ByVal Txt As String)
  241. RaiseEvent StatusChange(Txt)
  242. End Sub
  243. Private Property Get smtpState() As String
  244. smtpState = LastState
  245. End Property
  246. Private Property Let smtpState(ByVal vNewValue As String)
  247. LastState = vNewValue
  248. RaiseEvent StatusChange(vNewValue)
  249. End Property
  250.  
  251. 'БЫСТРЫЙ СБРОС ПАРАМЕТРОВ ПИСЬМА
  252. Sub Reset()
  253. Set Attachments = New Collection
  254. MailServerPort = 25
  255. AuthLogin = vbNullString
  256. AuthPassword = vbNullString
  257. MailTo = vbNullString
  258. MailFrom = vbNullString
  259. MailFromName = vbNullString
  260. MailToName = vbNullString
  261. MailSubject = vbNullString
  262. MailBody = vbNullString
  263. MailServer = vbNullString
  264. MailPriority = 3
  265. End Sub
  266.  
  267. 'СОЗДАНИЕ И ОТПРАВКА ПИСЬМА
  268. Sub Send()
  269. Dim Tmp As String
  270. Dim TP() As String
  271. Dim i As Long
  272. Set MailToSeparated = New Collection
  273. Tmp = Replace(MailTo, ",", ";")
  274. Tmp = Replace(Tmp, " ", vbNullString)
  275. If InStr(Tmp, ";") > 0 Then
  276. TP = Split(Tmp, ";")
  277. For i = 0 To UBound(TP)
  278. If TP(i) Like "*@*.*" Then MailToSeparated.Add TP(i)
  279. Next
  280. Else
  281. Call MailToSeparated.Add(Tmp)
  282. End If
  283. Call SendEmail(MailServer, MailFromName, MailFrom, MailToName, Tmp, MailSubject, MailBody)
  284. End Sub
  285. Private Sub SendEmail(MailServerName As String, FromName As String, FromEmailAddress As String, ToName As String, ToEmailAddress As String, EmailSubject As String, EmailBodyOfMessage As String)
  286. Winsock1.LocalPort = 0 ' Must Set local port To 0 (Zero) or you can only send 1 e-mail per program start
  287. 'create the message complemente (after DATA)
  288. sComplemento = CreateMessage(FromName, FromEmailAddress, ToName, ToEmailAddress, EmailSubject, EmailBodyOfMessage)
  289. Dim lP1 As Long
  290. Dim lP2 As Long
  291. lP1 = 50
  292. If Winsock1.State = sckClosed Then ' Check To see if socet is closed
  293. Busy = True
  294. Winsock1.Protocol = sckTCPProtocol ' Set protocol For sending
  295. Winsock1.RemoteHost = MailServerName ' Set the server address
  296. Winsock1.RemotePort = MailServerPort ' Set the SMTP Port
  297. bQuit = True
  298. Winsock1.Connect ' Start connection
  299. Do While bQuit
  300. If (lP2 = lP1) Then
  301. RaiseEvent ErrorSend("Неудалось отправить письмо, т.к. сервер не отвечает.")
  302. Busy = False
  303. smtpState = SMTP_Closed
  304. Winsock1.CloseSocket
  305. Exit Do
  306. End If
  307. DoEvents
  308. lP2 = lP2 + 1
  309. RaiseEvent StatusChange("[" & Format(lP2, "00") & "/" & Format(lP1, "00") & "] Подключение к серверу. Ожидается ответ...")
  310. Sleep 100
  311. Loop
  312. End If
  313. End Sub
  314.  
  315. Private Sub UserControl_Initialize()
  316. 'СБРОС ПАРАМЕТРОВ ПИСЬМА
  317. Call Reset
  318. 'ИНИЦИАЛИЗАЦИЯ Base64 ТАБЛИЦЫ
  319. Dim i As Long
  320. Dim tDecodeTable As Variant
  321. tDecodeTable = Array("255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "62", "255", "255", "255", "63", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "255", "255", "255", "64", "255", "255", "255", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", _
  322. "18", "19", "20", "21", "22", "23", "24", "25", "255", "255", "255", "255", "255", "255", "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", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255" _
  323. , "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255")
  324. For i = LBound(tDecodeTable) To UBound(tDecodeTable)
  325. DecodeTable(i) = tDecodeTable(i)
  326. Next
  327. For i = 65 To 90
  328. Base64Tab(i - 65) = i
  329. Next
  330. For i = 97 To 122
  331. Base64Tab(i - 71) = i
  332. Next
  333. For i = 0 To 9
  334. Base64Tab(i + 52) = 48 + i
  335. Next
  336. Base64Tab(62) = 43
  337. Base64Tab(63) = 47
  338. End Sub
  339.  
  340. Private Sub UserControl_Resize()
  341. On Error Resume Next
  342. Width = 32 * 15
  343. Height = Width
  344. End Sub
  345.  
  346. 'ВЕСЬ ПРОЦЕСС ОБЩЕНИЯ С СЕРВЕРОМ
  347. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  348.  
  349. Dim B() As Byte
  350. Dim C() As Byte
  351. Dim Tmp As String
  352.  
  353. bQuit = False
  354. Winsock1.GetData Response, vbString
  355.  
  356. Select Case Val(Mid$(Response, 1, 3))
  357. Case 220
  358. smtpState = SMTP_Identifying
  359. Call SendData("HELO " & Winsock1.LocalHostName & vbCrLf)
  360. Case 235
  361. Select Case smtpState
  362. Case SMTP_AuthPassword
  363. smtpState = SMTP_MailFrom
  364. WinsockSendData "MAIL FROM: <" & MailFrom & ">" & vbCrLf
  365. End Select
  366. Case 250
  367. Select Case smtpState
  368. Case SMTP_Identifying
  369. If Trim(AuthLogin) <> "" And Trim(AuthPassword) <> "" Then
  370. smtpState = SMTP_AuthIdentify
  371. WinsockSendData "AUTH LOGIN" & vbCrLf
  372. Else
  373. smtpState = SMTP_MailFrom
  374. WinsockSendData "MAIL FROM: <" & MailFrom & ">" & vbCrLf
  375. End If
  376. Case SMTP_MailFrom
  377. WinsockSendData "RCPT TO:<" & MailToSeparated.Item(1) & ">" & vbCrLf
  378. Call MailToSeparated.Remove(1)
  379. smtpState = IIf(MailToSeparated.Count > 0, SMTP_MailFrom, SMTP_RcptTo)
  380. Case SMTP_RcptTo
  381. smtpState = SMTP_BeginBody
  382. WinsockSendData "DATA" & vbCrLf
  383. Case SMTP_SendBody
  384. smtpState = SMTP_Closing
  385. WinsockSendData "QUIT"
  386. Winsock1.CloseSocket
  387. smtpState = SMTP_Closed
  388. blnSendOK = True
  389. RaiseEvent Sent
  390. End Select
  391. Case 251
  392. Select Case smtpState
  393. Case SMTP_RcptTo
  394. smtpState = SMTP_BeginBody
  395. WinsockSendData "DATA" & vbCrLf
  396. End Select
  397. Case 334
  398. Select Case smtpState
  399. 'ОТПРАВКА ЛОГИНА
  400. Case SMTP_AuthIdentify
  401. smtpState = SMTP_AuthUsername
  402. B = StrConv(AuthLogin, vbFromUnicode)
  403. Call EncodeB64(B, C)
  404. Tmp = StrConv(C, vbUnicode)
  405. WinsockSendData Tmp & vbCrLf
  406. 'ОТПРАВКА ПАРОЛЯ
  407. Case SMTP_AuthUsername
  408. smtpState = SMTP_AuthPassword
  409. B = StrConv(AuthPassword, vbFromUnicode)
  410. Call EncodeB64(B, C)
  411. Tmp = StrConv(C, vbUnicode)
  412. WinsockSendData Tmp & vbCrLf
  413. End Select
  414. Case 354
  415. smtpState = SMTP_SendBody
  416. Call SendData(sComplemento)
  417. Call SendData(vbCrLf & "." & vbCrLf)
  418. Case Is >= 400
  419. RaiseEvent ErrorSend(Response)
  420. smtpState = SMTP_Closed
  421. Winsock1.CloseSocket
  422. Case Else
  423. smtpState = SMTP_Closed
  424. Winsock1.CloseSocket
  425. End Select
  426.  
  427. End Sub
  428.  
  429. 'ПИСЬМО ЦЕЛИКОМ ПРОХОДИТ ЧЕРЕЗ ЭТУ ПРОЦЕДУРУ
  430. Private Function SendData(sText As String)
  431. Call ToLog(sText)
  432. Winsock1.SendData sText
  433. End Function
  434.  
  435. Sub WinsockSendData(ByVal strMsg As String)
  436. Winsock1.SendData strMsg
  437. End Sub
  438.  
  439. 'МОЖНО ПО ПУТИ СОХРАНЯТЬ "ИСХОДНИК" ПИСЬМА
  440. Private Sub ToLog(ByVal sString As String)
  441. 'Open "c:\sendmail.txt" For Append As #11
  442. ' Print #11, sString
  443. 'Close #11
  444. End Sub
  445.  
  446. Private Function FastB64Encode(ByVal strBuf As String) As String
  447. Dim B() As Byte
  448. Dim C() As Byte
  449. B = StrConv(strBuf, vbFromUnicode)
  450. EncodeB64 B, C
  451. FastB64Encode = StrConv(C, vbUnicode)
  452. End Function
  453.  
  454. 'СОЗДАНИЕ "ИСХОДНИКА СООБЩЕНИЯ"
  455. Private Function CreateMessage(FromName As String, _
  456. FromEmailAddress As String, _
  457. ToName As String, _
  458. ToEmailAddress As String, _
  459. EmailSubject As String, _
  460. EmailBodyOfMessage As String) As String
  461.  
  462. 'ЗДЕСЬ ИСПОЛЬЗУЕТСЯ СКЛЕИВАНИЕ ПЕРЕМЕННЫХ
  463. 'КТО-ТО МОЖЕТ СКАЗАТЬ ЧТО ЭТО ОЧЕНЬ МЕДЛЕННО
  464. 'НО ЭТО ТОЛЬКО ТАК КАЖЕТСЯ, НА САМОМ ДЕЛЕ ЗДЕСЬ ОЧЕНЬ МАЛО ТОЧЕК СКЛЕИВАНИЯ,
  465. 'ПОЭТОМУ ДАННЫЙ КОД НЕ СНИЖАЕТ ОБЩЕЙ ПРОИЗВОДИТЕЛЬНОСТИ АЛГОРИТМА
  466.  
  467. 'ЗДЕСЬ НЕПРАВИЛЬНО ЛИШЬ ТО, ЧТО ЭТОТ МЕТОД НЕ РАСЧИТАН НА ФАЙЛЫ
  468. 'РАЗМЕРОМ В НЕСКОЛЬКО ДЕСЯТКОВ МЕГАБАЙТ!
  469. 'Т.К. ЗДЕСЬ ПОТРЕБУЕТСЯ ТАКОЙ ОБЪЕМ ОПЕРАТИВНОЙ ПАМЯТИ
  470. 'СКОЛЬКО ВЕСЯТ ВСЕ ВЛОЖЕННЫЕ ФАЙЛЫ * 1.3
  471. 'Т.К. ОНИ ЕЩЕ ПЕРЕКОДИРУЮТСЯ ПО АЛГОРИТМУ Base64
  472. Dim i As Integer
  473.  
  474. 'ЗАГОЛОВОК ПИСЬМА
  475. sS = vbNullString
  476. 'sS = sS & "X-Originating-IP: [x.x.x.x]" & vbCrLf
  477. 'X-Originating-Email: [user@domain.ru]
  478. 'X-Sender: user@domain.ru
  479. sS = sS & "X-Priority: " & VBA.Trim$(MailPriority) & vbCrLf
  480. sS = sS & "From: =?windows-1251?B?" & FastB64Encode(FromName) & "?= <" & FromEmailAddress & " > " & vbCrLf
  481. sS = sS & "To: " & ToEmailAddress & vbCrLf
  482. sS = sS & "Subject: =?windows-1251?B?" & FastB64Encode(EmailSubject) & "?=" & vbCrLf
  483. sS = sS & "Date: " & Format(Date, "Ddd") & ", " & Format(Date, "dd Mmm YYYY") & " " & Format(Time, "hh:mm:ss") & " -0300" & vbCrLf
  484. sS = sS & "MIME-Version: 1.0" & vbCrLf
  485. sS = sS & "Content-Type: multipart/mixed;" & vbCrLf
  486. sS = sS & vbTab & "boundary=""" & "----=_NextPart_000_000C_01C21C67F3F2CCA0" & """" & vbCrLf
  487. sS = sS & vbCrLf
  488. sS = sS & "This is a multi-part message in MIME format." & vbCrLf
  489. sS = sS & vbCrLf
  490. sS = sS & "------=_NextPart_000_000C_01C21C67F3F2CCA0" & vbCrLf
  491. sS = sS & "Content-Type: text/plain;" & vbCrLf
  492. sS = sS & vbTab & "charset=""" & CHARSET & """" & vbCrLf
  493. sS = sS & "Content-Transfer-Encoding: 7bit" & vbCrLf
  494. sS = sS & vbCrLf
  495. sS = sS & EmailBodyOfMessage & vbCrLf
  496. sS = sS & vbCrLf
  497.  
  498. 'КОДИРУЕМ ВЛОЖЕННЫЕ ФАЙЛЫ ПИСЬМА
  499. For i = 1 To Attachments.Count
  500. sS = sS & "------=_NextPart_000_000C_01C21C67F3F2CCA0" & vbCrLf
  501. sS = sS & "Content-Type: application/octet-stream;" & vbCrLf
  502. sS = sS & vbTab & "name=""" & Dir(Attachments.Item(i)) & """" & vbCrLf
  503. sS = sS & "Content-Transfer-Encoding: " & FILE_CODE & vbCrLf
  504. sS = sS & "Content-Disposition: attachment;" & vbCrLf
  505. sS = sS & vbTab & "filename=""" & Dir(Attachments.Item(i)) & """" & vbCrLf
  506. sS = sS & vbCrLf
  507. sS = sS & Do76(EncodeFile(Attachments.Item(i))) & vbCrLf
  508. Next
  509.  
  510. sS = sS & vbCrLf
  511. sS = sS & "------=_NextPart_000_000C_01C21C67F3F2CCA0--" & vbCrLf
  512. CreateMessage = sS
  513.  
  514. End Function
  515.  
  516. Public Function Convert(ByVal strSrc As String, ByVal nFromCP As CodePages, ByVal nToCP As CodePages) As String
  517. Dim nLen As Long
  518. Dim strDst As String
  519. Dim strRet As String
  520. Dim nRet As Long
  521. nLen = VBA.Len(strSrc)
  522. strDst = VBA.Space$(nLen * 2)
  523. strRet = VBA.Space$(nLen * 2)
  524. nRet = MultiByteToWideChar(nFromCP, MB_PRECOMPOSED, strSrc, nLen, strDst, nLen)
  525. nRet = WideCharToMultiByte(nToCP, 0, strDst, nRet, strRet, nLen * 2, ByVal 0, 0)
  526. Convert = VBA.Left$(strRet, nRet)
  527. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement