Jump to content

Is there a UDF for sending emails via IMAP through exchange?


gte
 Share

Recommended Posts

Here is the function I was using, but it stopped working, it now says:

"Error code:2 Description:The server rejected the sender address. The server response was: 454 5.7.3 Client does not have permission to submit mail to this server."

I figured maybe it was because they blocked the smtp port and directed sent messages via IMAP, maybe not.

Any ideas what could be causing this?

;
;##################################
; Include
;##################################
#Include<file.au3>
;##################################
; Variables
;##################################
$SmtpServer = "[exchangeservername]"              ; address for the smtp-server to use - REQUIRED
$FromName = ""                      ; name from who the email was sent
$FromAddress = "" ; address from where the mail should come
$ToAddress = ""   ; destination address of the email - REQUIRED
$Subject = ""; subject from the email - can be anything you want it to be
$Body = "" & @CR & _
"" & @CR & _ 
""   ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = ""                       ; the file you want to attach- leave blank if not needed
$CcAddress = ""       ; address for cc - leave blank if not needed
$BccAddress = ""     ; address for bcc - leave blank if not needed
$Importance = "High"                  ; Send message priority: "High", "Normal", "Low"
$Username = ""                    ; username for the account used from where the mail gets sent - REQUIRED
$Password = ""                  ; password for the account used from where the mail gets sent - REQUIRED
$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

;##################################
; Script
;##################################
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
If @error Then
    MsgBox(0, "Error sending message", "Error code:" & @error & "  Description:" & $rc)
EndIf
;
; The UDF
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance="Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
    Local $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])
            ConsoleWrite('@@ Debug(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
            If FileExists($S_Files2Attach[$x]) Then
                $objEmail.AddAttachment ($S_Files2Attach[$x])
            Else
                ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
                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
    If Number($IPPort) = 0 then $IPPort = 25
    $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
    ; Set Email Importance
    Switch $s_Importance
        Case "High"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch
    $objEmail.Fields.Update
    ; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail=""
EndFunc   ;==>_INetSmtpMailCom
;
;
; Com Error Handler
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

Here is the code i used the last time to send emails via exchange.

Additionally you didn't tell us anything about the exchange server version. Especially since

Exchange 2k7 the roles are divided between mailbox-server, cas-server (client access) and

hubtransport-server.

Outlook communicates directly with the mailbox-server whereby if you need pop3, imap and

smtp you need to get in communication to the hubtransport server role which may reside on

a different server.

#include <INet.au3>

$s_SmtpServer="mailserver"
$s_FromName="forename lastname"
$s_FromAddress="forename.lastname@domainname.de"
$s_ToAddress="recepient@domainname.de"
$s_Subject="Test-EMAIL"
Dim $as_Body[2]
$as_Body[0]="This is a test"
$as_Body[1]="Testline no. 2"
$s_helo="HELO"
$b_trace=1

$Response = _INetSmtpMail ( $s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress ,$s_Subject ,$as_Body, $s_helo, $b_trace)

$err = @error
If $Response = 1 Then
    MsgBox(0, "Success!", "Mail sent")
Else
    MsgBox(0, "Error!", "Mail failed with error code " & $err)
EndIf
Link to comment
Share on other sites

Thanks, I like this script! You seem to know a good bit about exchange servers ..

I ran the script, it gave an error which says

"Mail failed with error code 50"

We use "MICROSOFT EXCHANGE SERVER 2003, ENTERPRISE EDITION" here. I am using office 2k7, Logon network security is set to "Negotiate Authentication", Encrypt Data is checked.

Here is the code i used the last time to send emails via exchange.

Additionally you didn't tell us anything about the exchange server version. Especially since

Exchange 2k7 the roles are divided between mailbox-server, cas-server (client access) and

hubtransport-server.

Outlook communicates directly with the mailbox-server whereby if you need pop3, imap and

smtp you need to get in communication to the hubtransport server role which may reside on

a different server.

#include <INet.au3>

$s_SmtpServer="mailserver"
$s_FromName="forename lastname"
$s_FromAddress="forename.lastname@domainname.de"
$s_ToAddress="recepient@domainname.de"
$s_Subject="Test-EMAIL"
Dim $as_Body[2]
$as_Body[0]="This is a test"
$as_Body[1]="Testline no. 2"
$s_helo="HELO"
$b_trace=1

$Response = _INetSmtpMail ( $s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress ,$s_Subject ,$as_Body, $s_helo, $b_trace)

$err = @error
If $Response = 1 Then
    MsgBox(0, "Success!", "Mail sent")
Else
    MsgBox(0, "Error!", "Mail failed with error code " & $err)
EndIf

Link to comment
Share on other sites

Hi,

sorry, typing error. Please use

$b_trace=-1

and it will work!

No thanks to me please. I also found this script anywhere in this forum a long time ago!

There are a lot of entries when you search for smtp and exchange.

Link to comment
Share on other sites

I thank you for reading and replying

It's telling me "Mail failed with error code 51" now?

Hi,

sorry, typing error. Please use

$b_trace=-1

and it will work!

No thanks to me please. I also found this script anywhere in this forum a long time ago!

There are a lot of entries when you search for smtp and exchange.

Link to comment
Share on other sites

OK,

now, before we going to keep up looking at the script let's check if the SMTP-Connector at the Exchange-Server you are trying to connect to is working the way we want. You have 2 possibilities: Either you check and send a test mail by commandline using telnet or you can use a small graphical tool.

This link is for testing with telnet: Telnet to Port 25 to Test SMTP Communication

This link is for a small graphical tool named SMTPExplorer. It's a german tool but very easy to handle, cause all SMTP-Commands are english. You only need to know that "Verbinden" means connect and "Trennen" means disconnect ;)

Download here: SMTP Explorer

It's important that you use exactly the same names & email adresses like in your script.

Link to comment
Share on other sites

Local $olMailItem     = 0
    Local $olFormatRichText = 3
    Local $olImportanceLow     = 0
    Local $olImportanceNormal= 1
    Local $olImportanceHigh     = 2
Local $olByValue = 1


$oOApp = ObjCreate("Outlook.Application")
    $oOMail = $oOApp.CreateItem($olMailItem)
    ;$oOMail.Save
    With $oOMail
    .To = ("mailadres@yahoo.com")
        .Subject = "some subject"
        .BodyFormat =  $olFormatRichText
        .Importance = $olImportanceNormal
    .Body = "the body text"
      .Display
      ;.Send
    EndWith

Try this code.If you have a working outlook account it will work.

Edited by Juvigy
Link to comment
Share on other sites

There was a great UDF that I was using with POP3 to send unattended emails, is there a similar UDF for sending unattended emails via IMAP and an exchange server?

Have a look at the Outlook code posted by 'Wooltown' in this Forum. It pretty much has everything you ever wanted to know about CreateObject(Outlook.Application) which does not use CDO Ant..

Edited by anixon
Link to comment
Share on other sites

Juvigy,

This worked in creating the email

The problem is this, I either can't send emails, or can't send key strokes when my machine is locked ... any ideas?

Local $olMailItem     = 0
    Local $olFormatRichText = 3
    Local $olImportanceLow     = 0
    Local $olImportanceNormal= 1
    Local $olImportanceHigh     = 2
Local $olByValue = 1


$oOApp = ObjCreate("Outlook.Application")
    $oOMail = $oOApp.CreateItem($olMailItem)
    ;$oOMail.Save
    With $oOMail
    .To = ("mailadres@yahoo.com")
        .Subject = "some subject"
        .BodyFormat =  $olFormatRichText
        .Importance = $olImportanceNormal
    .Body = "the body text"
      .Display
      ;.Send
    EndWith

Try this code.If you have a working outlook account it will work.

Link to comment
Share on other sites

You dont need to send !S or ControlClick to send the email.

Just remove

.Display
and uncomment
.Send
in the code

@Edit

Like this:

Local $olMailItem     = 0
    Local $olFormatRichText = 3
    Local $olImportanceLow     = 0
    Local $olImportanceNormal= 1
    Local $olImportanceHigh     = 2
Local $olByValue = 1


$oOApp = ObjCreate("Outlook.Application")
    $oOMail = $oOApp.CreateItem($olMailItem)
    ;$oOMail.Save
    With $oOMail
    .To = ("mailadres@yahoo.com")
        .Subject = "some subject"
        .BodyFormat =  $olFormatRichText
        .Importance = $olImportanceNormal
    .Body = "the body text"
    .Send
    EndWith
Edited by Juvigy
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...