¿Cómo puedo enviar un correo electrónico de texto sin formato (con saltos de línea) usando sp_send_dbmail?

StackOverflow https://stackoverflow.com/questions/3514650

Pregunta

Tengo un procedimiento de SQL Server 2008 que envía correo electrónico a través sp_send_dbmail.

Estoy usando el siguiente código:

  set @bodyText = ( select 
                      N'Here is one line of text ' +
                      N'It would be nice to have this on a 2nd line ' +
                      N'Below is some data: ' +
                      N' ' +
                      N' ' +
                      field1 +
                      N' ' +
                      field2 +
                      N' ' +
                      N'This is the last line'
                    from myTable )

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'myProfile',
        @recipients = @to,
        @body = @bodyText,
        @body_format = 'TEXT',
        @subject = 'Testing Email' ;

Mi miPerfil está configurado para utilizar el servidor SMTP local, lo que resulta en un archivo .EML en c: \ inetpub \ mailroot \ cola

Cuando uno abierto de esos archivos .eml (UG - la única cosa que puede abrirlos es Outlook Express, mirándolos en cualquier otra cosa simplemente muestra el cuerpo como un blob codificado en base64.) Parece que está haciendo que el resultado como HTML - por lo que no estoy seguro de si el problema está en el cliente, o

He intentado poner \ n en el mensaje, pero eso no funcionó. ¿Cómo puedo enviar texto plano con saltos de línea, y verificar que las miradas resultado final corrigen?

Por cierto, no puedo realmente enviar el correo electrónico a probarlo con clientes de correo electrónico reales - corp. la red está bloqueado.

¿Fue útil?

Solución

Usted está en realidad no insertando saltos de línea. Se les puede incorporar directamente en un literal de cadena en SQL Server como a continuación.

SET @bodyText = (SELECT N'Here is one line of text 
It would be nice to have this on a 2nd line 
Below is some data: 


' + field1 + N' 

' + field2 + N' 

' + N'This is the last line'
                 FROM   myTable);

O un enfoque más ordenado podría ser

DECLARE @Template NVARCHAR(max) = 
N'Here is one line of text 
It would be nice to have this on a 2nd line 
Below is some data: 

##field1##

##field2##

This is the last line';

SET @bodyText = (SELECT REPLACE(
                    REPLACE(@Template, 
                       '##field1##', field1), 
                       '##field2##', field2)
                 FROM   myTable); 

Tanto generará un error si myTable contiene más de una fila que está asignando el resultado a una variable escalar.

Otros consejos

Siempre he utilizado CHAR(13)+CHAR(10) para crear saltos de línea (que parece funcionar mezclados con los valores nvarchar) en TSQL, por lo que intentar algo como esto:

DECLARE @CRLF char(2)
       ,@bodyText nvarchar(max)
       ,@field1  nvarchar(10)
       ,@field2  nvarchar(10)
SELECT @CRLF=CHAR(13)+CHAR(10)
      ,@field1='your data'
      ,@field2='and more'

set @bodyText =
                N'Here is one line of text ' 
                +@CRLF+ N'It would be nice to have this on a 2nd line ' 
                +@CRLF+ N'Below is some data: ' + N' ' + N' ' + ISNULL(@field1,'') + N' ' + ISNULL(@field2 + N' ' ,'')
                +@CRLF+ N'This is the last line' 


PRINT @bodyText

Salida:

Here is one line of text 
It would be nice to have this on a 2nd line 
Below is some data:   your data and more 
This is the last line

este CHAR(13)+CHAR(10) trabajará con msdb.dbo.sp_send_dbmail, envío de correos electrónicos con formato utilizando todo el tiempo.

tarde a la fiesta, pero como Fernando68 menciona anteriormente, si se le permite mensajes HTML, ponga @bodyFormat = 'HTML', entonces se puede utilizar para <br/> saltos de línea y que sea tan elegante como usted quiere utilizar todas las etiquetas que se obtienen de HTML como saltos de línea, IMG, fuertes y así sucesivamente ...

  set @bodyText = ( select 
                      '<h1>My Data</h1><p>Here is one line of text<br/>
                       It would be nice to have this on a 2nd line <br/>
                       Below is some data: <br/>'
                       + field1 + '<br/>'
                       + field2 + '<br/>'
                       + 'This is the last line</p>'
                       from myTable )

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'myProfile',
        @recipients = @to,
        @body = @bodyText,
        @body_format = 'HTML',
        @subject = 'Testing Email' ;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top