如何保持为前端JavaScript格式化的MS Graph电子邮件的正文格式?

huangapple go评论63阅读模式
英文:

How to maintain Body formatting for MS Graph email that is formatted for Javascript on the frontend?

问题

在我的前端中,我正在使用Vuetify,这是一个VueJS UI框架,用于电子邮件输入的正文部分采用了一个组合框。用户可以编辑它等等。我最初使用以下形式的预测文本来填充它:

textModel = "To whom it may concern, \n\n" +
"We are writing to you about blah blah blah. \n\n" +
"Thank You"

然后,将这些数据通过[FormData]发送到后端API,并将其绑定到一个较大的电子邮件模型(主题、收件人等)中。然而,对于我的电子邮件界面,我使用以下方式将正文分配给消息对象:

var message = new Message
{
    Subject = email.Subject,
    Body = new ItemBody
    {
        ContentType = Microsoft.Graph.BodyType.Html,
        Content = email.Body,
    },
    ToRecipients = ToRec,
    CcRecipients = CcRec,
    BccRecipients = BccRec,
    Attachments = attachments,
};

当我通过MS Graph API发送电子邮件并收到电子邮件时,正文格式化为单行,因此"\n"不起作用。如何处理这种情况以便在前端和后端使用换行符?我是否需要在正文字符串上执行查找和替换操作,并将其替换为HTML中的
标签?

谢谢

英文:

On my front end I am using Vuetify which is a VueJS UI framework which uses a combobox for the body of the email input. The user can edit it and what not. I initially prefil it with predicted text of this form:

textModel = "To whom it may concern, \n\n" +
"We are writing to you about blah blah blah. \n\n" +
"Thank You"

This data is then sent to the backend API via [FormData] and binding it to a model as it is apart of a larging email Model (subject, recipients, etc). However, for my email interface I use the following to assign the body to the message object:

            var message = new Message
        {
            Subject = email.Subject,
            Body = new ItemBody
            {
                ContentType = Microsoft.Graph.BodyType.Html,
                Content = email.Body,
            },

            ToRecipients = ToRec,
            CcRecipients = CcRec,
            BccRecipients = BccRec,
            Attachments = attachments,
        };

When I send the email via MS Graph API and I receive the email the body is formatted as a single line so the "\n" is doing nothing. How would I account for new lines in this manner to use on the front end and backend? Do I have to do a find and replace on the body string and replace with a br tags in html?

Thanks

答案1

得分: 1

"你试图发送的内容在 textModel 中格式化为文本,但在 Body 中,你将正文类型设置为了 HTML。如果你想发送纯文本正文,你应该这样设置:

Body = new ItemBody
{
    ContentType = Microsoft.Graph.BodyType.Text,
    Content = email.Body,
},

或者,你可以将你的文本格式化为 HTML,例如:

textModel = "To whom it may concern, <br>" +
    "We are writing to you about blah blah blah. <br><br>" +
    "Thank You<br>"
英文:

What your trying to send in

textModel = "To whom it may concern, \n\n" +
"We are writing to you about blah blah blah. \n\n" +
"Thank You"

is formatted text but in

            Body = new ItemBody
        {
            ContentType = Microsoft.Graph.BodyType.Html,
            Content = email.Body,
        },

your saying the body is Html if you want to send a Text body you should make it

        Body = new ItemBody
        {
            ContentType = Microsoft.Graph.BodyType.Text,
            Content = email.Body,
        },

or format your text as html instead eg

textModel = "To whom it may concern, <br>" +
"We are writing to you about blah blah blah. <br><br>" +
"Thank You<br>"

huangapple
  • 本文由 发表于 2023年4月4日 04:03:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定