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

7 REPLIES 7

Maddysunil
Kilo Sage

@Dhanyasri 

Try with below approach, I tried in background script it is working

 

var mail = new GlideEmailOutbound();
mail.setReplyTo('agent@abc.com');
mail.setSubject('Testing outbound email');
mail.addAddress('cc', 'admin@example.com');
mail.setBody('Hello world!');
mail.addRecipient('abel.tuter@example.com');
mail.save();

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

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

Hi @Community Alums ,

 

I have tried it and worked. Can we send it by attaching the report which was generated using script as well. Could you please refer below attached code and suggest me.

 

Thankyou