Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to send the mail using script in servicenow in scheduled job without events

Dhanyasri
Tera Contributor

I have used the following script to send the mail, but it is not working.

 

 
var email = new GlideEmailOutbound();
email.setSubject('Test Email from ServiceNow');
email.setBody('This is a test email sent from ServiceNow.');
email.addTo('abel.tuter@example.com');
email.send();
var a = email.mail();
if (a) {
    gs.info('Email sent successfully.');
} else {
    gs.error('Failed to send email.');
}
 
 
Result is "Failed to send email."
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Dhanyasri ,

Please refer below code 

var mail = new GlideEmailOutbound();
mail.setSubject('Outbound Email Send');
mail.setBody('Hello, Email send successfully');
mail.addRecipient('adela.cervantsz@example.com');
mail.save();

 

It got updated in email log table as well

SarthakKashya2_0-1712764657619.png

 

Please mark my answer correct and helpful if it works for you 


Thanks and Regards 

Sarthak

View solution in original post

8 REPLIES 8

Amit Gujarathi
Giga Sage
Giga Sage

HI @Dhanyasri ,
I trust you are doing great.
please find the updated code for the same :

try {
    var email = new GlideEmailOutbound();
    email.setSubject('Test Email from ServiceNow');
    email.setBody('This is a test email sent from ServiceNow.');
    email.addTo('abel.tuter@example.com');
    email.send();
    gs.info('Email sent successfully.');
} catch (ex) {
    gs.error('Failed to send email: ' + ex);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi @Amit Gujarathi ,

 

I have tried it, but email is not triggered

You need to make sure that your script is under Global 

Hi @Amit Gujarathi ,

 

email.save() is creating an entry to the system mailboxes -> outbounds table, but it is not triggering any email. if I do email.send() also I am not receiving any email in my mailbox. 

Am I doing anything wrong?

 

var mail = new GlideEmailOutbound();

mail.setReplyTo('aismita.dey@servicenow.com');

mail.setSubject('Testing outbound email');

mail.addAddress('cc', 'aismita.dey@servicenow.com');

mail.setBody('Hello world!');

mail.send();