Monday, February 20, 2012

Problems using xp_smtp_sendmail

I am trying to use this code to send an email

declare @.rc int,
@.invjournal int

set @.invjournal = 2222

exec @.rc = master.dbo.xp_smtp_sendmail
@.FROM= N'matt@.hasta.se',
@.FROM_NAME= N'Matt Douhan',
@.TO= N'inventering@.hasta.se',
@.replyto = N'Reply to NONE',
@.CC= N'',
@.BCC= N'',
@.priority= N'NORMAL',
@.subject= N' TEST TEST TEST Inventeringsjournal '+@.invjournal+'
klar fr integration till redovisningen TEST TEST TEST',
@.message= N'Vnligen uppdatera denna inventeringsjournal till
redovisningen',
@.messagefile= N'',
@.type= N'text/plain',
@.attachment= N'',
@.attachments= N'',
@.codepage= 0,
@.server = N'mandarin.internal.hasta.se'
select RC = @.rc
go

but it fails with the following with the following error msg

Server: Msg 170, Level 15, State 1, Line 14
Line 14: Incorrect syntax near '+'.

the problem seems to be when I want to use the @.invjournal variable in
the subject, if I take it away and only send text it works just fine.

Any ideas would be much appreciated

rgds

Matt"Matt" <matt@.fruitsalad.org> wrote in message
news:b609190f.0404030357.643644d2@.posting.google.c om...
> I am trying to use this code to send an email
> declare @.rc int,
> @.invjournal int
> set @.invjournal = 2222
> exec @.rc = master.dbo.xp_smtp_sendmail
> @.FROM = N'matt@.hasta.se',
> @.FROM_NAME = N'Matt Douhan',
> @.TO = N'inventering@.hasta.se',
> @.replyto = N'Reply to NONE',
> @.CC = N'',
> @.BCC = N'',
> @.priority = N'NORMAL',
> @.subject = N' TEST TEST TEST Inventeringsjournal '+@.invjournal+'
> klar fr integration till redovisningen TEST TEST TEST',
> @.message = N'Vnligen uppdatera denna inventeringsjournal till
> redovisningen',
> @.messagefile = N'',
> @.type = N'text/plain',
> @.attachment = N'',
> @.attachments = N'',
> @.codepage = 0,
> @.server = N'mandarin.internal.hasta.se'
> select RC = @.rc
> go
>
> but it fails with the following with the following error msg
> Server: Msg 170, Level 15, State 1, Line 14
> Line 14: Incorrect syntax near '+'.
> the problem seems to be when I want to use the @.invjournal variable in
> the subject, if I take it away and only send text it works just fine.
> Any ideas would be much appreciated
> rgds
> Matt

You need to construct the entire string first:

declare @.mysubject nvarchar(4000)
set @.mysubject = N' TEST TEST TEST Inventeringsjournal '+@.invjournal+' klar
fr integration till redovisningen TEST TEST TEST'

exec master..xp_smtp_sendmail
...
@.subject = @.mysubject,
...

See the example "Using variables" on the xp_smtp_sendmail homepage.

Simon

No comments:

Post a Comment