كيف يمكنني إرسال بريد إلكتروني نصي عادي (مع فواصل الأسطر) باستخدام sp_send_dbmail؟

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

سؤال

لدي إجراء SQL Server 2008 يرسل البريد الإلكتروني عبر sp_send_dbmail.

أنا أستخدم الكود التالي:

  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' ;

تم تعيين myProfile الخاص بي لاستخدام خادم SMTP المحلي ، والذي ينتج عنه ملف .eml في C: inetpub mailroot Queue

عندما أفتح واحدة من ملفات .eml (UG - الشيء الوحيد الذي يمكنه فتحها هو Outlook Express ، والنظر إليها في أي شيء آخر يظهر فقط الجسم باعتباره نقطة مشفرة Base64.) يبدو أنها تجعل النتيجة HTML - لذلك لست متأكدًا مما إذا كانت المشكلة موجودة في العميل ، أو

لقد حاولت وضع n في الرسالة ، لكن هذا لم ينجح. كيف يمكنني إرسال نص عادي مع فواصل الخط ، والتحقق من أن النتيجة النهائية تبدو صحيحة؟

راجع للشغل ، لا يمكنني بالفعل إرسال البريد الإلكتروني لاختباره مع عملاء البريد الإلكتروني الحقيقي - كورب. تم تأمين الشبكة.

هل كانت مفيدة؟

المحلول

أنت في الواقع لا تضمن أي فواصل خط. يمكنك تضمينها مباشرة في سلسلة حرفية في خادم SQL على النحو التالي.

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);

أو قد يكون النهج الناجم

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); 

كلاهما سوف يرفع خطأ إذا myTable يحتوي على أكثر من صف واحد لأنك تقوم بتعيين النتيجة لمتغير القياسي.

نصائح أخرى

لقد استخدمت دائما CHAR(13)+CHAR(10) لإنشاء فواصل خط (يبدو أنها تعمل مختلطة مع قيم nvarchar) في TSQL ، لذلك جرب شيء مثل هذا:

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

انتاج:

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

هذه CHAR(13)+CHAR(10) سوف تعمل مع msdb.dbo.sp_send_dbmail, ، أرسل رسائل البريد الإلكتروني المنسقة باستخدام ذلك طوال الوقت.

في وقت متأخر من الحزب ، ولكن كما فرناندو 68 يذكر أعلاه ، إذا سمح لك رسائل البريد الإلكتروني HTML ، setbodyformat = 'html' ، فيمكنك استخدامها <br/> بالنسبة إلى خطوات الخطوط وجعلها خيالية كما تريد باستخدام جميع العلامات التي تحصل عليها من HTML مثل Breaks ، IMG ، قوية وما إلى ذلك ...

  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' ;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top