使用MS Outlook发送电子邮件不添加签名

时间:2014-06-05 16:24:09

标签: delphi email outlook delphi-2010 ole

我使用Delphi RAD Studio 2010和下一个使用Outlook发送电子邮件的代码:

procedure SendOutlookMail(email,subject,body,fileat:string);
const
  olMailItem = 0;
var
   vMailItem: variant;
   Outlook: OutlookApplication;
   NmSpace: NameSpace;
   Folder: MAPIFolder;
begin
  Outlook := CoOutlookApplication.Create;
  NmSpace := Outlook.GetNamespace('MAPI');
  NmSpace.Logon('', '', False, False);
  Folder := NmSpace.GetDefaultFolder(olFolderInbox);
  Folder.Display;

  vMailItem := Outlook.CreateItem(olMailItem);
  if email<>'' then  vMailItem.Recipients.Add(email);
  vMailItem.Subject := subject;
  vMailItem.Body := Body;
  vMailItem.Attachments.Add(fileat);
  vMailItem.Display(false);
end;

它会打开一条新的Outlook消息并将其显示在前面,只需按“发送”即可发送。没关系。问题是,使用此方法创建新电子邮件不会添加签名。如果我在Ms Outlook中创建新邮件,则会自动添加签名 无论如何我可以添加用户在MS Outlook中配置的签名吗? (不将签名文本添加到“Body”字符串变量)。提前谢谢。

2 个答案:

答案 0 :(得分:2)

当您调用MailItem.Display或访问MailItem.GetInspecrtor时,会添加签名。

首先调用MailItem.Display(此时将添加签名),然后将您的数据与现有主体合并。请注意,设置纯文本Body属性将消除格式,因此您需要使用HTMLBody属性。请记住,2个HTML字符串不能简单连接 - 阅读HTMLBody属性,找到合适的插入位置(在<body>标记之后?),然后插入数据。

答案 1 :(得分:1)

//这是我在Delphi中的表现。它显示了其他人没有做过的部分,即//解析电子邮件HTML并将文本放在签名之上。 //开头的部分会加载我要添加的字符串,所以你//可以忽略它。

procedure TfrmEngInfo2.CreateMSOutlookHoldEmail;
var
  i: Integer;
  Outlook: OleVariant;
  Mail: OleVariant;
  EmailTo,EmailCC: string;
  SubjectLine: string;
  PartNbr,PONbr,JobNbr: string;
  HTMLInfo: string;
  JobInfo: string;
  x: integer;
  iPos: integer;
  RealPosition,GTPosition: integer;
  const
  olMailItem = 0;
  olByValue = 1;
  olFormatHTML = 2;


begin
  SubjectLine := 'PCB Hold /';
  if JOBHEMAI.state = dsInactive then
    JOBHEMAI.open;
  if CUST.state = dsInactive then CUST.open;

  if RELEASE.FindKey([AdsQuery1.FieldByName('R-JOB#').asstring, AdsQuery1.FieldByName('R-RELEASE-NBR').asstring]) then
  begin
    PONbr := RELEASE.FieldByName('R-PO-NBR').asstring;
    JobNbr := AdsQuery1.FieldByName('R-JOB#').asstring + AdsQuery1.FieldByName('R-RELEASE-NBR').asstring;
    SubjectLine := SubjectLine + ' PO#: ' + PONbr + ' / ';
  end;

  if JOBHEMAI.FindKey([AdsQuery1.FieldByName('R-JOB#').asstring]) then
    EmailTo := JOBHEMAI.FieldByName('HoldEmailAddress').asstring;

   if HEADER.FindKey([AdsQuery1.FieldByName('R-JOB#').asstring]) then
   begin
    PartNbr := HEADER.FieldByName('H-PART#').asstring;
    SubjectLine := SubjectLine + ' Part#: ' + PartNbr;
    if CUST.FindKey([HEADER.FieldByName('H-CUST-NBR').asstring]) then
      if EmailTo = '' then
        EmailTo := CUST.FieldByName('HoldEmailAddress').asstring;
    if SALEPRSN.FindKey([HEADER.FieldByName('H-SLMN#').asstring]) then
      EmailCC := SALEPRSN.fieldByName('E-mail').asstring;

   end;
   SubjectLine := SubjectLine + JobNbr;


  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;
  JobInfo := 'We have released the hold for your job Part: ' + PartNbr + ' PO: ' + PONbr
  + 'Job Number: ' + JobNbr;
  Mail := Outlook.CreateItem(olMailItem);
  Mail.To := EmailTo;
  Mail.cc := EmailCC;
  Mail.Subject := SubjectLine;
  // Main.Attachments.Add('c:\test.jpg', olByValue, 1, 'My Test Image');
  Mail.BodyFormat := olFormatHTML;
  //This load the HTMLBody with all the outlook stuff including the users signature.  Basically a blank email message
  Mail.GetInspector;
  // Find the position of the opening <body command.
  x := pos('<body',Mail.HTMLBody);
  // Put the HTMLBody in to a string field so we can examine it.
  HTMLInfo := Mail.HTMLBody;

   // Now loop through and find the position of the closing tag. '>'   It must be in a position greater than the starting tag
   RealPosition := 0;
   while pos('>',HTMLInfo) > 0 do
   begin
      GTPosition := pos('>',HTMLInfo);
      RealPosition := RealPosition + GTPosition;
      if RealPosition > x then
        break;
      HTMLInfo := copy(HTMLInfo,GTPosition + 1,Length(HTMLInfo) - GTPosition);
   end;

  // Since we destroyed the HTMLInfo in the analysis, load it again.
  HTMLInfo := Mail.HTMLBody;
  // Now insert our information at the right spot.
  insert(JobInfo,HTMLInfo,RealPosition + 1);
  // Now load the modified HTMLInfo back to the Mail.HTMLBody so that it can display in the message.
  Mail.HTMLBody := HTMLInfo;
  // Finally display the Outlook Email
  Mail.Display;
  Outlook := unassigned;
end;