当前位置 : 主页 > 编程语言 > delphi >

Delphi叫用.NET自订类

来源:互联网 收集:自由互联 发布时间:2021-06-23
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; namesp

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:WINDOWSMicrosoft.NETFrameworkv2.0.50727
set sdk=C:Program FilesMicrosoft Visual Studio 8SDKv2.0Bin
%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;

原文:大专栏  Delphi叫用.NET自订类

网友评论