How to catch exceptions raised by mail application via applescript

run the following script,

tell application "Mail"
	set newMessage to make new outgoing message with properties {subject:"", content:"" & return & return}
	
	tell newMessage
		set sender to ""
		make new to recipient at end with properties {address:""}
		make new cc recipient at end with properties {address:""}
		make new bcc recipient at end with properties {address:""}
		
		send
	end tell
	
end tell

an exception is raised in Mail Application saying “This message couldn’t be delivered because the following internal error occurred: This message couldn’t be delivered because you haven’t specified any recipients.”
is it possible to catch this exception using apple script if so how?

Hi,

I’m using an old version of mail, but somethings are the same.

Firstly, you should set visible of the outgoing message to true so you can see if your message is turning out right.

tell application “Mail”
activate
set newMessage to make new outgoing message with properties {subject:“test”, content:“”, visible:true}
end tell

Next, I think the sender and the recipients shouldn’t be blank. Lastly, I think you need to use ‘at end of to recipients’ and not just ‘at end’.

tell application “Mail”
activate
set newMessage to make new outgoing message with properties {subject:“test”, content:“”, visible:true}

tell newMessage
	set sender to "sender@somewhere.com"
	make new to recipient at end of to recipients with properties {address:"recipient@somewhere.com"}
	--make new cc recipient at end of cc recipients with properties {address:""}
	--make new bcc recipient at end of bcc recipients with properties {address:""}
	
	--send
end tell

end tell

Try using some of this.

gl,

Hi kel,
Thanks for the reply…

If we specify sender and reciepient its woking fine…but my requirement is if we run the script without passing them, then how to catch those exceptions??
main intension is how to catch exceptions inthose situations?

Here’s a workaround. I had to delete my last post because I forgot to change the address.

tell application “Mail”
activate
set newMessage to make new outgoing message with properties ¬
{subject:“test”, content:“hello”, visible:true}
tell newMessage
set sender to “kelvy@somewhere.net
make new to recipient at end of to recipients with properties {address:“somebody@somewhere.com”}
–make new cc recipient at end of cc recipients with properties {address:“”}
–make new bcc recipient at end of bcc recipients with properties {address:“”}
try
send
on error – dummy statement
end try
end tell
set out_list to (every message of out mailbox)
if out_list is not {} then display dialog “there was an error sending”
end tell

In my version, when mail can’t be sent it is placed in the out mailbox. You might want to compare a the out mailbox before and after you send. Note that the parameter label for out mailbox is probably different in your version.

Hope it works for you,

Hi Kel,

Once again thanks,

It works fine for the wrong address i.e, in case when message remains in outbox, but the mesage “there was an error sending” is also coming even the mail is delivered, as every mail has to go via outbox.

One more thing here we are raising our own error message, instead of that is there any way to catch the exact message thrown by the mail application.

Thanks in advance…

I see what you mean. I didn’t think it was going through the out mailbox when it was sent.

Back to the drawing board.

Usually you can trap an error with an error handler:

try
– some statements
on error errMsg
display dialog errMsg
– do womething else
end try

When an error occurs, the value of errMsg will be the text of the error message. This doesn’t work in my version of Mail. So I see no way of getting the error message. It might not even be an error, but just a dialog.

About the script, instead of using the out mailbox, I tried the ‘sent mailbox’. Again your version of Mail may have a different name for this property.

tell application “Mail”
activate
set old_count to (count (every message of sent mailbox))
set newMessage to make new outgoing message with properties ¬
{subject:“test”, content:“hello”, visible:true}
tell newMessage
set sender to “kelvy@somewhere.net
make new to recipient at end of to recipients with properties ¬
{address:“kelvy@somewhere.net”}
try
send
on error
– dummy
end try
end tell
set new_count to (count (every message of sent mailbox))
end tell
if new_count = old_count then – message wasn’t added to sent mailbox
display dialog “error sending”
end if
beep 3

gl,