Delphi叫用.NET自訂類別

Delphi叫用.NET自訂類別

.NET自訂類別製作
1.參考:http://teamsybase.net/blogs/?p=106
2.VS2005建立mySMTP的類別庫(DLL Library)專案 
3.加入程式碼:

using System;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace mySMTP
{
public class SMTP
{
private String senderName;

public String SenderName
{
get { return senderName; }
set { senderName = value; }
}
private String senderEmail;
public String SenderEmail
{
get { return senderEmail; }
set { senderEmail = value; }
}
private String smtpServer;
public String SmtpServer
{
get { return smtpServer; }
set { smtpServer = value; }
}
private String recipientEmail;
public String RecipientEmail
{
get { return recipientEmail; }
set { recipientEmail = value; }
}
private String subject;
public String Subject
{
get { return subject; }
set { subject = value; }
}
private String ccEmail = "";
public String CcEmail
{
get { return ccEmail; }
set { ccEmail = value; }
}
private String bccEmail = "";
public String BccEmail
{
get { return bccEmail; }
set { bccEmail = value; }
}
private bool isHTML = false;
public bool IsHTML
{
get { return isHTML; }
set { isHTML = value; }
}
private String messageText;
public String MessageText
{
get { return messageText; }
set { messageText = value; }
}

private int port;
public int Port
{
get { return port; }
set { port = value; }
}

private String password="";
public String Password
{
get { return password; }
set { password = value; }
}


public bool sendMail()
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();

MailAddress fromAddress = new MailAddress(senderEmail, senderName);
smtpClient.Host = smtpServer;
smtpClient.Port = port;
message.From = fromAddress;
message.To.Add(recipientEmail);
message.Subject = subject;
if (!ccEmail.Equals(""))
{
message.CC.Add(ccEmail);
}
if (!bccEmail.Equals(""))
{
message.Bcc.Add(ccEmail);
}
message.IsBodyHtml = isHTML;
message.Body = messageText;
NetworkCredential info = new NetworkCredential(senderEmail, password);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = info;
smtpClient.EnableSsl = true;
try {
smtpClient.Send(message);
return true;
}
catch {
return false;
}
}
}

}


4.專案組態設定:專案->mySMTP屬性..
a.應用程式頁籤:選擇組件資訊->勾選「讓組件成為COM-Visible」
b.簽署頁籤:勾選「簽署組件」->下拉選擇「新增..」->取消勾選「以密碼保護我的金鑰檔」->金鑰檔名稱輸入「mySMTP」->按確定
c.建置事件頁籤:建置後事件命令列,點選「建置後進行編輯」按鈕輸入:(這是把組件加到GAC,並產生mySMTP.reg註冊為COM的檔案)

set dnf=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
set sdk=C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin
%dnf%\regasm $(TargetFileName) /regfile:$(TargetName).reg
"%sdk%\gacutil" /i "$(TargetPath)"

5.編譯mySMTP專案後,進行把組件註冊到COM,執行mySMTP.reg檔

=================================================
建立新的視窗應用程式專案進行mySMTP.dll測試:
1.將mySMTP.dll加入專案當參考
2.程式碼:

using System;
using System.Text;
using System.Windows.Forms;

namespace mySMTPDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
mySMTP.SMTP testSMTP = new mySMTP.SMTP();
testSMTP.SenderName = "test";
testSMTP.SenderEmail = "*****@gmail.com"; //寄件者
testSMTP.SmtpServer = "smtp.gmail.com";
testSMTP.Port = 587;
testSMTP.Password = "*****";
testSMTP.RecipientEmail = "*****@gmail.com"; //收件者
testSMTP.MessageText = "Hello .NET";
testSMTP.Subject = "Hello";
testSMTP.sendMail();
}
}
}


Delphi叫用.NET的mySMTP.SMTP類別
uses  ComObj;
...
procedure TForm1.Button1Click(Sender: TObject);
var
testSMTP:Variant;
begin
testSMTP:=CreateOleObject('mySMTP.SMTP');
testSMTP.SenderName := 'test';
testSMTP.SenderEmail := '*****@gmail.com';
testSMTP.SmtpServer := 'smtp.gmail.com';
testSMTP.Port := 587;
testSMTP.Password := '******';
testSMTP.RecipientEmail :='*****@gmail.com';
testSMTP.MessageText := 'Hello Delphi';
testSMTP.Subject := 'Hello';
testSMTP.sendMail;
showMessage('ok');
end;