Detailed steps for calling the c # class library in vb and asp are as follows (javeEYE)

Source: Internet
Author: User
Tags mailmessage smtpclient

The DLL generated on the. NET platform using the C # language is msil-Based Binary Code. Generally, it cannot be called by native languages and programs (such as VB, Vc, and Delphi ). However, native programs on Windows can directly use COM objects. Some tools provided by the. NET platform can forge a COM interface for it and register it in the system. In this case, you can use the pseudo COM interface in the native program to call the. net dll.

1. Use C # To create. Net DLL

1.1 create a project

Create a new project in. Net: testcom.

1.2 implement C # class

Add an interface and a class to the class1.cs file:

Using system;

Using system. Collections. Generic;

Using system. text;

Using system. runtime. interopservices;

 

Namespace testcom

{

[Comvisible (true)]

Public interface iclass1

{

String test ();

}

 

[Comvisible (true)]

[Classinterface (classinterfacetype. autodual)]

Public class class1: iclass1

{

Public String test ()

{

Return "OK ";

}

 

}

}

 

Note that attributes on interfaces and classes are visible to and generated for com.

1.3 Add a strong name

Select the vistual studio command prompt under vistual Studio Tools under the vistual studio directory in the Start Menu. Use Sn-k c:/mykey. SNK to generate the signature file.

Right-click the project, click Properties, select Signature, select the Assembly signature, and select the mykey. SNK file.

 

1.4 generate a solution

You can find TestCom. dll in/TestCom/bin/Debug of the project directory. So far, the. net dll generated by c # has been completed.

 

2 generate and register a Type Library

 

2.1 generate a tlb Library

In the Visual Studio command prompt, switch to this directory.

Enter tlbexp TestCom. dll/out: TestCom. tlb. A message is displayed, indicating that the tlb library file is exported successfully.

 

2.2 register a Type Library

Enter regasm TestCom. dll/tlb: TestCom. tlb/codebase to import the Type Library to the Registry. A message indicating that the type is successfully registered indicates that the operation is successful. In this case, TestCom. dll can be used as a com.

 

2.3 add dll to GAC

Enter gacutil/I TestCom. dll to add this. net assembly to GAC.

 

3 vb call Test

3.1 create a vb Project

Create a standard exe project in vb.

Select reference from the project menu, and find TestCom in the pop-up reference com Object List.

Drag and Drop a button on the form, double-click the button, and enter the code in the button event:

Dim a As New TestCom. Class1

Msgbox A. Test

 

 

3.2 run the program

Start the project and click the button to bring up the dialog box "OK", indicating that the call is successful.

 

 

4 ASP call Test

4.1 create an ASP file

Create a text file in the root directory of IIS and change it to 1.asp. enter the following content:

<%

Set S = Createobject ("testcom. class1 ")

Response. Write (S. Test ())

%>

 

 

4.2 run the program

Access this file in IE, and the page output is OK, indicating that the call is successful.

 

 

 

 

Solution 2: no trial

 

Using system; <br/> using system. net. mail; <br/> using system. text; </P> <p> namespace imels <br/>{< br/> public class sendmail <br/>{< br/> Public Sendmail () {}</P> <p> private string _ to = string. empty; </P> <p> /// <summary> <br/> // The recipient address. Use ", <br/> /// </Summary> <br/> Public string to <br/>{< br/> set {_ to = value ;} <br/>}</P> <p> private string _ from = string. empty; </P> <p> // <summa Ry> <br/> // sender address <br/> /// </Summary> <br/> Public String from <br/>{< br/> set {_ from = value ;} <br/>}</P> <p> private string _ fromname = string. empty; </P> <p> /// <summary> <br/> // sender display name <br/> /// </Summary> <br/> Public String fromname <br/>{< br/> set {_ fromname = value ;} <br/>}</P> <p> private string _ cc = string. empty; </P> <p> /// <summary> <br/> // cc. Use ", separated by commas (,) <br/> /// </s Ummary> <br/> Public String CC <br/>{< br/> set {_ cc = value ;} <br/>}</P> <p> private string _ BCC = string. empty; </P> <p> /// <summary> <br/> /// password copy. Use ", <br/> /// </Summary> <br/> Public String BCC <br/>{< br/> set {_ BCC = value ;} <br/>}</P> <p> private string _ charset = "gb2312 "; </P> <p> // <summary> <br/> // encode the email body <br/> /// </Summary> <br/> Public string charset <br/>{< br/> S Et {_ charset = value ;}< br/>}</P> <p> private string _ contenttype = "html "; <br/> /// <summary> <br/> // email format (HTML or TXT) <br/> // </Summary> <br/> Public String contenttype <br/>{< br/> set {_ contenttype = value ;} <br/>}</P> <p> private string _ subject = string. empty; <br/> /// <summary> <br/> // email title <br/> /// </Summary> <br/> Public String subject <br/ >{< br/> set {_ subject = Value ;}< Br/>}</P> <p> private string _ body = string. empty; <br/> /// <summary> <br/> // email content <br/> /// </Summary> <br/> Public String body <br/ >{< br/> set {_ body = value ;} <br/>}</P> <p> private string _ SMTP; <br/> /// <summary> <br/> // SMTP server address <br/> /// </Summary> <br/> Public String SMTP <br />{< br/> set {_ SMTP = value ;} <br/>}</P> <p> private string _ username; <br/> // <summary> <br />/// SMTP username <br/> /// </Summary> <br/> Public String username <br/>{< br/> set {_ username = Value ;} <br/>}< br/> /// <summary> <br/> // SMTP password <br/> /// </Summary> <br/> private string _ password; </P> <p> Public String password <br/>{< br/> set {_ password = value ;} <br/>}</P> <p> private int _ Port = 25; <br/> // <summary> <br/> // SMTP product <br/> /// </Summary> <br/> Public int port <br />{< Br/> set {_ Port = value ;} <br/>}</P> <p> // <summary> <br/> // send <br/> /// </Summary> <br/> Public void send () <br/>{< br/> mailaddress from = new mailaddress (_ from, _ fromname); <br/> mailmessage message = new mailmessage (); <br/> message. from = from; </P> <p> string [] toadd = _. split (','); <br/> foreach (string _ add in toadd) <br/>{< br/> try <br/>{< br/> message. to. add (New mailad Dress (_ add); <br/>}< br/> catch (exception e) <br/>{< br/> _ error + = "to address error: "+ E. message + "(" + _ add + ");"; <br/>}</P> <p> If (_ CC! = String. empty) <br/>{</P> <p> string [] ccadd = _ CC. split (','); </P> <p> foreach (string _ add in ccadd) <br/>{< br/> try <br/>{< br/> message. cc. add (New mailaddress (_ add); <br/>}< br/> catch (exception E) <br/>{< br/> _ error + = "CC address error:" + E. message + "(" + _ add + ");"; <br/>}< br/> If (_ BCC! = String. empty) <br/>{< br/> string [] bccadd = _ BCC. split (','); </P> <p> foreach (string _ add in bccadd) <br/>{< br/> try <br/>{< br/> message. BCC. add (New mailaddress (_ add); <br/>}< br/> catch (exception E) <br/>{< br/> _ error + = "BCC address error:" + E. message + "(" + _ add + ");"; <br/>}</P> <p> message. sender = from; <br/> message. subject = _ subject; <br/> message. body = _ body; </P> <p> If (_ contenttype = "html" | _ contenttype = string. empty) <br/>{< br/> message. isbodyhtml = true; <br/>}< br/> else <br/>{< br/> message. isbodyhtml = false; <br/>}</P> <p> message. bodyencoding = encoding. getencoding (_ charset); <br/> message. deliverynotificationoptions = deliverynotificationoptions. none; <br/> smtpclient _ SMTP = new smtpclient (); <br/> _ SMTP. host = _ SMTP; <br/> _ SMTP. port = _ port; <br/> _ SMTP. usedefaultcredentials = false; <br/> _ SMTP. credentials = new system. net. networkcredential (_ username, _ password); <br/> _ SMTP. deliverymethod = smtpdeliverymethod. network; <br/> try <br/> {<br/> _ SMTP. send (Message); <br/>}< br/> catch (smtpexception e) <br/>{< br/> _ error + = "SMTP error:" + E. message + ";"; <br/>}</P> <p> private string _ error = string. empty; <br/> /// <summary> <br/> // error message returned <br/> /// </Summary> <br/> Public string error <br />{< br/> get {return _ error ;} <br/>}< br/> /// <summary> <br/> // clear the error message <br/> /// </Summary> <br/> public void clearerr () <br/>{< br/> _ error = string. empty; <br/>}< br/>}

 

Let's talk about the specific implementation process:

1. First, create a class library project. On the project properties page, set the Assembly name to "IMELS" in the "application" tab (of course, you can set this name as your favorite ), the output type is a class library,

Click "assembly information" and select "make assembly COM visible ",

2. Click the "sign" tab and select "sign for program ",

Then select the key file from the drop-down list of "select strong name key file". If no key file exists, select "new". Here I choose "New,

Enter the key name in the "key file name" column. You can choose to add a password for the key to protect it. I have not used a password here.

Then add a class "SendMail" for the project, and the code is as above.

3. After the code is complete, generate the DLL file and put the DLL on D: disk or another disk, but it is best not to place it on the system disk, and then register it, you cannot use regsvr32 to register a DLL written in C #. You must use regasm in the format of regasm/codebase d:/DLL/IMELS. dll.

In this way, the DLL compilation and registration have been completed, and the following is the application. The method of calling asp is as follows:

 

VB Code
  1. <%
  2. Dim send
  3. Set send = Server. CreateObject ("IMELS. SendMail ")
  4. Send. From = "test@163.com"
  5. Send. FromName = "No questions"
  6. Send. Smtp = "smtp.163.com"
  7. Send. Username = "User Name"
  8. Send. Password = "Password"
  9. Send. Subject = "asp call C # write DLL send mail test title"
  10. Send. ContentType = "html"
  11. Send. Charset = "gb2312"
  12. Send. Body = "asp calls C # compiled DLL to send mail test Body"
  13. Send. To = "to@163.com"
  14. Send. CC = "CC address"
  15. Send. BCC = "BCC address"
  16. Send. Send ()
  17. Response. Write (send. Error)
  18. %>

<% <Br/> dim send <br/> set send = Server. createObject ("IMELS. sendMail ") </p> <p> send. from = "test@163.com" <br/> send. fromName = "NONE" <br/> send. smtp = "smtp.163.com" <br/> send. username = "Username" <br/> send. password = "Password" <br/> send. subject = "asp call C # write DLL send mail test title" <br/> send. contentType = "html" <br/> send. charset = "gb2312" <br/> send. body = "asp call C # write DLL send mail test Body" <br/> send. to = "to@163.com" <br/> send. CC = "CC address" <br/> send. BCC = "BCC address" <br/> send. send () <br/> Response. write (send. error) <br/> %>

 

All right, you're done !!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.