Advertisement
Guest User

SendMail.vbs

a guest
Jun 26th, 2011
3,535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '=============Отправка почтового сообщения с вложением============================
  2. Dim objEmail, MailFrom, MailTo, MailBCC, MailSubject, MailBody, MailAttachment, objArgs, objNamedArgs, re, rv, ss, match, retstr, i
  3. Set objArgs = WScript.Arguments  ' Создаем объект "Аргументы командной строки"
  4. Set objNamedArgs = objArgs.Named     ' Создаем объект "Именованные Аргументы"
  5.  
  6. If ((NOT objNamedArgs.Exists("F")) OR ( NOT objNamedArgs.Exists("T"))) Then
  7.   WScript.Echo "No have rigth argument on the command line." & VBCRLF & _
  8.         "This tool Send E-mail for you recepients"& VBCRLF& _
  9.         "Usage: SendMail.vbs </keys:parameters> "& VBCRLF & _
  10.         "KEYS:" & VBCRLF & _
  11.         ""& VBCRLF& _
  12.         "NECESSARY KEYS"& VBCRLF& _
  13.         ""& VBCRLF& _
  14.         "/F:<e-mail> e-mail adress in field FROM"& VBCRLF& _
  15.         "/T:<e-mail> e-mail adress in field TO (May be many adress semicolon separated)"& VBCRLF& _
  16.         ""& VBCRLF& _
  17.         "OPTIONAL KEYS"& VBCRLF& _
  18.         ""& VBCRLF& _
  19.         "/C:<e-mail> e-mail adress in field BCC (BLIND CURRENT COPY)"& VBCRLF& _
  20.         "/S:<text> Text in field SUBJECT"& VBCRLF& _
  21.         "/B:<text> Text in BODY letter"& VBCRLF& _
  22.         "/A:<Drive\path> Link to file which will in ATTACHMENT"& VBCRLF& _
  23.         ""& VBCRLF& _
  24.         "Example: " & VbCrLf & "SendMail.vbs /F:user@host.com /T:user2@host.com /S:""Test Subject"" /B:""Hey guys! This is my letters!"" /A:C:\test.txt"
  25.   WScript.Quit
  26. End If
  27.  
  28. If objNamedArgs.Exists("F") Then
  29.   MailFrom = objNamedArgs("F")
  30.  End If
  31.  
  32. If objNamedArgs.Exists("T") Then
  33.  MailTo = objNamedArgs("T")
  34.  End If
  35.  
  36. If objNamedArgs.Exists("C") Then
  37.  MailBCC = objNamedArgs("C")
  38.    End If
  39.  
  40. If objNamedArgs.Exists("S") Then
  41.  MailSubject = objNamedArgs("S")
  42.  End If
  43.  
  44. If objNamedArgs.Exists("B") Then
  45.  MailBody = objNamedArgs("B")
  46.  End If
  47.  
  48. If objNamedArgs.Exists("A") Then
  49.   MailAttachment = objNamedArgs("A")
  50. End If
  51.  
  52. ' FUNCTION ============
  53. Function GetString(sString)
  54. ' Get one string from nulti-string
  55. '
  56. Dim TempString
  57.     GetPrefixNameGroup = sString
  58.     if( InStr(sString, "_") >= 1 ) then
  59.          TempString = Left(sString, InStr(sString, "_")-1)
  60.          GetString = TempString
  61.      end If
  62. End Function
  63. '////////////////////////////////////////////////////////////////////////////
  64.  
  65. ' FUNCTION ============
  66. Function TrimString(sString)
  67. ' Trim prefix of name group
  68. '
  69. Dim TempString
  70.     if( InStr(sString, "_") > 1 ) then
  71.          TempString = Mid(sString, InStr(sString, "_")+1)
  72.          TrimString = TempString
  73.      end If
  74. End Function
  75. '////////////////////////////////////////////////////////////////////////////
  76.  
  77.          tempStr = GetString ( MailBody)
  78.          EndLine = Tempstr & VbCrLf
  79.          
  80.          tempStr = TrimString( MailBody)
  81.          PArt1  = GetString(tempStr)
  82.          EndLine = Endline & Part1 & VbCrLf
  83.          
  84.          tempStr = TrimString(tempStr)       
  85.          Part2  = GetString(tempStr)
  86.          EndLine = Endline & part2 & VbCrLf
  87.    
  88.          tempStr = TrimString(tempStr)       
  89.          part3  = GetString(tempStr)
  90.          EndLine = Endline & part3 & VbCrLf
  91.    
  92.          tempStr = TrimString(tempStr)       
  93.         part4  = GetString(tempStr)
  94.          EndLine = Endline & part4 & VbCrLf
  95.    
  96.          tempStr = TrimString(tempStr)       
  97.         part5  = GetString(tempStr)
  98.          EndLine = Endline & part5 & VbCrLf
  99.  
  100. Set objEmail = CreateObject("CDO.Message")
  101.     objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  102.     '(значение 1 - Send the message using Pickup, например при использовании локального почтового сервера).
  103.     '(значение 2 - Send the message using the network, отсылка через сетевое подключение).
  104.     '(значение 3 - Send the message using the Microsoft Exchange mail submission Uniform Resource Identifier (URI)).
  105.    
  106.     'Почтовый сервер
  107.     objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.yandex.ru"
  108.    
  109.     'Настройка режима аутентификации
  110.     objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  111.     'Значение 1 - означает, что используется базовая аутентификация (Use the basic (clear text) authentication mechanism.).
  112.     'Значение 0 -  без аутентификации (анонимно) (Perform no authentication.).
  113.     'Значение 2 –  аутентификация NTLM (Use the NTLM authentication mechanism.).
  114.     objEmail.Configuration.Fields.Update
  115.    
  116.     'Имя пользователя и пароль
  117.     objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user@host.com"
  118.     objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
  119.    
  120.     'Иногда необходимо также определить использование специфического порта (отличного от 25), и время тайм-аута:
  121.     'objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  122.     'objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 20
  123.    
  124.     'Задаем будет или нет использоваться SSL
  125.     'objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
  126.    
  127.     'После изменений, вносимых в конфигурацию, изменения надо сохранить:
  128.     objEmail.Configuration.Fields.Update
  129.    
  130.     objEmail.From = MailFrom
  131.     objEmail.To = MailTo
  132.     objEmail.BCC = MailBCC
  133.     objEmail.Subject = MailSubject
  134.     objEmail.TextBody = Endline
  135.     objEmail.Send
  136. WScript.Quit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement