Forum Discussion

Shah_Samiur's avatar
Shah_Samiur
Copper Contributor
Jan 07, 2024

Send Email using yahoo smtp

hi!

I want to send email from vba userform and I have used the following codes with failure. Please help me to make it correct. Thanks in advance.

 

Macro is as follows-

 

On Error GoTo Err

Dim NewMail As Object
Dim mailConfig As Object
Dim fields As Variant
Dim msConfigURL As String

Set NewMail = CreateObject("CDO.Message")
Set mailConfig = CreateObject("CDO.Configuration")

' load all default configurations
mailConfig.Load -1

Set fields = mailConfig.fields

'Set All Email Properties

With NewMail
.Subject = "Test Mail from LearnExcelMacro.com"
.From = "email address removed for privacy reasons"
.To = "email address removed for privacy reasons"
.CC = ""
.BCC = ""
.TextBody = ""
End With

msConfigURL = "http://schemas.microsoft.com/cdo/configuration"

With fields
'Enable SSL Authentication
.Item(msConfigURL & "/smtpusessl") = True

'Make SMTP authentication Enabled=true (1)
.Item(msConfigURL & "/smtpauthenticate") = 1

'Set the SMTP server and port Details
'To get these details you can get on Settings Page of your yahoo Account
.Item(msConfigURL & "/smtpserver") = "smtp.mail.yahoo.com"
.Item(msConfigURL & "/smtpserverport") = 465 '25 '587 ' 465
.Item(msConfigURL & "/sendusing") = 2

'Set your credentials of your yahoo Account
.Item(msConfigURL & "/sendusername") = "email address removed for privacy reasons"
.Item(msConfigURL & "/sendpassword") = "nessa123usa"

'Update the configuration fields
.Update

End With

NewMail.Configuration = mailConfig
NewMail.Send
MsgBox ("Mail has been Sent")

Exit_Err:

Set NewMail = Nothing
Set mailConfig = Nothing
' End
Exit Sub

Err:
Select Case Err.Number

Case -2147220973 'Could be because of Internet Connection
MsgBox " Could be no Internet Connection !! -- " & Err.description

Case -2147220975 'Incorrect credentials User ID or password
MsgBox "Incorrect Credentials !! -- " & Err.description

Case Else 'Rest other errors
MsgBox "Error occured while sending the email !! -- " & Err.description
End Select

Resume Exit_Err

  • Shah_Samiur's avatar
    Shah_Samiur
    Copper Contributor
    Thanks a lot Sir, for the response. I will test it and let you know soon. Thanks again.
  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    Shah_Samiur 

    To send an email using Yahoo SMTP in VBA, you can use the following code. Make sure to replace the placeholders with your actual email address and password.

    Vba Code is untested; please backup your file before.

    Sub SendEmailUsingYahooSMTP()
        On Error GoTo ErrHandler
    
        Dim iMsg As Object
        Dim iConf As Object
        Dim Flds As Object
    
        ' Email Configuration
        Set iMsg = CreateObject("CDO.Message")
        Set iConf = CreateObject("CDO.Configuration")
        Set Flds = iConf.Fields
    
        ' SMTP Configuration
        With Flds
            ' Set configuration fields for Yahoo SMTP
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mail.yahoo.com"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 ' Basic (clear-text) authentication
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "email address removed for privacy reasons"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_yahoo_password"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True ' Use SSL
            .Update
        End With
    
        ' Email Properties
        With iMsg
            Set .Configuration = iConf
            .To = "email address removed for privacy reasons"
            .CC = ""
            .BCC = ""
            .From = "email address removed for privacy reasons"
            .Subject = "Test Email"
            .TextBody = "This is a test email sent using Yahoo SMTP in VBA."
            .Send
        End With
    
        MsgBox "Email sent successfully!", vbInformation
    
    ExitSub:
        Set iMsg = Nothing
        Set iConf = Nothing
        Set Flds = Nothing
        Exit Sub
    
    ErrHandler:
        MsgBox "An error occurred: " & Err.Description, vbCritical
        Resume ExitSub
    End Sub

     

    Make sure to enable "Less Secure App Access" in your Yahoo account settings and generate an "App Password" for your application to use. Replace "email address removed for privacy reasons" and "your_yahoo_password" with your Yahoo email address and app password.

    Note: Using hard-coded credentials in your VBA code is not recommended for security reasons. Always ensure that your code is secure, and consider other secure methods for storing credentials if the code will be shared or distributed. The text was created with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

     

    Was the answer useful? Mark as best response and like it!

    This will help all forum participants.

    • Shah_Samiur's avatar
      Shah_Samiur
      Copper Contributor
      Thanks a lot Sir, for the response. I will test it and let you know. Thanks again.

Resources