简体   繁体   中英

ASP classic CDO Email messaging - how to covert space and new line in email message

How can i convert space and tab in my email. kindly see below example.

test1 
test 
zzzyyy     trsthshs  asas   asas    dasdads sadasd
asdad 1    2   3   5   6  7                       8
:test only..

when i received the email the format was changed. number 7 was near to number 8, the spacing was descreased to 1

test1 
test 
zzzyyy trsthshs asas asas dasdads sadasd
asdad 1 2 3 5 6 7 8
:test only..

how can I retain the the format to have multiple spaces..

I used this code.

Set objCDOMail = Server.CreateObject("CDO.Message")
Set  objCDOMail.Configuration = cdoConfig 
objCDOMail.BodyPart.Charset = "UTF-8"
objCDOMail.HTMLBody  = sBodyText 

You can send a message using CDO.Message either as plaintext using the .TextBody property or as HTML using the .HtmlBody property as in your code snippet above. If you use plaintext, since it should default to a mono-spaced font in typical email reader on receipt, your manual spacing would be preserved. If you need to send as HTML, then you need to format the text according to the way HTML is rendered --- doesn't matter that it is email or in a web browser. You have to keep in mind that HTML will automatically collapse adjacent spaces by default. So if you need to format the text, you need to apply the proper styles to the block of text in a DIV tag or you can use the HTML PRE tag to denote preformatted text.

While you are using HTML email format, try to use HTML Encoding for the message before to send it.

string encodedMessage = System.Web.HttpUtility.HtmlEncode(message);

this will change the spaces in the mail to HTML space special character ( )

Your text is not HTML so don't assign it to the HtmlBody property. Instead use:

objCDOMail.TextBody = sBodyText

To replace, white space, tab, line feeds, you can do the following...

space_entity = "&" & "nbsp" & ";" 'I had to break it up like this cause stackoverflow does not take & n b s p ; sequence witohut the spaces in there ;)
    html_br = "" 'same reason! can't write regular br tag in here.. :(
sBodyText = replace(sBodyText," ",space_entity)
    sBodyText = replace(sBodyText,vbtab,space_entity)
    sBodyText = replace(sBodyText,vbcrlf,html_br & space_entity)

HTH

Replace(strValue, " ", " ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM