簡體   English   中英

格式化電子郵件正文 HTML

[英]Formatting e-mail body HTML

我將如何讓正文使用 HTML 格式。 我需要添加什么以及需要添加哪一行? 我試過 MailMessage.IsBodyHtml = true; 但這對我不起作用。 這是這樣做的方法嗎? 該代碼應該替換另一行還是應該單獨一行?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
using System.Net;
using System.Net.Mail;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            const string subject = "ASSET RECOVERY";
            listBox1.Items.Clear();
            //1. Replace the password
            var fromAddress = new MailAddress("", ""); //Email address then Name
            const string fromPassword = ""; //Email Password
            string body = "";



            //2. Potentially replace any of the Host / Port combo if necessary
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };


            string path = "C:\\Users\\adrian.simonis\\Desktop\\VPN\\AdriansExcel3.xlsx ";
            //3. Replace the above with the actual path that your excel file is.

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = excel.Workbooks.Open(path);
            Worksheet excelSheet = wb.ActiveSheet;
            int rows = excelSheet.Rows.Count;
            int successfulSends = 0;


            for (int i = 1; i < rows; i++)
            {
                string mySendAddress = "";
                string myAsset = "";

                try
                {
                    body = <font size = "20" color = "red" style = "text-align:center;" > "ATTENTION\n\n" </ font >< br > +

"Company is collecting underutilized PCs and other surplus computing equipment. Retiring computers reduces a variety of annual costs, including corporate allocations, maintenance, licensing fees and labor associated with information protection and system patching.\n\n" +

"The following criteria is used to determine an underutilized device:\n\n" +



"Use Login Times and Keyboard / Mouse Activity, Load Average, CPU Utilization, Physical Memory Utilization, Software Usage, Disk Utilization\n\n" +



  "All of these factors are viewed over time scales: one week, four weeks and 16 weeks to determine if this asset falls under a low usage threshold\n\n" +

  "In order to support Aero IT Asset Reduction initiatives, this asset (ASSET TAG HERE) has been identified as underutilized and will be removed on.\n\n" +

"1 - 28 - 20\n\n" +

"If there is a critical business need to leave this device in place, please send an email justification to keep the asset to the following public folder\n\n" +





" | Sr.Desktop Technician\n\n" +

"CompanyAsset Recovery Support\n\n" +

"Email: \n\n" +

"Office: \n\n";

                    myAsset = excelSheet.Cells[i, 19].Value.ToString();
                    mySendAddress = excelSheet.Cells[i, 22].Value.ToString();
                    body = body.Replace("(ASSET TAG HERE)", myAsset); //his assetAssetTag1 ha
                    label2.Text = "Sending email to: " + mySendAddress + " with asset tag: " + myAsset;
                }
                catch
                {
                    System.Threading.Thread.Sleep(3000);
                    label2.Text = "Finished: reached empty excel cell with no send to address";
                    break;
                }

                //Send email here!
                var toAddress = new MailAddress(mySendAddress);
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body
                })

                {
                    try
                    {
                        smtp.Send(message);
                        listBox2.Items.Add(toAddress);
                    }
                    catch (ArgumentOutOfRangeException ex)
                    {
                        listBox1.Items.Add(toAddress);
                    }
                }

                successfulSends++;
                label1.Text = "Successful emails: " + successfulSends;
                System.Threading.Thread.Sleep(3000);

            }

            wb.Close();
        }
    }
}

因此,在將其轉換為 HTML 后,其發送綠色。

"<html><font size = 20, color = red, style =text - align:center;>ATTENTION </ font >\n\n" +

"<html><font size = 10>Company is collecting underutilized PCs and other surplus computing equipment. Retiring computers reduces a variety of annual costs, including corporate allocations, maintenance, licensing fees and labor associated with information protection and system patching.</ font >\n\n" +

"The following criteria is used to determine an underutilized device:\n\n" +



"Use Login Times and Keyboard / Mouse Activity, Load Average, CPU Utilization, Physical Memory Utilization, Software Usage, Disk Utilization\n\n" +



  "All of these factors are viewed over time scales: one week, four weeks and 16 weeks to determine if this asset falls under a low usage threshold\n\n" +

  "In order to support Aero IT Asset Reduction initiatives, this asset (ASSET TAG HERE) has been identified as underutilized and will be removed on.\n\n" +

"1 - 28 - 20\n\n" +

"If there is a critical business need to leave this device in place, please send an email justification to keep the asset to the following public folder\n\n" +





"| Sr.Desktop Technician\n\n" +

"CompanyAsset Recovery Support\n\n" +

"Email:\n\n" +

"Office: phone\n\n</html>";

您正在創建的 HTML 文檔無效。 例如,您正在打開兩個標簽,但從不關閉它們。 此外,據我所知,您的內聯樣式無效(除非我在這里遺漏了某些東西)。

當您嘗試運行程序時,這一行看起來也應該拋出錯誤:

body = <font size = "20" color = "red" style = "text-align:center;" > "ATTENTION\n\n" </ font >< br > +

我有時使用的一個簡單的解決方案是設置一個由占位符填充的有效 HTML 模板(例如在資源文件中),而不是使用容易出錯且經常導致丟失結束標記和其他問題的字符串連接:

<!DOCTYPE html>
<html>
<head>
    <style>
        .attention {
            text-align: center;
            font-size: 20px;
            color: red;
        }
        .content {{
            color: black;
            font-size: 10px;
        }}
    </style>
</head>
<body>
<p class="attention">[PLACEHOLDER_TITLE]</p>

<p class="content">[PLACEHOLDER_CONTENT]</p>

<ol>
  <li>[PLACEHOLDER_LIST_ITEM_1]</li>
  <li>[PLACEHOLDER_LIST_ITEM_2]</li>
  <li>[PLACEHOLDER_LIST_ITEM_3]</li>
</ol>
</body>
</html>

這些可以很容易地在以后用實際內容替換,並使代碼更易於閱讀:

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

需要明確的是:在您的實際應用程序中,您必須確保所有字符都被正確轉義(在模板和您插入的替換字符串中)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM