|
Forum Index : Microcontroller and PC projects : Send emails from the WebMite using your gmail account
| Author | Message | ||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11551 |
Here is how to use the WebMite RP2350 (6.03.00RC25 or above) to send emails from your gmail account. The example code below attaches a jpg image but this is easily stripped out or replaced with a different sort of attachment. Here's how, step by step. 1. Enable 2-Step Verification (required) App Passwords only exist once 2FA is on. Go to https://myaccount.google.com/security Under "How you sign in to Google", open 2-Step Verification and complete the setup (phone or authenticator app). 2. Generate the App Password Go to https://myaccount.google.com/apppasswords (or search "App passwords" in your Google Account settings). You may be asked to re-enter your password. Enter a name when prompted — e.g. PicoMite (it's just a label, type anything). Click Create. Google shows a 16-character password in four groups of four, like abcd efgh ijkl mnop. Copy it now — it's shown only once. 3. Use it in your program It works exactly like your SMTP2GO credentials — just three changes: Local string EmailFrom = "you@gmail.com" Local string smtpuser = "you@gmail.com" ' full Gmail address Local string smtppass = "asixteencharcode" ' the 16 chars, spaces removed ... WEB OPEN TLS CLIENT "smtp.gmail.com", 465, 20000 ' no banner READ over TLS — send EHLO directly (greeting consumed during OPEN) WEB TCP CLIENT REQUEST "EHLO picomite" + cr, b() WEB TCP CLIENT REQUEST "AUTH LOGIN" + cr, b() WEB TCP CLIENT REQUEST base64Encode(smtpuser) + cr, b() WEB TCP CLIENT REQUEST base64Encode(smtppass) + cr, b() ' ... rest identical to the working SMTP2GO-over-TLS code Remove the spaces from the 16-char password (the spaces are only for readability). Use port 465 with WEB OPEN TLS CLIENT (implicit TLS — which works on the WEBRP2350). Gmail also offers 587, but that's STARTTLS, which the firmware doesn't do, so stick with 465. Username is your full email address. Gotchas No "App passwords" link? Then either 2FA isn't enabled yet, or it's a Google Workspace account where the admin has disabled app passwords (in that case you'd need to use the OAuth2 route, which isn't currently supported), or Advanced Protection is on. Treat the app password like your account password — it can send mail as you. If it leaks or you lose it, delete it on that same page Sub MailSend Const cr = Chr$(13)+Chr$(10) Local integer b(512), buf(1100) Local integer fnbr, fsize, k Local String txt, h$, enc$, chunk$ Local string EmailFrom="me@gmail.com" Local string EmailTo="you@gmail.com" Local string smtpuser = "me@gmail.com" Local string smtppass = "asixteencharcode" Local string bd = "=_picomite_boundary_=" Local string fname = "A:/test.jpg" txt = "Test email" ErrEmail = true : FaultDetected = true If Dir$(fname, File) = "" Then Print "Attachment not found: " + fname : Exit Sub On Error Skip WEB OPEN TLS CLIENT "smtp.gmail.com", 465,20000 If MM.Errno <> 0 Then Print "Connect failed" : Exit Sub ' wait for the 220 greeting BEFORE talking (not an early talker) ' WEB TCP CLIENT READ b(), 30000 ' If Not WaitCode(b(), "220") Then GoTo bail WEB TCP CLIENT REQUEST "EHLO picomite" + cr, b() If Not WaitCode(b(), "250") Then GoTo bail WEB TCP CLIENT REQUEST "AUTH LOGIN" + cr, b() If Not WaitCode(b(), "334") Then GoTo bail WEB TCP CLIENT REQUEST base64Encode(smtpuser) + cr, b() If Not WaitCode(b(), "334") Then GoTo bail WEB TCP CLIENT REQUEST base64Encode(smtppass) + cr, b() If Not WaitCode(b(), "235") Then GoTo bail WEB TCP CLIENT REQUEST "MAIL FROM:<" + EmailFrom + ">" + cr, b() If Not WaitCode(b(), "250") Then GoTo bail WEB TCP CLIENT REQUEST "RCPT TO:<" + EmailTo + ">" + cr, b() If Not WaitCode(b(), "250") Then GoTo bail WEB TCP CLIENT REQUEST "DATA" + cr, b() If Not WaitCode(b(), "354") Then GoTo bail ' ===== build the DATA body in buf() and stream it with WRITE ===== LongString Clear buf() h$ = "From: " + EmailFrom + cr h$ = h$ + "To: " + EmailTo + cr h$ = h$ + "Subject: A test email" + cr h$ = h$ + "MIME-Version: 1.0" + cr h$ = h$ + "Content-Type: multipart/mixed; boundary=" + Chr$(34) + bd + Chr$(34) + cr + cr h$ = h$ + "--" + bd + cr h$ = h$ + "Content-Type: text/plain; charset=us-ascii" + cr + cr LongString Append buf(), h$ LongString Append buf(), txt + cr h$ = "--" + bd + cr h$ = h$ + "Content-Type: image/jpeg; name=" + Chr$(34) + "test.jpg" + Chr$(34) + cr h$ = h$ + "Content-Transfer-Encoding: base64" + cr h$ = h$ + "Content-Disposition: attachment; filename=" + Chr$(34) + "test.jpg" + Chr$(34) + cr + cr LongString Append buf(), h$ fnbr = 1 Open fname For Input As #fnbr fsize = Lof(#fnbr) Do While fsize > 0 k = MIN(189, fsize) chunk$ = Input$(k, #fnbr) enc$ = base64Encode(chunk$) If LLen(buf()) + Len(enc$) + 2 > 8700 Then WEB TCP CLIENT WRITE buf() LongString Clear buf() EndIf LongString Append buf(), enc$ + cr fsize = fsize - k Loop Close #fnbr LongString Append buf(), "--" + bd + "--" + cr WEB TCP CLIENT WRITE buf() LongString Clear buf() WEB TCP CLIENT REQUEST "." + cr, b() If WaitCode(b(), "250") Then ErrEmail = false : FaultDetected = false Print "Email sent with attachment" Else Print "Send not confirmed" EndIf WEB TCP CLIENT REQUEST "QUIT" + cr, b() bail: WEB CLOSE TCP CLIENT End Sub Function WaitCode(b() As integer, code As string) As integer WaitCode = (LInStr(b(), code) > 0) End Function Function base64Encode(si As string) As string Local integer iiii = Math(base64 encode si, base64Encode) End Function Edited 2026-06-26 23:21 by matherp |
||||
| terekgabor Senior Member Joined: 02/01/2026 Location: HungaryPosts: 104 |
Nice Peter! I will try it. G@bor |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |