Jump to content

Sending Email


Recommended Posts

Hi,

I had copy  " Email sending" code   and trying  to execute it    ! I didn't get any error But it failed to send the mail ! Please help me out ...

 

Here is the code :-


#Include<file.au3>


$SmtpServer = "smtp.gmail.com"                                 ; address for the smtp-server to use - REQUIRED
$FromName = "Balmukundam"                                     ; name from who the email was sent
$FromAddress = "bmukundamsw@gmail.com"          ; address from where the mail should come
$ToAddress = "gsharmasw@gmail.comm"                   ; destination address of the email - REQUIRED
$Subject = "test"                                                                ; subject from the email - can be anything you want it to be
$Body = "this is body"                                                      ; 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 = "Normal"                                                ; Send message priority: "High", "Normal", "Low"
$Username = "bmukundamsw@gmail.com"                                     ; 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 = 465                                                                  ; 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, $s_Username , $s_Password , $IPPort , $ssl )
    Local $objEmail = ObjCreate("CDO.Message")
    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    MsgBox(0,"near","0 ")
    Local $i_Error_desciption = ""
    MsgBox(0,"near","1 ")
    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
     MsgBox(0,"near","2")
    $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
     MsgBox(0,"near","3")
    If $ssl Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    ;Update settings
    $objEmail.Configuration.Fields.Update
    MsgBox(0,"near","4")
    ; 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
    MsgBox(0,"near","5")
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail=""
    MsgBox(0,"near","6")
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

 

 

 

 

 

internet.au3

Edited by Balmukundam
Link to comment
Share on other sites

Contact your local postal service office and ask them why they have not delivered your mail to its destination.

All joking aside, we are going to need many more details to be able to help.  post your code.

You should also never edit your first post once someone has replied, instead you should add new comments to the thread.

the code you posted shows you are using the Gmail smtp but you have the ports for gmail commented out.

Edited by alienclone
If @error Then
    MsgBox(262192, "", @ComputerName & " slaps " & @UserName & " around a bit with a large trout!")
EndIf

"Yeah yeah yeah patience, how long will that take?"  -Ed Gruberman

REAL search results  |  SciTE4AutoIt3 Editor Full Version

Link to comment
Share on other sites

$objEmail.Send
    MsgBox(0,"near","5")
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail=""

MsgBox i executed succesfully so the @error =0, disabling this MsgBox will enable showing MsgBox with error.

btw.: i hope the accountdata used in script isn't real. But if so change Password in script and your EMailaccount. Deleting email-adresses makes sence too.

 

Edited by AutoBert
Link to comment
Share on other sites

1 hour ago, alienclone said:

Contact your local postal service office and ask them why they have not delivered your mail to its destination.

All joking aside, we are going to need many more details to be able to help.  post your code.

You should also never edit your first post once someone has replied, instead you should add new comments to the thread.

the code you posted shows you are using the Gmail smtp but you have the ports for gmail commented out.

All right I will take care of your words 

Link to comment
Share on other sites

  • Developers
2 hours ago, Balmukundam said:

$IPPort = 465                                                                  ; port used for sending the mail
$ssl = 0                                           ; enables/disables secure socket layer sending - put to 1 if using httpS

Why is $ssl set to 0? Doubt that will ever work with Gmail.
Also remove the " MsgBox(0,"near","5")" line as that wil invalidate the test for an successful send.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

So what have you set it to now? 
Are you trying this from a Corporate / schiool or private environment?  in other words: Are you sure you are allowed to connect via port 465 to the internet?

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hi, 

 I want to send mail to multiple Person and  each mail  will having  unique file to be attached  ? I get error In  passing array value to function Please help me out...

 

 

 


#Include<file.au3>
Dim $i=0

; Variables
$SmtpServer = "smtp.gmail.com"
$FromName = "Balmukundam"
$FromAddress = "bmukundamsw@gmail.com"
dim $ToAddress[2]
$ToAddress[0]= "balmukundam2012@gmail.com"
$ToAddress[1]= "sagnihotrisw@gmail.com"

$Subject = "test"
$Body = "this is body"
dim $AttachFiles[2]
$AttachFiles[0]= "C:\Users\Admin\Desktop\ram"
$AttachFiles[1]= "C:\Users\Admin\Desktop\ram"

$CcAddress = ""
$BccAddress=""
$Importance = "Normal"
$Username = "bmukundamsw@gmail.com"
$Password="######"
$IPPort = 465
$ssl = 1
;~ $IPPort=465
;~ $ssl=1


; Script

Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")


;For $i = 0 To 1 Step +1
While $i <= 1

$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)

$i = $i + 1
WEnd

;Next


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, $s_Username , $s_Password , $IPPort , $ssl )
    Local $objEmail = ObjCreate("CDO.Message")
    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'

    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    MsgBox(0,"near","0 ")
    Local $i_Error_desciption = ""
    MsgBox(0,"near","1 ")
    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
     MsgBox(0,"near","2")
    $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
     MsgBox(0,"near","3")
    If $ssl Then
        $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    ;Update settings
    $objEmail.Configuration.Fields.Update
    MsgBox(0,"near","4")
    ; 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

Edited by Balmukundam
Link to comment
Share on other sites

  • Developers

Believe the emal addresses should be comma delimited:

$ToAddress = "balmukundam2012@gmail.com,sagnihotrisw@gmail.com"

 

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...