Ask a Question

SendMail not working. Tried everything I found in documentation and forum. Need help.

royd
Regular Contributor

SendMail not working. Tried everything I found in documentation and forum. Need help.

In gmail, 2-step verification is turned off.

Account access is enabled.

Less secure app access is turned on.

There is a log file here: C:\\TestLog\\Log.mht

 

 The script I am using:

function Test()
{
  SendEmail("xxxxn@gmail.com", "xxxx@xxxxxxxxx.com", "Test", "TestMessage", "C:\\TestLog\\Log.mht");
}
 
function SendEmail(mFrom,  mTo,  mSubject,  mBody,  mAttachment)
{
  var smtpServer = "smtp.gmail.com" //"smtp.gmail.com";
  var smtpPort = 587; //tried 587, 465, 26 & 25 as well
  var userLogin = "xxxx"; // e.g. "abc" if the address is abc@gmail.com
  var userPassword = "PASSWORD";
  var autentificationType = 1; // cdoBasic
  var connectionTimeout = 30;
  var useSSL = true;// Required by Gmail
  
 
  try {
    var schema = "http://schemas.microsoft.com/cdo/configuration/";
    var mConfig = Sys.OleObject("CDO.Configuration");
    mConfig.Fields.Item(schema + "sendusing") = 2; // cdoSendUsingPort
    mConfig.Fields.Item(schema + "smtpserver") = smtpServer;
    mConfig.Fields.Item(schema + "smtpserverport") = smtpPort;
    mConfig.Fields.Item(schema + "sendusername") = userLogin;
    mConfig.Fields.Item(schema + "sendpassword") = userPassword;
    mConfig.Fields.Item(schema + "smtpauthenticate") = autentificationType;
    mConfig.Fields.Item(schema + "smtpconnectiontimeout") = connectionTimeout;
    mConfig.Fields.Item(schema + "smtpusessl") = useSSL;
    mConfig.Fields.Update();
    
    var mMessage = Sys.OleObject("CDO.Message");
    mMessage.Configuration = mConfig;
    mMessage.From = mFrom;
    mMessage.To = mTo;
    mMessage.Subject = mSubject;
    mMessage.HTMLBody = mBody;
    
    if(0 < mAttachment.length) {
      mMessage.AddAttachment(mAttachment);
    }
    
    mMessage.Send();
  }
  catch(exception) {
    Log.Error("E-mail cannot be sent", exception.description);
    return false;  
  }
  Log.Message("Message to <" + mTo +  "> was successfully sent");
  return true;
}

Log reports: 'E-mail cannot be sent', Additional Info panel is blank.

 

Help is much appreciated.

 

Dave

13 REPLIES 13
tristaanogre
Esteemed Contributor

What happens if you put a breakpoint in the code and step through?  At what point does the exception get thrown?


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
royd
Regular Contributor

Hi Robert,

 

Ran the test as suggested - 

 

It steps through till -

mConfig.Fields.Item(schema + "sendusing") = 2; 

Then jumps to:

Log.Error("E-mail cannot be sent", exception.description);

Thanks.

tristaanogre
Esteemed Contributor

OK... did a quick experiement...

So, everywhere you have something of the pattern of 

 

mConfig.Fields.Item(schema + "sendusing")

 

add .Value at the end.  So, you should end up with

 

mConfig.Fields.Item(schema + "sendusing").Value = 2

 

and so on. 


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
royd
Regular Contributor

Okay, I changed them to:

mConfig.Fields.Item(schema + "sendusing").Value = 2; // cdoSendUsingPort
    mConfig.Fields.Item(schema + "smtpserver").Value = smtpServer;
    mConfig.Fields.Item(schema + "smtpserverport".Value) = smtpPort;
    mConfig.Fields.Item(schema + "sendusername").Value = userLogin;
    mConfig.Fields.Item(schema + "sendpassword").Value = userPassword;
    mConfig.Fields.Item(schema + "smtpauthenticate").Value = autentificationType;
    mConfig.Fields.Item(schema + "smtpconnectiontimeout").Value = connectionTimeout;
    mConfig.Fields.Item(schema + "smtpusessl").Value = useSSL;
    mConfig.Fields.Update();

Still getting the same error! 😞

tristaanogre
Esteemed Contributor

Is the exception still popping at the same code line?


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
royd
Regular Contributor

I think we are making some proress!

 

It is going through every line till -

mMessage.Send();

... and then the exception takes place at the above line? I guess for some reason it can't send the message!!

tristaanogre
Esteemed Contributor

Change the Log.Error to change the word "description" to "message".  Let's see if we can find out what the actual exception is.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
royd
Regular Contributor

Okay, now we got (Additional info):

 

"The transport failed to connect to the server."

tristaanogre
Esteemed Contributor

Change the smtpPort back to 465 and try again


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
cancel
Showing results for 
Search instead for 
Did you mean: