在 C# 中格式化纯文本电子邮件

发布于 2024-09-10 17:44:12 字数 899 浏览 4 评论 0原文

我正在尝试使用纯文本打印电子邮件内的表格。 我有以下代码:

string body = string.Format("{0,-30}{1,-30}{2,-50}{3,-40}",
                                   "Col1", "Col2", "Col2", “Col4”);

body += string.Format("{0,-30}{1,-30}{2,-50}{3,-40}",
                                value1, value2, value3, value4);

Microsoft.Office.Interop.Outlook.ApplicationClass myOutlookApplication = null;
myOutlookApplication = new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook.MailItem myNewMail = 
    (Microsoft.Office.Interop.Outlook.MailItem)myOutlookApplication.CreateItem( 
      Microsoft.Office.Interop.Outlook.OlItemType.olMailItem );            

myNewMail.To = recipient;
myNewMail.Subject = subject;
myNewMail.Body = body;
myNewMail.BodyFormat = OlBodyFormat.olFormatPlain;            

myNewMail.Send();

我遇到的问题是正文的文本未对齐。它似乎也将文本包装在邮件内。谁能告诉我我在这里可能做错了什么?

I am trying to print a table inside an e-mail using plain text.
I have the following code:

string body = string.Format("{0,-30}{1,-30}{2,-50}{3,-40}",
                                   "Col1", "Col2", "Col2", “Col4”);

body += string.Format("{0,-30}{1,-30}{2,-50}{3,-40}",
                                value1, value2, value3, value4);

Microsoft.Office.Interop.Outlook.ApplicationClass myOutlookApplication = null;
myOutlookApplication = new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook.MailItem myNewMail = 
    (Microsoft.Office.Interop.Outlook.MailItem)myOutlookApplication.CreateItem( 
      Microsoft.Office.Interop.Outlook.OlItemType.olMailItem );            

myNewMail.To = recipient;
myNewMail.Subject = subject;
myNewMail.Body = body;
myNewMail.BodyFormat = OlBodyFormat.olFormatPlain;            

myNewMail.Send();

The problem I have is that the text for the body does not line up. It also seems to wrap the text inside the mail. Can anyone tell me what I may be doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恍梦境° 2024-09-17 17:44:12

仅在以下情况下以纯文本创建(或实际模仿其行为)表格才有效:

  1. 表格中的值与列的宽度完全相同。这是您(程序员)应该注意的事情,当文本太短时,在文本后面添加空格,或者如果文本太长,则剪切文本。

  2. 接收文本的用户使用等宽字体 (http://en.wikipedia. org/wiki/Monospaced_font)来查看表格。不幸的是,如果您通过电子邮件发送表格,这将超出您的控制...

Creating (or actually imitating the behaviour of) a table in plain text only works well when:

  1. The values in your tables are exactly the same width as the widht of the column. This is something you (the programmer) should take care of by trailing the text with spaces when the text is too short or cutting of the text if is too long.

  2. The user that is receiving the text is using a monospace font (http://en.wikipedia.org/wiki/Monospaced_font) to view the table. And that is unfortunately beyond your control if you send the table in an e-mail message...

清泪尽 2024-09-17 17:44:12

这是由于字体造成的。如果你想让它对齐,你需要使用固定宽度的字体,就像你的代码编辑器希望的那样。

否则,您可以尝试使用 TAB 进行某些操作。但这可能很棘手。

That's due to fonts. If you want it to line up, you need to use a fixed width font like your code editor hopefully does.

Else you can attempt something using TAB's. But that can be tricky.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文