viewpoint-particle

Author Topic: Simple HTML Email vi SMTP  (Read 5013 times)

PercivalOgg

  • Newbie
  • *
  • Posts: 4
Simple HTML Email vi SMTP
« 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
#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

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: Simple HTML Email vi SMTP
« Reply #1 on: July 23, 2013, 02:26:55 pm »
Here is an example function of how to send an email using the System.Net.Mail class if interested:

Code: Winbatch
#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
msg = ObjectClrNew( "System.Net.Mail.MailMessage" )
. What Windows platform did you test on?
Deana F.
Technical Support
Wilson WindowWare Inc.

PercivalOgg

  • Newbie
  • *
  • Posts: 4
Re: Simple HTML Email vi SMTP
« Reply #2 on: July 23, 2013, 03:15:33 pm »
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

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: Simple HTML Email vi SMTP
« Reply #3 on: July 23, 2013, 03:27:52 pm »
Yes the ObjectClrUse helps. This code seems to work:

Code: Winbatch
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

  • Newbie
  • *
  • Posts: 4
Re: Simple HTML Email vi SMTP
« Reply #4 on: July 24, 2013, 04:55:44 am »
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
ObjectClrOption( "use", "System.Net, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a" )
 

Thanks!

kdmoyers

  • Sr. Member
  • ****
  • Posts: 280
Re: Simple HTML Email vi SMTP
« Reply #5 on: July 24, 2013, 10:38:10 am »
Thanks for posting that PercivalOgg, really appreciate it!
-Kirby
The mind is everything; What you think, you become.