在 C# 中格式化纯文本电子邮件
我正在尝试使用纯文本打印电子邮件内的表格。 我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅在以下情况下以纯文本创建(或实际模仿其行为)表格才有效:
表格中的值与列的宽度完全相同。这是您(程序员)应该注意的事情,当文本太短时,在文本后面添加空格,或者如果文本太长,则剪切文本。
接收文本的用户使用等宽字体 (http://en.wikipedia. org/wiki/Monospaced_font)来查看表格。不幸的是,如果您通过电子邮件发送表格,这将超出您的控制...
Creating (or actually imitating the behaviour of) a table in plain text only works well when:
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.
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...
这是由于字体造成的。如果你想让它对齐,你需要使用固定宽度的字体,就像你的代码编辑器希望的那样。
否则,您可以尝试使用 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.