hrz999 Posted August 27, 2008 Share Posted August 27, 2008 Hi, first of all thanks for the reall great work done here. I am trying to make a script that check the emails in a Exchange MAPI account, and if the subject of the email is what I am searching for, it automatically generate a new messagge, to be sent via smtp (another server, with another account) at my mailbox. The message must have in attachment the original message found in the Exchange - MAPI account. At the moment I can read the emails in the Exchange account via MAPI com object. I can send the email via the _INETSmtpMailCom. But I can't figure how to save the original message in .eml format to be attached in the email to be sent. Followng the code I am trying now. Any Idea is really appreciated. Thanks. #cs ################################## AUTOIT V3 SCRIPT MAPI MAIL CHECK ################################## #ce #cs ################################## INCLUDES ################################## #ce #Include<file.au3> #include<Array.au3> #cs ################################## VARIABLES/SETTINGS ################################## #ce $s_SmtpServer = "XXXXXXXXX" ; address for the smtp-server to use - REQUIRED $s_FromName = "XXXXXXXX" ; name from who the email was sent $s_FromAddress = "XXXXXXXXXXXX" ; address from where the mail should come $s_ToAddress = "XXXXXXXX" ; destination address of the email - REQUIRED $s_Subject = "XXXXXXXXXXXXXX" ; subject from the email - can be anything you want it to be $as_Body = "XXXXXXXXX" ; the messagebody from the mail - can be left blank but then you get a blank mail $s_AttachFiles = "XXXXXXXXX" ; the file you want to attach- leave blank if not needed $s_CcAddress = "" ; address for cc - leave blank if not needed $s_BccAddress = "" ; address for bcc - leave blank if not needed $s_Username = "XXXXXXXXX" ; username for the account used from where the mail gets sent - Optional (Needed for eg GMail) $s_Password = "XXXXXXXXX" ; password for the account used from where the mail gets sent - Optional (Needed for eg GMail) $IPPort = 25 ; port used for sending the mail $ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS ;~ $IPPort=465 ; GMAIL port used for sending the mail ;~ $ssl=1 ; GMAILenables/disables secure socket layer sending - put to 1 if using https $objOutlook = ObjCreate("Outlook.Application") ; creo l'oggetto $objOutlook $objNameSpace = $objOutlook.GetNamespace("MAPI") ; setto il namespace MAPI $objNamespace.Logon("XXXXXXXXX", "XXXXXXXXX", FALSE, TRUE) ; mi loggo $objFolder = $objNamespace.GetDefaultFolder(6) ; setto la cartella da verificare che è posta in arrivo $numItems = $objFolder.Items.Count ; conto quante email ci sono $p = 1 ; $p è la variabile che indica l'ID della email che vado a verificare $i = 0 ; azzero il contatore #cs ################################## SCRIPT ################################## #ce For $i = 1 To $numItems ; loop per la verifica delle email $objMessage = $objFolder.Items($p) ; numero del messaggio che sarà quasi sempre 1 perchè in cima alla lista $s_Subject = $objMessage.Subject() ; Oggetto del messagio $as_Body = $objMessage.Body() ; If $s_Subject <> "prova" Then ; se l'oggetto è diverso da prova $p = $p + 1 ; aumento il valore di $p in modo da andare alla riga successiva nel prossimo loop Else ; altrimenti $file = FileOpen($s_AttachFiles, 2 + 16 ) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf FileWrite($file, $as_Body) FileClose($file) Global $oMyRet[2] Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") $rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl) If @error Then MsgBox(0, "Error sending message", "Error code:" & @error & " Rc:" & $rc) EndIf $objMessage.Delete ; cancello il messaggio dalla posta in arrivo ; FileDelete($file) EndIf Next #cs ################################## PRIVATE FUNCTIONS ################################## #ce Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = "", $s_Password = "",$IPPort=25, $ssl=0) $objEmail = ObjCreate("CDO.Message") $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>' $objEmail.To = $s_ToAddress Local $i_Error = 0 Local $i_Error_desciption = "" If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress $objEmail.Subject = $s_Subject If StringInStr($as_Body,"<") and StringInStr($as_Body,">") Then $objEmail.HTMLBody = $as_Body Else $objEmail.Textbody = $as_Body & @CRLF EndIf If $s_AttachFiles <> "" Then Local $S_Files2Attach = StringSplit($s_AttachFiles, ";") For $x = 1 To $S_Files2Attach[0] $S_Files2Attach[$x] = _PathFull ($S_Files2Attach[$x]) If FileExists($S_Files2Attach[$x]) Then $objEmail.AddAttachment ($S_Files2Attach[$x]) Else $i_Error_desciption = $i_Error_desciption & @lf & 'File not found to attach: ' & $S_Files2Attach[$x] SetError(1) return 0 EndIf Next EndIf $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort ;Authenticated SMTP If $s_Username <> "" Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password EndIf If $Ssl Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True EndIf ;Update settings $objEmail.Configuration.Fields.Update ; Sent the Message $objEmail.Send if @error then SetError(2) return $oMyRet[1] EndIf EndFunc ;==>_INetSmtpMailCom Func MyErrFunc() $HexNumber = Hex($oMyError.number, 8) $oMyRet[0] = $HexNumber $oMyRet[1] = StringStripWS($oMyError.description,3) ConsoleWrite("### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet[1] & @LF) SetError(1); something to check for when this function returns Return EndFunc ;==>MyErrFunc Link to comment Share on other sites More sharing options...
Baraoic Posted August 29, 2008 Share Posted August 29, 2008 Sorry hrz999 I have no idea how to do that, but you said you can get the script to read the emails? well I looked at your script and i understand the part about sending the email but i am confused as to how you are able to read the mail. I would like to beable to have a script open exchange mailbox have it search for an email then read the body and copy part of the body to an array. Would you know how to do that? Sorry cant help you with your problem, but thanks. Link to comment Share on other sites More sharing options...
hrz999 Posted August 30, 2008 Author Share Posted August 30, 2008 Sorry hrz999 I have no idea how to do that, but you said you can get the script to read the emails? well I looked at your script and i understand the part about sending the email but i am confused as to how you are able to read the mail. I would like to beable to have a script open exchange mailbox have it search for an email then read the body and copy part of the body to an array. Would you know how to do that? Sorry cant help you with your problem, but thanks.$objMessage.Body() is the body of the email in the mapi sessionhope this help Link to comment Share on other sites More sharing options...
hrz999 Posted August 30, 2008 Author Share Posted August 30, 2008 (edited) I'll try to be, a bit shorter and clear in the description of what I need to complete this script. I set a variable as the body of an email that I can read from a Mapi session. Now I need to save this variable as a file. Any idea or support is realy appreciated. Bye. Edited August 30, 2008 by hrz999 Link to comment Share on other sites More sharing options...
hrz999 Posted August 30, 2008 Author Share Posted August 30, 2008 Hmmm, there is a different way that I can use but even here I need some help. Instead of save the body of the message from a variable, I am trying to use the SaveAs Method. So the script can save the entire email in the .msg format. But I am now stuck at the conversion of the vba code below : MyItem.SaveAs "C:\statusrep.oft", OlSaveAsType.olTemplate that I converted in $MyItem.SaveAs("C:\email.msg", olMSG) But it obviously wont work :/ Some can Help ? thanks Link to comment Share on other sites More sharing options...
hrz999 Posted August 30, 2008 Author Share Posted August 30, 2008 COOL, now works like a charm PS. the last issue was the second parameter in the method Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now