Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


lchiu7

6521 posts

Uber Geek
+1 received by user: 543

Trusted

#260051 8-Nov-2019 10:44
Send private message

I spend some time helping a friend with a small office setup. a number of bat files run every night that back up their important to a NAS and the NAS is synchronised to OneDrive It would be good if at the completion of the execution of those bat files an email could be sent to me so I can look at the output from the backups.

Trying to find an easy way for a bat file to send an email. They are a Gmail user and I have not been able to find an easy way to access Google's SMTP server. But their internet connection is provided by spark so I thought to be able to access the spark SMTP server shouldn't I.?

In a previous site I was able to use a little freeware tool called blat and access the exchange server on the internal Network but this little operation doesn't have its own SMTP server.

Any ideas appreciated.

Thanks




Staying in Wellington. Check out my AirBnB in the Wellington CBD.  https://www.airbnb.co.nz/h/wellycbd  PM me and mention GZ to get a 15% discount and no AirBnB charges.


Filter this topic showing only the reply marked as answer View this topic in a long page with up to 500 replies per page Create new topic
 1 | 2
Dynamic
4016 posts

Uber Geek
+1 received by user: 1852

ID Verified
Trusted
Lifetime subscriber

  #2350091 8-Nov-2019 10:49
Send private message

Spark often block outgoing Port 25...  can you test with Telnet or similar?

 

telnet smtpservername 25

 

For example if I telnet to smtp.gmail.com, I get a response of 220 smtp.gmail.com ESMTP r24sm3965098pgu.36 - gsmtp

 

I've used and like blat.  There are alternatives as well.





“Don't believe anything you read on the net. Except this. Well, including this, I suppose.” Douglas Adams




SirHumphreyAppleby
2939 posts

Uber Geek
+1 received by user: 1862


  #2350093 8-Nov-2019 10:53
Send private message

I maintain CMail so naturally I would recommend that as an alternative to Blat. Blat unfortunately does not support SSL, which is required for sending via Google and many other services. You can use Blat with a third party SSL tool, but you're limited to sending via SMTPS, which is considered deprecated.

 

To use GMail servers, you will need to authenticate, which usually involves allowing less secure applications in your Google account (don't worry, these are still secure), or application passwords. You should also be sending via the message submission port, 587.

 

Note, if you setup your own server, you need to setup SPF, DKIM and ensure your ISP isn't blocking port 25. It's a lot more work than is necessary just to send mail form a batch file.

 

You can also use PowerShell to send messages without any third party software installed.


elpenguino
3577 posts

Uber Geek
+1 received by user: 2939


  #2350095 8-Nov-2019 10:57
Send private message

If you've got Office on the machine you should be able to script it up to send an email easily enough. I do a similar thing from Access.





Most of the posters in this thread are just like chimpanzees on MDMA, full of feelings of bonhomie, joy, and optimism. Fred99 8/4/21




MurrayM
2502 posts

Uber Geek
+1 received by user: 742

ID Verified
Trusted
Lifetime subscriber

  #2350096 8-Nov-2019 10:57
Send private message

I was going to suggest using Blat. I used it many years ago and found it worked fine.


Spyware
3818 posts

Uber Geek
+1 received by user: 1366

Lifetime subscriber

  #2350100 8-Nov-2019 11:03
Send private message

Run Postfix on an RPi to relay to Spark.





Spark Max Fibre using Mikrotik CCR1009-8G-1S-1S+, CRS125-24G-1S, Unifi UAP, U6-Pro, UAP-AC-M-Pro, Apple TV 4K (2022), Apple TV 4K (2017), iPad Air 1st gen, iPad Air 4th gen, iPhone 13, SkyNZ3151 (the white box). If it doesn't move then it's data cabled.


tchart
2397 posts

Uber Geek
+1 received by user: 579

ID Verified
Trusted

  #2350103 8-Nov-2019 11:07
Send private message

I usually just use Gmails SMTP servers. You do need to configure the account to use it though.

 

I use the following code in PowerShell to send an email via Gmail;

 

function Send-ToEmail([string]$email, [string]$subject, [string]$body)
{
    try
    {
        $message = new-object Net.Mail.MailMessage
        $message.From = "XYZ@gmail.com"
        $message.ReplyTo = "XYZ@gmail.com"
        $message.To.Add($email)
        $message.Subject = $subject
        $message.Body = $body

 

        $smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587")
        $smtp.EnableSSL = $true;
        $smtp.Credentials = New-Object System.Net.NetworkCredential("XYZ@gmail.com", "PASSWORDGOESHERE");

 

        $smtp.send($message)

 

        $result = $true
    }
    catch
    {
        $result = $false
    }

 

    return $result
 }

 

 


 
 
 

Shop on-line at New World now for your groceries (affiliate link).
SirHumphreyAppleby
2939 posts

Uber Geek
+1 received by user: 1862


  #2350106 8-Nov-2019 11:11
Send private message

Spyware:

 

Run Postfix on an RPi to relay to Spark.

 

 

There is no real benefit running an SMTP server delivering to a smart host over having the client deliver to the smart host directly. The biggest drawback with running a server is if mail delivery fails, you may not know about it for a long time. If your batch file handles this directly, it can take appropriate action when failures occur.


davidcole
6099 posts

Uber Geek
+1 received by user: 1465

Trusted

  #2350115 8-Nov-2019 12:06
Send private message

Yeah I had a powershell that would send via gmail.  But with a local smtp server you can do it without requiring smtp authentication.

 

 





Previously known as psycik

Home Assistant: Gigabyte AMD A8 Brix, Home Assistant with Aeotech ZWave Controller, Raspberry PI, Wemos D1 Mini, Zwave, Shelly Humidity and Temperature sensors
Media:Chromecast v2, ATV4 4k, ATV4, HDHomeRun Dual
Server
Host Plex Server 3x3TB, 4x4TB using MergerFS, Samsung 850 evo 512 GB SSD, Proxmox Server with 1xW10, 2xUbuntu 22.04 LTS, Backblaze Backups, usenetprime.com fastmail.com Sharesies Trakt.TV Sharesight 


fearandloathing
537 posts

Ultimate Geek
+1 received by user: 206

ID Verified
Lifetime subscriber

  #2350132 8-Nov-2019 12:54
Send private message

Send-MailMessage is native to PowerShell

 

 

 

$mycredentials = Get-Credential
#$MailServer="your-domain-co-nz.mail.protection.outlook.com"
$MailServer="smtp.office365.com"
#From Address, does not need to be actual mailbox
$Sender="hello.there@your-domain.co.nz"
#Recipients
#$Recipient=@("hello.there@your-domain.co.nz")
$Recipient=@("hello.there@your-domain.co.nz","hello.there2@your-domain.co.nz")

 

Send-MailMessage -From $Sender -To $Recipient -Subject "Testing sending attachment 21" `
    -SmtpServer $MailServer -Body "this message should include an attachment" -Credential $mycredentials -UseSsl

 

    <#
    -Attachments .\test.txt
    -Priority High
    #>


lchiu7

6521 posts

Uber Geek
+1 received by user: 543

Trusted

  #2350226 8-Nov-2019 13:56
Send private message

Thanks all. Powershell looks like the way to go. But I'm not experienced in Powershell. The function that tchart documents looks good but I'm entirely sure how to call it. So I create the script as a standalone script that I can call say from a bat file ? The bat file would be doing all my copying etc.




Staying in Wellington. Check out my AirBnB in the Wellington CBD.  https://www.airbnb.co.nz/h/wellycbd  PM me and mention GZ to get a 15% discount and no AirBnB charges.


lchiu7

6521 posts

Uber Geek
+1 received by user: 543

Trusted

  #2350452 8-Nov-2019 20:21
Send private message

tchart:

 

I usually just use Gmails SMTP servers. You do need to configure the account to use it though.

 

I use the following code in PowerShell to send an email via Gmail;

 

function Send-ToEmail([string]$email, [string]$subject, [string]$body)
{
    try
    {
        $message = new-object Net.Mail.MailMessage
        $message.From = "XYZ@gmail.com"
        $message.ReplyTo = "XYZ@gmail.com"
        $message.To.Add($email)
        $message.Subject = $subject
        $message.Body = $body

 

        $smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587")
        $smtp.EnableSSL = $true;
        $smtp.Credentials = New-Object System.Net.NetworkCredential("XYZ@gmail.com", "PASSWORDGOESHERE");

 

        $smtp.send($message)

 

        $result = $true
    }
    catch
    {
        $result = $false
    }

 

    return $result
 }

 

 

 

 

 

 

OK this one looked like a great example. I replaced all the fields with my own, the SMTP server not Gmail but my own domain's one on port 465. The SMTP server requires authentication and I am sure I put in the right email address and password (checked it several times)

 

But whenever I run the script it always returns false and no mail is sent.

 

I just invoked the script in the Powershell script by having

 

Send-ToEmail("send to address", "subject line", "some random text in the body")

 

 

 

Not entirely sure what to do now or how to even debug this.





Staying in Wellington. Check out my AirBnB in the Wellington CBD.  https://www.airbnb.co.nz/h/wellycbd  PM me and mention GZ to get a 15% discount and no AirBnB charges.


 
 
 

Support Geekzone with one-off or recurring donations Donate via PressPatron.
davidcole
6099 posts

Uber Geek
+1 received by user: 1465

Trusted

  #2350497 8-Nov-2019 22:39
Send private message

Take out the try and catch lines while you’re figuring it out.  As it’s getting an error somewhere, but the try catch is hiding it from you.





Previously known as psycik

Home Assistant: Gigabyte AMD A8 Brix, Home Assistant with Aeotech ZWave Controller, Raspberry PI, Wemos D1 Mini, Zwave, Shelly Humidity and Temperature sensors
Media:Chromecast v2, ATV4 4k, ATV4, HDHomeRun Dual
Server
Host Plex Server 3x3TB, 4x4TB using MergerFS, Samsung 850 evo 512 GB SSD, Proxmox Server with 1xW10, 2xUbuntu 22.04 LTS, Backblaze Backups, usenetprime.com fastmail.com Sharesies Trakt.TV Sharesight 


lchiu7

6521 posts

Uber Geek
+1 received by user: 543

Trusted

  #2350506 8-Nov-2019 23:52
Send private message

Well persistence and I made progress. Found a script like that I was able to use

 

$EmailFrom = "xxxx@outlook.com"

 

$EmailTo = "yyyy@gmail.com"

 

$Subject = "Test from O365 SMTP "

 

$Body = "Test from O365 SMTP Authentication"

 

$SMTPServer = "smtp.office365.com"

 

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)

 

$SMTPClient.EnableSsl = $true

 

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("user@outlook.com", "password");

 

$SMTPClient.Send($EmailTo, $EmailFrom, $Subject, $Subject)

 

 

 

That seemed to work but for the machine I was using I had to alter the Powershell execution policies.

 

Next step is to try to work out how to attach files.  

 

 

 

 





Staying in Wellington. Check out my AirBnB in the Wellington CBD.  https://www.airbnb.co.nz/h/wellycbd  PM me and mention GZ to get a 15% discount and no AirBnB charges.


ANglEAUT
altered-ego
2436 posts

Uber Geek
+1 received by user: 842

Trusted
Lifetime subscriber

  #2350515 9-Nov-2019 03:51
Send private message

lchiu7:

 

tchart:

 

I usually just use Gmails SMTP servers. You do need to configure the account to use it though. ...

 

... The SMTP server requires authentication and I am sure I put in the right email address and password (checked it several times) ...

 

Enable less secure apps in the Google / Outlook.com account if you have 2FA enabled.





Please keep this GZ community vibrant by contributing in a constructive & respectful manner.


ANglEAUT
altered-ego
2436 posts

Uber Geek
+1 received by user: 842

Trusted
Lifetime subscriber

  #2350516 9-Nov-2019 04:04
Send private message

lchiu7:

 

... Next step is to try to work out how to attach files.  

 

As fearandloathing said, the Send-MailMessage cmdlet is supported natively in PowerShell and has the -Attachments parameter for exactly this purpose.

 

Or you can have Stack Overflow tell you (9 years ago).





Please keep this GZ community vibrant by contributing in a constructive & respectful manner.


 1 | 2
Filter this topic showing only the reply marked as answer View this topic in a long page with up to 500 replies per page Create new topic








Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.