

Enter your Gmail username and the 16-digit app password into the respective fields. Leave the smtpserver and smtpserverport fields as is when copying the code. With fields: This is where you configure your SMTP settings for your Gmail account.For the body, you can piece together components of the message by using the & string to insert data from any of the Microsoft Excel sheets right into the email message, just like shown above. From field needs to contain your Gmail address, but you're free to set the other fields however you want. With NewMail: This section contains all the parameters for sending your email, including the body of your email.MsgBox "Error encountered while sending email." & vbNewLine & Err.Number & ": " & Err.DescriptionĪnd these are the sections and fields you need to customize: MsgBox "Check your login credentials and try again." & vbNewLine & Err.Number & ": " & Err.Description Click on the Macro name you want to run and press the Run button. Head to the Developer tab and click on Macros. Make sure you open the workbook that contains the macro you want to run. MsgBox "Check your internet connection." & vbNewLine & Err.Number & ": " & Err.DescriptionĬase -2147220975 'Incorrect credentials User ID or password Follow these steps to run a macro from the Developer tab: 1. MsgBox "Your email has been sent", vbInformationĬase -2147220973 'Could be because of Internet Connection Item(msConfigURL & "/sendpassword") = "password" 'Your password or App Password Item(msConfigURL & "/sendusername") = 'Your gmail address Item(msConfigURL & "/sendusing") = 2 'Send using default setting Item(msConfigURL & "/smtpserverport") = 465 'Set the SMTP port Details Item(msConfigURL & "/smtpserver") = "" 'Set the SMTP server details Item(msConfigURL & "/smtpauthenticate") = 1 'SMTP authentication Enabled Item(msConfigURL & "/smtpusessl") = True 'Enable SSL Authentication Addattachment "c:\data\email.pdf" 'Duplicate the line for a second attachment. Addattachment "c:\data\email.xlsx" 'Optional file attachment remove if not needed. And here is some added data:" & Str(Sheet1.Cells( 2, 1)) TextBody = "This is the body of your email. Subject = "Send Email From an Excel Spreadsheet"
EXCEL MACRO CODE
This is what the complete code looks like: Sub Send_Emails() Paste all the code snippets below into your Module1 (Code) window. Keep in mind that while many of the fields are optional, the From and To fields are required. Now you're ready to create the mail objects and set up all the fields necessary to send an email.
