Author Topic: Problems with Send Email with Powershell code  (Read 340 times)

0 Members and 1 Guest are viewing this topic.

PKENEWELL

  • Bull Frog
  • Posts: 319
Problems with Send Email with Powershell code
« on: February 02, 2024, 09:43:08 AM »
Hi All,

I was inspired by the last post on sending an email with outlook, to use Powershell and Gmail as an alternate method, but I cannot get this to work! I have been playing around with this code since yesterday and I'm about to give up on it. NOTE: in my test function, I stripped out the actual username, password, emails, etc that I used for obvious reasons.

Perhaps some Powershell expert on here might know why it wasn't working for me?

Code - Auto/Visual Lisp: [Select]
  1. (defun Sendmail2 (usr pwd me you sub msg / *error* cmd dom exec fl rlst wscript)
  2.    
  3.    (defun *error* (msg)
  4.       (vl-bt)
  5.       (if fl (close fl))
  6.    )
  7.    
  8.    (setq dom "smtp.gmail.com"
  9.          rlst
  10.             (list
  11.                "@ECHO OFF"
  12.                "CALL :PowerShell"
  13.                "CD /D C:\\Windows\\System32\\WindowsPowerShell\\v1.0"
  14.                "Powershell -ExecutionPolicy Bypass -Command \"& '%CreateScript%'\""
  15.                "EXIT\n"
  16.                ":PowerShell"
  17.                "SET CreateScript=%temp%\\~tempEmailScript.ps1"
  18.                "IF EXIST \"%CreateScript%\" DEL /Q /F \"%CreateScript%\""
  19.                "ECHO                                          >> \"%CreateScript%\""
  20.                (strcat "ECHO $Username    = \"" usr "\"       >> \"%CreateScript%\"")
  21.                (strcat "ECHO $Password    = \"" pwd "\"       >> \"%CreateScript%\"")
  22.                (strcat "ECHO $EmailTo     = \"" you "\"       >> \"%CreateScript%\"")
  23.                (strcat "ECHO $EmailFrom   = \"" me  "\"       >> \"%CreateScript%\"")
  24.                (strcat "ECHO $Subject     = \"" sub "\"       >> \"%CreateScript%\"")
  25.                (strcat "ECHO $Body        = \"" msg "\"       >> \"%CreateScript%\"")
  26.                (strcat "ECHO $SMTPServer  = \"" dom "\"       >> \"%CreateScript%\"")
  27.                "ECHO $SMTPClient  = New-Object Net.Mail.SmtpClient($SmtpServer, 587)                            >> \"%CreateScript%\""   ; Ports 587, 25, or 465 for gmail
  28.                "ECHO $SMTPClient.EnableSsl = $true                                                              >> \"%CreateScript%\""
  29.                "ECHO $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)    >> \"%CreateScript%\""
  30.                "ECHO $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom, $Password)   >> \"%CreateScript%\""
  31.                "ECHO $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)                                    >> \"%CreateScript%\""
  32.                "GOTO :EOF"
  33.             )
  34.    )
  35.    
  36.    (setq fl (open (strcat (getvar "tempprefix") "SendMail.BAT") "w"))
  37.    (foreach n rlst (write-line n fl))
  38.    (close fl)
  39.    (if (setq wscript (vlax-create-object "WScript.Shell")
  40.              cmd     (findfile (strcat (getvar "tempprefix") "SendMail.BAT"))
  41.        )
  42.      (progn
  43.            (setq exec (vlax-invoke wscript 'Exec cmd))
  44.            (while (and exec (= 0 (vlax-get-property exec 'Status)))
  45.                   (princ (strcat "\rWaiting for Program \"" cmd "\"..."))
  46.            )
  47.            (mapcar 'vlax-release-object (list exec wscript))
  48.      )
  49.    )
  50.    (princ)
  51. )
  52.  
  53. (defun c:testit ()
  54.    (sendmail2
  55.       "(My Username)"
  56.       "(My Password)"
  57.       "(My Email)"
  58.       "(Your Email)"
  59.       "TEST"
  60.       "This is a test message."
  61.    )
  62. )
  63.  

BONUS Challenge: I was also attempting to play around with using CDO, but Haven't figured out how (or if) I can setup the Credentials in the Schema fields:
Code - Auto/Visual Lisp: [Select]
  1. (defun SendEmail (me you sub msg / mobj)
  2.    (if (setq mobj (vlax-create-object "CDO.message"))
  3.       (progn
  4.          (vlax-put mobj 'From me)
  5.          (vlax-put mobj 'To you)
  6.          (vlax-put mobj 'Subject sub)
  7.          (vlax-put mobj 'TextBody msg)
  8. ;;          (vlax-for n (vlax-get (vlax-get mobj 'Configuration) 'Fields)
  9. ;;              (vlax-put n )
  10. ;;          )
  11.          (vlax-invoke mobj 'Send)
  12.          (vlax-release-object mobj)
  13.       )
  14.    )
  15. )
  16.  
« Last Edit: February 02, 2024, 09:52:11 AM by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Problems with Send Email with Powershell code
« Reply #1 on: February 02, 2024, 11:33:07 AM »
Haven't looked at your code and I'm not sure if this will be the right rabbit hole to go down but gmail is a pain on the SMTP side (they changed something a few years back). I had a lot of fun fixing the email feature here on theSwamp server. I think it found I needed port 465 at ssl://smtp.gmail...

MS Exchange related: Also, I had some problems with email automation once (CAD Mgr stuff) and instead of using the <username>@domain@smtp.office... url I just used the same SMTP URL as one of the printers in the office (I didn't tell any of the bosses--or IT--about how I got that stuff to work though! :) And I imagine if that company ever replaced me, my predecessor may have been a bit nervous if (s)he found that little gem. haha!). I just sent something to myself from a printer and tracked down the URL from the email headers.

-i.e. your problem is probably your password (the port you send with) and ssl. Try port 465 because I think it starts ssl before authentication. If I remember, 587 starts ssl after auth.

Good luck.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

PKENEWELL

  • Bull Frog
  • Posts: 319
Re: Problems with Send Email with Powershell code
« Reply #2 on: February 02, 2024, 12:49:49 PM »
Haven't looked at your code and I'm not sure if this will be the right rabbit hole to go down but gmail is a pain on the SMTP side (they changed something a few years back). I had a lot of fun fixing the email feature here on theSwamp server. I think it found I needed port 465 at ssl://smtp.gmail...

MS Exchange related: Also, I had some problems with email automation once (CAD Mgr stuff) and instead of using the <username>@domain@smtp.office... url I just used the same SMTP URL as one of the printers in the office (I didn't tell any of the bosses--or IT--about how I got that stuff to work though! :) And I imagine if that company ever replaced me, my predecessor may have been a bit nervous if (s)he found that little gem. haha!). I just sent something to myself from a printer and tracked down the URL from the email headers.

-i.e. your problem is probably your password (the port you send with) and ssl. Try port 465 because I think it starts ssl before authentication. If I remember, 587 starts ssl after auth.

Good luck.

Thanks for your insights John! I did try port 465 (to no avail), but I hadn't tried changing the server to prefix with "ssl://smtp.gmail.com". I'll keep playing, but it was just one of those "can I do it" things :)
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt