Yahoo Mail - SMTP, IMAP, POP Authentication with App Password
See more Yahoo Mail Examples
An application can read and send email from Yahoo mail accounts, but now it must use an "application password" rather then the account owner's password.Let's say your application is "Xyz". Each Yahoo Mail account owner would first need to go to the "Account Security" tab in her Yahoo Mail account settings and create an App Password. It is the App Password that is used with SMTP, IMAP, and POP instead of the normal account password. The Yahoo Mail account owner can choose to create individual app passwords, one for each app, or can create a single app password that she might use for all apps. Ultimately, this is just a way to allow the user to associate passwords with individual apps such that a password for an individual app can be changed or revoked without affecting other apps. This is also better security. It's the same idea as using a password manager. Rather than using the same password for all online accounts, each individual account has a generated password that is managed by a password manager service or program with a single master password.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- To authenticate with Yahoo Mail in SMTP, IMAP, or POP, the full email address is the username, and a generated app password is the password.
-- See Yahoo Mail Generate and manage third-party app passwords
-- The information at the above linked web page is:
-- Some older, third-party email apps (that do not use our Yahoo branded sign-in page) require you to enter a single password for login credentials.
-- To access your Yahoo Mail account on these apps, you'll need to generate and use an app password.
-- An app password is a long, randomly generated code that gives a non-Yahoo app permission to access your Yahoo account.
-- You�ll only need to provide this code once to sign in to your third-party email app.
--
-- Generate an app password
--
-- Sign in to your Yahoo Account Security page.
-- Click Generate app password or Generate and manage app passwords.
-- Enter your app's name in the text field.
-- Click Generate password.
-- Follow the instructions below the app password.
-- Click Done.
--
-- Use this app password and your email address to sign in to your email app.
-- ------
-- NOTE: The comment "You�ll only need to provide this code once to sign in to your third-party email app." is assuming your app
-- is persisting the user's password such that it re-uses it to authenticate each time it connects to the mail server.
-- All SMTP, IMAP, or POP sessions must authenticate with each new connection/session.
-- ------
-- First, let's demonstrate IMAP...
DECLARE @imap int
EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @imap, 'Port', 993
EXEC sp_OASetProperty @imap, 'Ssl', 1
EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'imap.mail.yahoo.com'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
DECLARE @myYahooEmailAddress nvarchar(4000)
SELECT @myYahooEmailAddress = 'joe@yahoo.com'
DECLARE @myGeneratedAppPassword nvarchar(4000)
SELECT @myGeneratedAppPassword = 'lrabkaprvntxdjmc'
-- Sign into the app/service using your normal username
-- Instead of your normal password, enter the app password above
EXEC sp_OAMethod @imap, 'Login', @success OUT, @myYahooEmailAddress, @myGeneratedAppPassword
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
EXEC sp_OAMethod @imap, 'SelectMailbox', @success OUT, 'Inbox'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
PRINT 'Yahoo IMAP all good!'
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
-- --------------------------------------------
-- Now do Yahoo SMTP:
DECLARE @mailman int
EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT
EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.mail.yahoo.com'
EXEC sp_OASetProperty @mailman, 'SmtpUsername', @myYahooEmailAddress
EXEC sp_OASetProperty @mailman, 'SmtpPassword', @myGeneratedAppPassword
EXEC sp_OASetProperty @mailman, 'SmtpSsl', 1
EXEC sp_OASetProperty @mailman, 'SmtpPort', 465
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
EXEC sp_OASetProperty @email, 'Subject', 'This is a test'
EXEC sp_OASetProperty @email, 'Body', 'This is a test'
EXEC sp_OASetProperty @email, 'FromName', 'Joe'
EXEC sp_OASetProperty @email, 'FromAddress', @myYahooEmailAddress
-- Please change the recipient before running this code..
EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Chilkat', 'info@chilkatsoft.com'
EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
EXEC @hr = sp_OADestroy @email
RETURN
END
EXEC sp_OAMethod @mailman, 'CloseSmtpConnection', @success OUT
IF @success <> 1
BEGIN
PRINT 'Connection to SMTP server not closed cleanly.'
END
PRINT 'Yahoo Mail Sent!'
-- ------------------------------------
-- Now do Yahoo POP3
EXEC sp_OASetProperty @mailman, 'MailHost', 'pop.mail.yahoo.com'
EXEC sp_OASetProperty @mailman, 'PopUsername', @myYahooEmailAddress
EXEC sp_OASetProperty @mailman, 'PopPassword', @myGeneratedAppPassword
EXEC sp_OASetProperty @mailman, 'MailPort', 995
EXEC sp_OASetProperty @mailman, 'PopSsl', 1
EXEC sp_OAMethod @mailman, 'Pop3Connect', @success OUT
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
EXEC @hr = sp_OADestroy @email
RETURN
END
EXEC sp_OAMethod @mailman, 'Pop3Authenticate', @success OUT
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
EXEC @hr = sp_OADestroy @email
RETURN
END
PRINT 'Connected and authenticated with Yahoo POP Mail Server.'
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @mailman
EXEC @hr = sp_OADestroy @email
END
GO