2013-01-10 18 views
1

我處於這種情況,我需要使用Thunderbird和Delphi XE3發送帶附件的電子郵件 我不知道從哪裏開始,所以我詢問任何人是否有鏈接到站點可能會找到信息。使用Thunderbird從Delphi發送郵件

+1

爲什麼你需要雷鳥?您可以使用Indy SMTP自己發送郵件。 – whosrdaddy

+0

這很難。如果Thunderbird是您的默認郵件客戶端,您可以ShellExecute電子郵件地址鏈接,但仍會留下附件。 Whosrdaddy的建議更好。 –

+2

Thunderbird是MAPI兼容的,所以你可以編寫一個程序,使用默認的電子郵件客戶端發送附件使用MAPI的電子郵件,如果它是雷鳥(或其他),它將工作。我發現一個簡單的例子,使用谷歌搜索_delphi mapi_:http://www.delphifaq.com/faq/delphi/network/f236.shtml – jachguate

回答

5

從文檔中,你可以使用雷鳥的command line options,所以我認爲使用ShellExecute應該工作。我沒有試過這個。

ShellExecute(Handle, 'path\to\thunderbird.exe', 
    '-compose "[email protected],attachment=''file:///c:/test.txt''", 
    nil, SW_SHOWNORMAL); 
+0

如果你直接執行.exe,那麼你應該使用'CreateProcess()'來代替。 'ShellExecute()'只是委託給'CreateProcess()'作爲.exe文件,所以你可以剪掉中間人。 –

+0

@RemyLebeau雖然是真的,但CreateProcess是相當複雜的野獸,它的靈活性的代價。所以你最好使用一些簡化的(減弱)包裝。它可以像Jedi CodeLibrary中的包裝一樣使用Delphi包裝器,也可以使用Microsoft提供的包裝器ShellExecute。如果有限的ShellExecute功能對您來說足夠了 - 爲什麼避免使用簡單的API? –

+0

@LeonardoHerrera - 這可能是一個選擇。它看起來很簡單,然後廣告這個選項。 – OZ8HP

2

下面的代碼是基於這兩篇文章:

步驟:

在窗體上放一個FileListBox和一個按鈕,並將FileListBox的MultiSelect屬性設置爲true。 使用此代碼將filelistbox中的選定條目傳遞給默認郵件應用程序。

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls, FileCtrl; 

type 
    TForm1 = class(TForm) 
    FileListBox1: TFileListBox; 
    Button1: TButton; 
    procedure Button1Click(Sender: TObject); 
    end; 

var 
    Form1: TForm1; 

implementation 

uses 
    ActiveX, ShlObj, ComObj; 

{$R *.dfm} 

function GetFileListDataObject(const Directory: string; Files: 
    TStrings): 
    IDataObject; 
type 
    PArrayOfPItemIDList = ^TArrayOfPItemIDList; 
    TArrayOfPItemIDList = array[0..0] of PItemIDList; 
var 
    Malloc: IMalloc; 
    Root: IShellFolder; 
    FolderPidl: PItemIDList; 
    Folder: IShellFolder; 
    p: PArrayOfPItemIDList; 
    chEaten: ULONG; 
    dwAttributes: ULONG; 
    FileCount: Integer; 
    i: Integer; 
begin 
    Result := nil; 
    if Files.Count = 0 then 
    Exit; 
    OleCheck(SHGetMalloc(Malloc)); 
    OleCheck(SHGetDesktopFolder(Root)); 
    OleCheck(Root.ParseDisplayName(0, nil, 
    PWideChar(WideString(Directory)), 
    chEaten, FolderPidl, dwAttributes)); 
    try 
    OleCheck(Root.BindToObject(FolderPidl, nil, IShellFolder, 
     Pointer(Folder))); 
    FileCount := Files.Count; 
    p := AllocMem(SizeOf(PItemIDList) * FileCount); 
    try 
     for i := 0 to FileCount - 1 do 
     begin 
     OleCheck(Folder.ParseDisplayName(0, nil, 
      PWideChar(WideString(Files[i])), chEaten, p^[i], 
      dwAttributes)); 
     end; 
     OleCheck(Folder.GetUIObjectOf(0, FileCount, p^[0], IDataObject, 
     nil, 
     Pointer(Result))); 
    finally 
     for i := 0 to FileCount - 1 do 
     begin 
     if p^[i] <> nil then 
      Malloc.Free(p^[i]); 
     end; 
     FreeMem(p); 
    end; 
    finally 
    Malloc.Free(FolderPidl); 
    end; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
var 
    SelFileList: TStrings; 
    I: Integer; 
    DataObject: IDataObject; 
    Effect: Integer; 
    CLSID_SendMail: TGUID; 
    DT: IDropTarget; 
    P: TPoint; 
begin 
    CLSID_SendMail := StringToGUID('{9E56BE60-C50F-11CF-9A2C-00A0C90A90CE}'); 

    with FileListBox1 do 
    begin 
    SelFileList := TStringList.Create; 
    try 
     SelFileList.Capacity := SelCount; 
     for i := 0 to FileListBox1.Items.Count - 1 do 
     if Selected[i] then 
      SelFileList.Add(Items[i]); 
     DataObject := GetFileListDataObject(Directory, SelFileList); 
    finally 
     SelFileList.Free; 
    end; 
    Effect := DROPEFFECT_NONE; 
    CoCreateInstance(CLSID_SendMail, nil, CLSCTX_ALL, IDropTarget, DT); 
    DT.DragEnter(DataObject, MK_LBUTTON, P, Effect); 
    DT.Drop(DataObject, MK_LBUTTON, P, Effect); 
    end; 
end; 

end. 


我原來的博客文章(德爾福2009年測試):http://mikejustin.wordpress.com/2009/07/03/how-can-i-simulate-send-to-with-delphi/