换行符未出现在笔记中的邮件正文中

本文关键字:正文 笔记 换行符 | 更新日期: 2023-09-27 18:31:57

我是笔记的新手。我正在尝试使用带有附件的 Lotus Notes 从我的应用程序发送邮件,邮件正常,附件也正常,但问题出在正文内容上,正文丢失格式并呈直线

我期望如下

Dear Sir,
please check the attachment.

Regards,
NewConcept Infotech Pvt.Ltd.,

但它是这样的

Dear Sir,please check the attachment.Regards,NewConcept Infotech Pvt.Ltd.,

我尝试了很多谷歌搜索的所有内容,但没有用。

这是我的代码

 public bool Email(string dbDirectory, string DataBase_Name, string Initialize_Pwd, string From, string To, string CC, string Bcc, string Subject, string body, string FileName, string LogFilePath)
        {
            bool msg = false;
            dynamic EMailReplyTo = ConfigurationSettings.AppSettings["EMailReplyTo"];
            NotesSession objNotesSession = new NotesSession();
            NotesDatabase ndb = null;
            NotesDocument ndoc = null;
            NotesDbDirectory ndbD = null;
            NotesStream LNStream;
            NotesMIMEEntity LNBody;
            object objAttach;
            try
            {
                ////--------------------Lotus Notes Connectivity-------------------------///
                List<string> lstOutPutEmail = new List<string>();
                lstOutPutEmail.Add(DataBase_Name);
                lstOutPutEmail.Add(Initialize_Pwd);
                objNotesSession.Initialize(lstOutPutEmail[1].ToString());
                ////  objNotesSession object Initialized
                ndbD = objNotesSession.GetDbDirectory(dbDirectory);
                ndb = objNotesSession.GetDatabase(dbDirectory, DataBase_Name, false);
                //If the database is not already open then open it.
                if (!ndb.IsOpen)
                {
                    ndb.Open();
                }
                if (ndb != null)
                {
                    ndoc = ndb.CreateDocument();
                    LNStream = objNotesSession.CreateStream();
                    LNBody = ndoc.CreateMIMEEntity();
                    //   ndoc.ReplaceItemValue("SendBy", From);
                    ndoc.ReplaceItemValue("Form", "Memo");
                    ndoc.ReplaceItemValue("From", From);
                    ndoc.ReplaceItemValue("Principal", From);
                    ndoc.ReplaceItemValue("SendTo", To.Split(','));
                    if (CC != null)
                    {
                        if (CC != "")
                        {
                            ndoc.ReplaceItemValue("CopyTo", CC.Split(','));
                        }
                    }
                    if (Bcc != null)
                    {
                        if (Bcc != "")
                        {
                            ndoc.ReplaceItemValue("BlindCopyTo", Bcc.Split(','));
                        }
                    }
                    ndoc.ReplaceItemValue("Subject", Subject);
                    //
                    NotesRichTextItem objMailRTF = ndoc.CreateRichTextItem("Body");

                    ndoc.ReplaceItemValue("Body", body);

                    ndoc.SaveMessageOnSend = true;
                    if (FileName != "")
                    {
                        objAttach = objMailRTF.EmbedObject(Domino.EMBED_TYPE.EMBED_ATTACHMENT, "", FileName, "Attachment");
                    }
                    ndoc.Send(false);
                    ndbD = null;
                    objNotesSession = null;
                    ndb = null;
                    ndoc = null;
                    gl.runLogfile("Mail Send Successfuly To : " + To, LogFilePath);
                }
                msg = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error On sending Mail To : " + To);
                gl.runLogfile("Error On sending Mail To : " + To, LogFilePath);
                gl.runLogfile(ex.Message, LogFilePath);
                msg = false;
            }
            finally
            {
            }
            return msg;
        }

换行符未出现在笔记中的邮件正文中

使用 RichTextItem 的方法 AddNewLine() 确实在 Body 字段中添加新行

NotesRichTextItem objMailRTF = ndoc.CreateRichTextItem("Body");
objMailRTF.AppendText("Dear Sir,");
objMailRTF.AddNewLine(1);
objMailRTF.AppendText("please check the attachment.");
objMailRTF.AddNewLine(2);
...

删除代码行ndoc.ReplaceItemValue("Body", body);,否则它将无法正常工作。

0. 哑剧

如果您使用的是 MIME,则无需创建Body字段。但是您需要使用<br>而不是newline字符。

//Remove this from your code:
//NotesRichTextItem objMailRTF = ndoc.CreateRichTextItem("Body");
//ndoc.ReplaceItemValue("Body", body);
objNotesSession.ConvertMIME = false;
LNStream.WriteText(body.Replace(Environment.NewLine, "<br>"));
LNBody.SetContentFromText(stream, "text/plain;charset=UTF-8", 1728);                
ndoc.SaveMessageOnSend = true;                
if (FileName != "")
{
    //objAttach = objMailRTF.EmbedObject(Domino.EMBED_TYPE.EMBED_ATTACHMENT, "", FileName, "Attachment");
    var child = LNBody.CreateChildEntity();
    var header = child.CreateHeader("Content-Disposition");
    header.SetHeaderValAndParams(string.Format("attachment; filename='"{0}'""), Path.GetFileName(FileName)); 
    LNStream = objNotesSession.CreateStream();
    LNStream.Open(FileName, "binary");
    child.SetContentFromBytes(LNStream, "application/octet-stream", 1730);
    child.EncodeContent(1727);
    ndoc.CloseMIMEEntities(True, "Body");
}

1. 无哑剧

如果您不想使用 MIME,则必须使用AppendText方法而不是ReplaceItemValue方法:

NotesRichTextItem objMailRTF = ndoc.CreateRichTextItem("Body");
//ndoc.ReplaceItemValue("Body", body);
objMailRTF.AppendText(body);
ndoc.SaveMessageOnSend = true;
if (FileName != "")
{
    objMailRTF = ndoc.CreateRichTextItem("Attachment");
    objAttach = objMailRTF.EmbedObject(Domino.EMBED_TYPE.EMBED_ATTACHMENT, "", FileName, "Attachment");
}

在 ndoc.body 中,您必须将换行符编码为 "chr$(13)chr$(10)"。在 java (char)13(char)10 或 ''r' 中。

您需要替换字符串主体,您得到参数,所有出现的换行符(可能是"'",但看看它在字符串中的编码方式)到 (char)13(char)10。

尝试:

body = body.replaceAll("'n", "'r'n");
ndoc.ReplaceItemValue("Body", body);
尝试将 '''' 如果

它不适用于 1 '' 仅。