Jump to content

Calling external VB.net dll with Send Email giving error when using DotNetAll.au3 - Loading of vb code and then call function to


IndianSage
 Share

Recommended Posts

Hi,

I am using external VB.net small program which allows me to send email using outside my network.

My external code is tested and is working fine from vb.net form.

On first step as given in earlier my activity - I am loading vbcode - it gives error:

Net.NetworkCredentials not define.

I am setting up credentials in side the Dll function called sendEmail with params inputs of credentials, smpt server address etc.

During loading step itself this error comes up.

Using same this dll creation technique- created other dlls -  I called those functions in other dlls from AutoIt script - they works fine. 

Can anyone help who has tried this out - calling external smtp server to send emails using vb.net.

Thanks in advance.

 

Link to comment
Share on other sites

Just would like you to consider again the following observation:

This same dll works fine when called from other vb.net program and we are able to send the email however when I call it thru AutoIt wrapper code in DotNetAll.aue3 does not work and give the said error even in loading the code itself.   

Link to comment
Share on other sites

Below is au3 code:

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <Array.au3>
#include <Excel.au3>

#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StringConstants.au3>
#include <WinAPIConv.au3>
#include <DotNetAll.au3>

;-------send email using vb src code and Dll

sendEmail3()

Func sendEmail3()
   Local $oCode = DotNet_LoadVBcode( FileRead( "Class1.vb" ), "clsSendEmail.dll" ) ; You must add the "System.Windows.Forms.dll"

   If @error Then
         Return ConsoleWrite( "DotNet_LoadVBcode ERR" & @CRLF )                   ; assembly because of line 1 in the VB code.
         Exit
   EndIf
   ConsoleWrite( "DotNet_LoadVBcode OK" & @CRLF )
;      Exit
   Local $iRes
   Local $oClsSendEmail = DotNet_CreateObject( $oCode, "Class1" ) ; $oFoo is an object of the "Foo" class in the VB code
      If IsObj( $oClsSendEmail  ) Then
         $oClsSendEmail.SendEmail("my email address","my passwrod", "Testing dll with autoit...", "Hi, This is test msg...", "ToEmailAddress", "SMTPServerAddress", "" ,"587", "my email id")                ; Test is a method (function) of the $oFoo object
         Sleep(5000)
         MsgBox(0,"result","Message sent")
      EndIf


EndFunc

 

Here is the vb.net code:

Imports System.Net.Mail
Imports System.Net

Public Class Class1
    Public myCredentials As Net.NetworkCredential
    Public Smtp_Server As New SmtpClient
    Public e_mail As New MailMessage()

    Public Function SendEmail(ByVal SmtpUserId As String, ByVal SmtpPswd As String, ByVal EmailSubj As String, ByVal EmailBody As String _
                               , ByVal EmailTo As String, ByVal IpAddr As String, ByVal EmailServer As String, ByVal Port As String, ByVal FromEmailId As String) As Integer


        '        Dim Smtp_Server As New SmtpClient
        '        Dim e_mail As New MailMessage()

        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential(SmtpUserId, SmtpPswd)
        Smtp_Server.Port = Port

        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = IpAddr

        e_mail = New MailMessage()
        e_mail.From = New MailAddress(FromEmailId)

        e_mail.To.Add(EmailTo)
        e_mail.Subject = EmailSubj

        e_mail.IsBodyHtml = False
        e_mail.Body = EmailBody
        Smtp_Server.Send(e_mail)


        Return 0

    End Function


End Class

I hope this will help in clarifying the matter. IN case more info is missing please do let me know.

Awaiting your reply.

Thanks in advance.

 

Edited by IndianSage
Missing clarification.
Link to comment
Share on other sites

You also need to add references to System.Net and System.Net.Mail. Use this code:

Local $oCode = DotNet_LoadVBcode( FileRead( "Class1.vb" ), "System.Net.dll | System.Net.Mail.dll | clsSendEmail.dll" )

 

Link to comment
Share on other sites

You should not copy System.Net.dll or System.Net.Mail.dll anywhere. They are included in the Global Assembly Cache. You just need to tell the code that it needs these dlls.

Use this code to make Net.NetworkCredential work:

tst00.au3

#include "DotNetAll.au3"

sendEmail3()

Func sendEmail3()
  Local $oCode = DotNet_LoadVBcode( FileRead( "tst00.vb" ), "System.dll | System.Net.dll | netstandard.dll" )
  If Not IsObj( $oCode ) Then Return ConsoleWrite( "DotNet_LoadVBcode ERR" & @CRLF )
  ConsoleWrite( "DotNet_LoadVBcode OK" & @CRLF )
EndFunc

tst00.vb

Imports System
Imports System.Net
Imports netstandard

Public Class Class1
  Public myCredentials As Net.NetworkCredential
End Class

Read Microsoft documentation to find out what imports and dlls you need. And just add one or a few lines at a time until you have all the imports and dlls in place and the code works so far.

And test code in SciTE so you get all error messages in the console.

Link to comment
Share on other sites

THe problem is the following AutoIt code is giving error as mentioned. Let me reiterate:

Local $oCode = DotNet_LoadVBcode( FileRead( "tst00.vb" ), "System.dll | System.Net.dll | netstandard.dll" )

Multiple dll mentioned is not working at all with pipe character (|).

I get following error on run/exec:

BC2017: could not find library 'System.Net.dll ?' 

Even though the files are there in the @scriptdir - all 3 dlls.

Can you give me fix for this issue?

Thanks in advance.

In parallel, I am trying what you have suggested too and revert with the results.

Edited by IndianSage
To Complete info.
Link to comment
Share on other sites

There have been some problems loading dlls that are not registered in the Global Assembly Cache. Eg. in this and the following posts. It seems like the methods used to load dlls look for them in the wrong directories. You can try specifying the dlls with full path. You can also try copying the dlls to the AutoIt installation directory where AutoIt3.exe and AutoIt3_x64.exe are located. AutoIt3.exe or AutoIt3_x64.exe is the running executable when scripts are run from SciTE. You can analyze the problem with Fuslogvw.exe (Assembly Binding Log Viewer), but it is not quite easy.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...