delphi发送html带附件邮件
var html: TIdText; begin if not SMTP.Connected then SMTP.Disconnect(); try SMTP.Connect; try if SMTP.Authenticate then //验证 begin EMessage.Recipients.EMailAddresses := FUser; EMessage.ClearBody;//清空正文 html:= TIdText.Create(EMessage.MessageParts); EMessage.Subject:='Nest MAC Traceability Report'; html.ContentType := 'text/html;charset=gb2312'; html.ContentTransfer := '7bit'; EMessage.ContentType := 'multipart/alternative'; html.Body.Add('Dear Sir:'); html.Body.Add(''); html.Body.Add('Mac address Status'); html.Body.Add('<html>'); html.Body.Add('<head>'); html.Body.Add('<meta http-equiv="Content-Type" content="textml; charset=big5">'); html.Body.Add('<meta name="GENERATOR" content="Microsoft FrontPage 4.0">'); html.Body.Add('<meta name="ProgId" content="FrontPage.Editor.Document">'); html.Body.Add('<title>Nest MAC Traceability Report</title>'); html.Body.Add('<body>'); html.Body.Add('<table>'); html.Body.Add('<tr>Date : </tr>'); html.Body.Add('<tr>'); html.Body.Add('<table bordercolorlight="black" width="50%" border="3" bordercolor="black" cellspacing="0" >'); html.Body.Add('<tr bgcolor="#31C4F5" align="Center">'); html.Body.Add('<td width="500">Image</td>'); html.Body.Add('<td width="500">Releasing date</td>'); html.Body.Add('<td width="700">Total released Qty</td>'); html.Body.Add('<td width="1000">Cumulative Consumed Qty</td>'); html.Body.Add('<td width="500">Leftover Qty</td>'); html.Body.Add('<td width="700">Cumulative scrap Qty</td>'); html.Body.Add('</tr>'); html.Body.Add('<tr>'); html.Body.Add('</tr>'); html.Body.Add('</table>'); html.Body.Add('</tr>'); html.Body.Add('</table>'); html.Body.Add('</body>'); html.Body.Add('<ml>'); EMessage.From.Address := SMTP.Username;//发送人 EMessage.From.Name := '彭工'; if FileExists(FAttacFile) then begin AttachmentFile := TIdAttachmentFile.Create(EMessage.MessageParts, FAttacFile); AttachmentFile.ContentType := 'text/plain'; end; SMTP.Send(EMessage); end; finally FreeAndNil(html); FreeAndNil(EMessage); if Assigned(AttachmentFile) then FreeAndNil(AttachmentFile); SMTP.Disconnect(); end; except ON E: Exception do raise Exception.Create(E.Message); end; end;