能否在Windows系统下无需外部应用用PowerBuilder 12.5发送富文本邮件?
Absolutely! You can send emails directly from PowerBuilder 12.5 without relying on external apps, and meet all your requirements (HTML/rich text formatting, Gmail/Yahoo addresses, Windows 7/10 compatibility) using one of two reliable approaches.
Approach 1: Using PowerBuilder's Native MailSession Object (MAPI)
This method leverages Windows' built-in MAPI (Messaging Application Programming Interface) and works with the user's default mail client (like Outlook or Windows Mail) configured with the sender's Gmail account. It’s straightforward to implement if the target machine already has a properly set up mail profile.
Key Details:
- Supports HTML formatting for rich, Word-style content
- Works on both Windows 7 and 10
- Requires the sender's Gmail account to be configured in a MAPI-compliant client
Example Code:
MailSession mses MailMessage mmsg MailReturnCode mrc // Initialize mail session mses = CREATE MailSession // Use your Gmail MAPI profile name and app password (if 2FA is enabled) mrc = mses.MailLogon(ProfileName:="Gmail", Password:="your_gmail_app_password") IF mrc <> mrcSuccess THEN MessageBox("Error", "Failed to log into mail session: " + String(mrc)) DESTROY mses RETURN END IF // Build the email message mmsg = CREATE MailMessage mmsg.MailFormat = mfHTML // Enable HTML mode mmsg.Subject = "Test Email from PowerBuilder" mmsg.NoteText = "<html><body><h1>Hello Donald!</h1><p>This is an <b>HTML-formatted</b> email with Word-style formatting.</p><p>Regards,<br>Mickey</p></body></html>" mmsg.Recipient[1].Address = "donald.duck@yahoo.co.uk" mmsg.Recipient[1].Type = mtTo mmsg.Sender.Address = "mickey.mouse@gmail.com" // Send the message mrc = mses.MailSend(mmsg) IF mrc = mrcSuccess THEN MessageBox("Success", "Email sent successfully!") ELSE MessageBox("Error", "Failed to send email: " + String(mrc)) END IF // Clean up resources mses.MailLogoff() DESTROY mmsg DESTROY mses
Approach 2: Using CDO for Windows (Direct SMTP)
This method sends emails directly via SMTP without depending on the user's mail client. It’s ideal for unattended or server-side scenarios where you don’t want to rely on local mail client settings.
Key Details:
- Direct SMTP connection to Gmail’s servers
- Full HTML support for rich content
- Works on Windows 7 and 10 (CDO is included with Windows)
- Requires an app password if 2-Step Verification is enabled on your Gmail account
Example Code:
OLEObject cdoMsg, cdoConf String smtpServer, smtpPort, username, password // Initialize CDO objects cdoMsg = CREATE OLEObject cdoConf = CREATE OLEObject IF cdoMsg.ConnectToNewObject("CDO.Message") = 0 AND cdoConf.ConnectToNewObject("CDO.Configuration") = 0 THEN // Gmail SMTP settings smtpServer = "smtp.gmail.com" smtpPort = "587" username = "mickey.mouse@gmail.com" password = "your_gmail_app_password" // Use app password for 2FA accounts // Configure SMTP authentication and SSL cdoConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer cdoConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = smtpPort cdoConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 // Send via SMTP cdoConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 // Basic auth cdoConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = username cdoConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = password cdoConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True cdoConf.Fields.Update() // Build the email cdoMsg.Configuration = cdoConf cdoMsg.From = username cdoMsg.To = "donald.duck@yahoo.co.uk" cdoMsg.Subject = "Direct SMTP Email from PB 12.5" cdoMsg.HTMLBody = "<html><body><h1>Hi Donald!</h1><p>This email is sent directly via SMTP with <u>formatted text</u> and links.</p><p>Best,<br>Mickey</p></body></html>" // Send the email TRY cdoMsg.Send() MessageBox("Success", "Email sent successfully!") CATCH (OLEException oe) MessageBox("Error", "Failed to send email: " + oe.Description) END TRY // Clean up cdoMsg.DisconnectObject() cdoConf.DisconnectObject() ELSE MessageBox("Error", "Failed to initialize CDO objects.") END IF DESTROY cdoMsg DESTROY cdoConf
Important Notes:
- For Gmail accounts with 2-Step Verification enabled, generate an App Password (instead of your regular password) to use in the code.
- To include MS Word-style formatting, save your Word document as HTML and use that content as the
NoteText(MailSession) orHTMLBody(CDO) value.
内容的提问来源于stack exchange,提问作者HugMyster

