Formatting e-mail body HTML

前端 未结 1 582
一向
一向 2021-01-26 00:31

How would I go about making the body work with HTML format. what do I need to add and what line would I need to add it? I have tried MailMessage.IsBodyHtml = true; but that did

1条回答
  •  遇见更好的自我
    2021-01-26 01:30

    The HTML document you are creating isn't valid. E.g. you are opening two tags, but never close them. Also as far as I can tell your inline-styles aren't valid (unless there is something I am missing here).

    This line also looks like it should throw an error, when you are trying to run the program:

    body =  "ATTENTION\n\n" < br > +
    

    Instead of using string concatenation which is error prone and often leads to missing closing tags and other issues, an easy solution I sometimes use is to set up a valid HTML-template (e.g. in a resource file) that is populated by placeholders:

    
    
    
        
    
    
    

    [PLACEHOLDER_TITLE]

    [PLACEHOLDER_CONTENT]

    1. [PLACEHOLDER_LIST_ITEM_1]
    2. [PLACEHOLDER_LIST_ITEM_2]
    3. [PLACEHOLDER_LIST_ITEM_3]

    These can then easily be replaced with the actual content later on and make the code much easier to read:

    string body = (String)GetLocalResourceObject("EmailTemplate");
    body.Replace("[PLACEHOLDER_TITLE]", title);
    body.Replace("[PLACEHOLDER_CONTENT]", content);
    ...
    

    Just to be clear: In your actual application you have to make sure that all characters are correctly escaped (in both the template and the replacement strings you are inserting into it).

    0 讨论(0)
提交回复
热议问题