Using a local temporary table generates this error. You can use temporary tables in DBMail, but they need to be global and not local tables, i.e. ##tmpa rather than #tmpa.