如何在电子邮件中发送字符串列表并更改文本的对齐方式和格式

时间:2019-04-09 15:28:17

标签: 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?:      

(我知道问题之间的间距需要固定,但要查找文本的内容以及希望的字体等)

1 个答案:

答案 0 :(得分:2)

初始化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>');

表格和正文结尾以及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文档)