newline not coming in mail body in Notes

隐身守侯 提交于 2019-12-11 06:36:42

问题


I am new to Notes. I am trying to send a mail from my application using Lotus Notes with an attachment,mail goes properly and attachment also goes but the problem is with the body content,the body looses its format and comes in a straight line

i expect as follows

Dear Sir,
please check the attachment.


Regards,
NewConcept Infotech Pvt.Ltd.,

but it comes like this

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

i tried everything googled a lot but no use.

this is my code

 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;
        }

回答1:


0. MIME

If you are using MIME then you don't need to create the Body field. But you need to use <br> instead of newline characters.

//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. No MIME

If you don't want to use MIME then you must use AppendText method instead of ReplaceItemValue method:

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");
}



回答2:


Use RichTextItem's method AddNewLine() do add new lines to your Body field

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

Delete the code line ndoc.ReplaceItemValue("Body", body); as it won't work otherwise.




回答3:


In your ndoc.body, you have to code the newlines as "chr$(13)chr$(10)". In java (char)13(char)10 or \r\n.

You need to replace in the String body, you get in parameter, all occurence of newline (probably '\n' but look how it is coded in the String) to (char)13(char)10.

try:

body = body.replaceAll("\n", "\r\n");
ndoc.ReplaceItemValue("Body", body);

try will \\ if it doesn't work with 1 \ only.



来源:https://stackoverflow.com/questions/27124953/newline-not-coming-in-mail-body-in-notes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!