Simple HTML Email vi SMTP

Started by PercivalOgg, July 23, 2013, 12:56:52 PM

Previous topic - Next topic

PercivalOgg

Here is an example function of how to send an email using the System.Net.Mail class if interested:

Code (winbatch) Select

#DefineFunction email_someone_( subject, body )

    msg = ObjectClrNew( "System.Net.Mail.MailMessage" )
    adr = ObjectClrNew( "System.Net.Mail.MailAddress", "from_person@email.address" )
    msg.From = adr

    msg.To.Add( "to_person@email.address" )

    affirmative = ObjectType( "BOOL", @TRUE )
    msg.Subject = subject
    msg.IsBodyHtml = affirmative
    msg.Body = body
    smtp = ObjectClrNew( "System.Net.Mail.SmtpClient" )
    smtp.Host = "smtp.host.server"

    smtp.Send( msg )

    smtp = 0
    adr = 0
    msg = 0

#EndFunction  ; email_someone_


Thanks!

Deana

Quote from: PercivalOgg on July 23, 2013, 12:56:52 PM
Here is an example function of how to send an email using the System.Net.Mail class if interested:

Code (winbatch) Select

#DefineFunction email_someone_( subject, body )

    msg = ObjectClrNew( "System.Net.Mail.MailMessage" )
    adr = ObjectClrNew( "System.Net.Mail.MailAddress", "from_person@email.address" )
    msg.From = adr

    msg.To.Add( "to_person@email.address" )

    affirmative = ObjectType( "BOOL", @TRUE )
    msg.Subject = subject
    msg.IsBodyHtml = affirmative
    msg.Body = body
    smtp = ObjectClrNew( "System.Net.Mail.SmtpClient" )
    smtp.Host = "smtp.host.server"

    smtp.Send( msg )

    smtp = 0
    adr = 0
    msg = 0

#EndFunction  ; email_someone_


Thanks!

Thank you for posting. However I am unable to get the code to run. I keep getting the error 1848 CLR Type Name Not Found on the line
Code (winbatch) Select
msg = ObjectClrNew( "System.Net.Mail.MailMessage" ). What Windows platform did you test on?
Deana F.
Technical Support
Wilson WindowWare Inc.

PercivalOgg

Hello Deana!

I am using a Windows 2008 server - I wonder if it doesn't work for you as I forgot
to include the ObjectUse statement. I will have a look tomorrow when I get back
to work and see what version/key I am using.

Thanks!


Deana

Yes the ObjectClrUse helps. This code seems to work:

Code (winbatch) Select
strHost = 'smtp.server.com'                                           ;!!!!!!!!! MODIFY TO FIT YOUR NEEDS !!!!!!!!
strTo = 'soandso@gmail.com'                                          ;!!!!!!!!! MODIFY TO FIT YOUR NEEDS !!!!!!!!
strFrom = 'soandso@server.com'                                     ;!!!!!!!!! MODIFY TO FIT YOUR NEEDS !!!!!!!!
strSubject = 'Sample dotNet Email'                                  ;!!!!!!!!! MODIFY TO FIT YOUR NEEDS !!!!!!!!
strBody = '<HTML><HEADER></HEADER><BODY>Hello World!</BODY></HTML>' ;!!!!!!!!! MODIFY TO FIT YOUR NEEDS !!!!!!!!
strUser = 'SoandSo'                                                        ;!!!!!!!!! MODIFY TO FIT YOUR NEEDS !!!!!!!!
strPassword = 'SuperSecret'                                            ;!!!!!!!!! MODIFY TO FIT YOUR NEEDS !!!!!!!!
nPort = ObjectType('I4', 25)                                                                      ;!!!!!!!!! MODIFY TO FIT YOUR NEEDS !!!!!!!!

ObjectClrOption ("use","System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")   
_TRUE = ObjectType( "BOOL", @TRUE )
_FALSE = ObjectType( "BOOL", @FALSE )

;NetworkCredential = ObjectClrNew( 'System.Net.NetworkCredential', strUser, strPassword)

MailAddress_From = ObjectClrNew( 'System.Net.Mail.MailAddress', strFrom )
MailAddress_To = ObjectClrNew( 'System.Net.Mail.MailAddress', strTo )

MailMessage = ObjectClrNew( 'System.Net.Mail.MailMessage', MailAddress_From, MailAddress_To )
MailMessage.IsBodyHtml = _True
MailMessage.Subject = strSubject
MailMessage.Body = strBody

SmtpClient = ObjectClrNew( 'System.Net.Mail.SmtpClient' )
SmtpClient.Host = strHost                   ; Specify SMTP Host
SmtpClient.Port = nPort                     ; Port Number
SmtpClient.UseDefaultCredentials = _TRUE   ; Use SSL
;SmtpClient.Credentials = NetworkCredential  ; Authentication
;SmtpClient.EnableSsl = _TRUE                ; Use SSL
SmtpClient.Send( MailMessage )              ; Send
Exit
Deana F.
Technical Support
Wilson WindowWare Inc.

PercivalOgg

Hello Deana!

I am glad you got it working - I know it was working for me  :)

The "use" statement I am using is:

Code (winbatch) Select

ObjectClrOption( "use", "System.Net, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a" )


Thanks!

kdmoyers

Thanks for posting that PercivalOgg, really appreciate it!
-Kirby
The mind is everything; What you think, you become.