delphi - 如何在电子邮件中发送字符串列表并更改文本的对齐方式和格式
问题描述
我为用户创建了一个离开表单,以便在编辑框和组合框中输入详细信息。然后我创建了一个按钮,它将发送一封电子邮件,其中包含输入到所述编辑和组合框中的详细信息中的所有值。我通过创建一个字符串列表然后将其分配给 mailitem.body 来完成此操作。一切正常,但我想要一种让电子邮件正文看起来更好的方法。通过更改格式、文本的字体大小等,或者至少能够在电子邮件正文中左对齐和居中对齐我的文本。
procedure TForm1.Button1Click(Sender: TObject);
const
olMailItem = 0;
var
Outlook : OLEVariant;
MailItem : Variant;
MailInspector : Variant;
stringlist : TStringlist;
tab : Char;
begin;
try
Outlook := GetActiveOleObject('Outlook.Application');
except
Outlook := CreateOleObject('Outlook.Application');
end;
try
Stringlist := TStringList.Create;
MailItem := Outlook.CreateItem(olMailItem) ;
MailItem.Subject := 'Leavers form';
MailItem.Recipients.Add('*******@hotmail.com');
MailItem.CC:='******@googlemail.com';
Stringlist := TStringList.Create;
Tab := chr(9);
StringList.Add('Hi all,');
StringList.Add('Please find leavers details below,');
StringList.Add('Thank you,');
StringList.Add('');
StringList.Add('Employee Name:' + tab + edit1.text);
StringList.Add('Payroll Number:' + tab + edit2.text);
stringList.Add('Campaign:' + tab + edit3.text);
stringList.Add('Leave Date:' + tab + edit4.text);
stringList.Add('probation period:' + tab + Combobox1.text);
stringList.Add('Reason for leaving:' + tab + Combobox8.text);
stringList.Add('If other please specify:' + tab + edit7.text);
stringList.Add('Would you re-employ?:' + tab + Combobox2.text);
stringList.Add('If "no" please specify reasons:' + tab + edit9.text);
stringList.Add('Has Employee recieved an advance:' + tab + Combobox3.text);
stringList.Add('payment which needs to be recuped');
stringList.Add('from their final salay' + tab + edit11.text);
stringList.Add('Due any payment in lieu of notice?:' + tab + Combobox4.text);
stringList.Add('If "yes" please specify weeks to pay e.g 4.25:'+ tab + edit13.text);
stringList.Add('Holiday Entitlment to date:' + tab + edit14.text);
stringList.Add('Holiday taken to date:' + tab + edit15.text);
stringList.Add('Holidays to be paid/deducted:'+ tab + edit16.text);
stringList.Add('has employee returned laptop?:' + tab +combobox9.text) ;
stringList.Add('Has employee returned pass?: ' + tab + Combobox5.text);
stringList.Add('Has employee returned headset?:' + tab + Combobox6.text);
stringList.Add('Has employee returned locker key?:' + tab + Combobox7.text);
MailItem.Body := stringlist.text ;
MailItem.Send;
实际结果
Hi all,
Please find leavers details below,
Thank you,
Employee Name: Jeffy Biscuits
Payroll Number: 123456789
Campaign: HR
Leave Date: 23/12/2018
probation period: No
Reason for leaving: Dismissal
If other please specify:
Would you re-employ?: No
If "no" please specify reasons: Poor attendance
Has Employee recieved an advance: No
payment which needs to be recuped
from their final salay
Due any payment in lieu of notice?: No
If "yes" please specify weeks to pay e.g 4.25:
Holiday Entitlment to date: 0
Holiday taken to date: 30
Holidays to be paid/deducted: 5
has employee returned laptop?: N/A
Has employee returned pass?: Yes
Has employee returned headset?: Yes
Has employee returned locker key?:
预期的
Hi all,
Please find leavers details below,
Thank you,
Employee Name: Jeffy Biscuits
Payroll Number: 123456789
Campaign: HR
Leave Date: 23/12/2018
probation period: No
Reason for leaving: Dismissal
If other please specify:
Would you re-employ?: No
If "no" please specify reasons: Poor attendance
Has Employee recieved an advance: No
payment which needs to be recuped
from their final salay
Due any payment in lieu of notice?: No
If "yes" please specify weeks to pay e.g 4.25:
Holiday Entitlment to date: 0
Holiday taken to date: 30
Holidays to be paid/deducted: 5
has employee returned laptop?: N/A
Has employee returned pass?: Yes
Has employee returned headset?: Yes
Has employee returned locker key?:
(我知道问题之间的间距需要固定,但要寻找文本元素和希望字体等)
解决方案
初始化MailItem
属性后,填充StringList: TStringList
以保存您的html
内容:
StringList := TStringList.Create;
...
StringList.Add('<html>');
StringList.Add('<head>');
StringList.Add('<style>');
StringList.Add('table, th, td {');
//StringList.Add(' border: 1px solid black;');
StringList.Add('}');
StringList.Add('</style>');
StringList.Add('</head>');
然后从<body>
:
并与月报和致敬
...
StringList.Add('<body>');
StringList.Add('<table>');
StringList.Add(' <tr>');
StringList.Add(' <th>Month</th>');
StringList.Add(' <th>Savings</th>');
StringList.Add(' </tr>');
StringList.Add(' <tr>');
StringList.Add(' <td>January</td>');
StringList.Add(' <td>$100</td>');
StringList.Add(' </tr>');
StringList.Add(' <tr>');
StringList.Add(' <td>February</td>');
StringList.Add(' <td>$80</td>');
StringList.Add(' </tr>');
StringList.Add('</table>');
StringList.Add('<p>');
StringList.Add('<p>');
StringList.Add('Hi all,<p>');
StringList.Add('Please find leavers details below,<p>');
StringList.Add('Thank you,<p>');
StringList.Add('<p>');
body和html的表格和结尾
...
StringList.Add('<table>');
StringList.Add('<tr><td>Employee Name:</td>' + '<td>' + edit1.text + '</td>');
StringList.Add('<tr><td>Payroll Number:</td>' + '<td>' + edit2.text + '</td>');
StringList.Add('<tr><td>Campaign:</td>' + '<td>' + edit3.text + '</td>');
StringList.Add('<tr><td>Leave Date:</td>' + '<td>' + edit4.text + '</td>');
StringList.Add('<tr><td>probation period:</td>' + '<td>' + Combobox1.text + '</td>');
... (continues)
...
StringList.Add('</table>');
StringList.Add('</body>');
StringList.Add('</html>');
然后添加StringList.Text
并MailItem.Body
发送。
请注意,我的编辑器中每条记录的第一个和第二个字段之间只有空白。那只是为了在编码期间有更好的可见性,以发现最终丢失的东西。表不需要空间。表格根据文本内容自行调整(列的宽度也可以显式设置,查看 html 文档)
推荐阅读
- php - 如何编辑具有相同键的数组组合的这个foreach循环?
- c++ - NOEXCEPT 之前的预期初始化程序
- typo3 - 使用 TYPO3 存储库查询获取禁用/隐藏的关系表记录
- php - Laravel/PHP 重构代码以使用相同的不同模式
- c# - 检查是否从 Checkchanged 事件中检查了 RadioButton
- amazon-web-services - 为什么使用 terraform 创建 API 网关的方法响应不同?
- google-tag-manager - 在跟踪代码管理器中多次触发事件
- r - 如何在R中以0小数舍入百分比
- linux - 访问某些 index.php 或 index.html 时出现 Apache 403 错误,尽管每个 index.php 或 index.html 具有相似的权限
- java - Hazelcast EntryProcessor ClassNotFoundException