creating smtp server in mail with applescript

I have been trying to make a smtp server in mail and having no luck with adding a password to it here is my settings

tell application "Mail"
	set newsmtpac to make new smtp server with properties {server name:"smtp.example.com", port:587, uses ssl:true}
	tell newsmtpac
		set authentication to password
		set user name to "email@example.com"
		set password to "password"
	end tell
end tell

and it returns

i can’t find out what is happing thanks for any help.

Mr. Gecko

This rang a bell for me. I got it to work by either moving set username to . to the end of tell block (more importantly, making it the last action dealing with the new smtp server object), or by changing the code to this:

tell application "Mail"
	set servername to "smtp.example.com"
	set username to "email@example.com"
	set newsmtpac to make new smtp server with properties {server name:servername, port:587, uses ssl:true}
	tell newsmtpac
		set authentication to password
		set user name to username
	end tell
	set newsmtpac to smtp server (servername & ":" & username)
	tell newsmtpac
		set password to "password"
	end tell
end tell

The problem appears to be that the smtp server object returned by Mail changes its “key” when its user name property is set or changed. To continue to access the object inside Mail after a new user name has been set, you have to adapt the key that AppleScript uses to reference it. According to my testing, if there is no user name, the key is just the server name. When there is a user name, the key is the concatenation of the server name , a colon, and the user name. In the context of your original script, you can simply avoid having to use the new key by making the action that changes the key be the last thing done. But practically, if this is part of a larger script, you will have to the take the other route because you will probably want to attach this new smtp server object to an account object of some sort (and to do that, you will have to use the new key; this was the problem in my previous experience with Mail’s smtp server objects).

There appear to be a couple of bugs in Mail’s AppleScript support: 1) The authentication, user name and password properties of smtp server objects created with make new smtp server . are not handled properly. 2) smtp server objects lack a unique, static identifier. In many cases, using the server name and user name should result in a solid “primary key” (in database parlance), but it is an unfortunate choice for scripts that want to update these properties.

Code tested with Mail Version 2.1 (752/752.2).

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)

Thanks for your help