How to create a multi line body in C# System.Net.Mail.MailMessage

前端 未结 14 612
忘了有多久
忘了有多久 2020-12-03 02:45

If create the body property as

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

message.Body =\"First Line \\n second line\";


        
相关标签:
14条回答
  • 2020-12-03 03:06

    I realise this may have been answered before. However, i had this issue this morning with Environment.Newline not being preserved in the email body. The following is a full (Now Working with Environment.NewLine being preserved) method i use for sending an email through my program.(The Modules.MessageUpdate portion can be skipped as this just writes to a log file i have.) This is located on the main page of my WinForms program.

        private void MasterMail(string MailContents)
        {
            Modules.MessageUpdate(this, ObjApp, EH, 3, 25, "", "", "", 0, 0, 0, 0, "Master Email - MasterMail Called.", "N", MainTxtDict, MessageResourcesTxtDict);
    
            Outlook.Application OApp = new Outlook.Application();
            //Location of email template to use. Outlook wont include my Signature through this automation so template contains only that.
            string Temp = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + ResourceDetails.DictionaryResources("SigTempEmail", MainTxtDict);
    
            Outlook.Folder folder = OApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
    
            //create the email object.
            Outlook.MailItem TestEmail = OApp.CreateItemFromTemplate(Temp, folder) as Outlook.MailItem;
    
            //Set subject line.
            TestEmail.Subject = "Automation Results";
    
            //Create Recipients object.
            Outlook.Recipients oRecips = (Outlook.Recipients)TestEmail.Recipients;
    
            //Set and check email addresses to send to.
            Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("EmailAddressToSendTo");
            oRecip.Resolve();
    
            //Set the body of the email. (.HTMLBody for HTML Emails. .Body will preserve "Environment.NewLine")
            TestEmail.Body = MailContents + TestEmail.Body;
            try
            {
                //If outlook is not open, Open it.
                Process[] pName = Process.GetProcessesByName("OUTLOOK.EXE");
                if (pName.Length == 0)
                {
                    System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE");
                }
    
                //Send email
                TestEmail.Send();
    
                //Update logfile - Success.
                Modules.MessageUpdate(this, ObjApp, EH, 1, 17, "", "", "", 0, 0, 0, 0, "Master Email sent.", "Y", MainTxtDict, MessageResourcesTxtDict);
            }
            catch (Exception E)
            {
                //Update LogFile - Fail.
                Modules.MessageUpdate(this, ObjApp, EH, 5, 4, "", "", "", 0, 0, 0, 0, "Master Email - Error Occurred. System says: " + E.Message, "Y", MainTxtDict, MessageResourcesTxtDict);
            }
            finally
            {
                if (OApp != null)
                {
                    OApp = null;
                }
                if (folder != null)
                {
                    folder = null;
                }
                if (TestEmail != null)
                {
                    TestEmail = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    

    You can add multiple recipients by either including a "; " between email addresses manually, or in one of my other methods i populate from a Txt file into a dictionary and use that to create the recipients email addresses using the following snippet.

            foreach (KeyValuePair<string, string> kvp in EmailDict)
            {
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(kvp.Value);
                RecipientList += string.Format("{0}; ", kvp.Value);
                oRecip.Resolve();
            }
    

    I hope at least some of this helps someone.

    0 讨论(0)
  • 2020-12-03 03:07

    Something that worked for me was simply separating data with a :

    message.Body = FirstLine + ":" + SecondLine;

    I hope this helps

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