{"id":12108,"date":"2011-12-16T22:31:46","date_gmt":"2011-12-16T22:31:46","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1061"},"modified":"2022-08-17T19:31:18","modified_gmt":"2022-08-17T19:31:18","slug":"how-to-send-an-email-using-excel-macro-from-gmail-or-yahoo","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2011\/12\/how-to-send-an-email-using-excel-macro-from-gmail-or-yahoo\/","title":{"rendered":"How to Send an Email using Excel Macro from Gmail or Yahoo"},"content":{"rendered":"
[et_pb_section fb_built=”1″ admin_label=”section” _builder_version=”4.17.6″ custom_padding=”0px|0px|0px|0px|true|true” da_disable_devices=”off|off|off” global_colors_info=”{}” da_is_popup=”off” da_exit_intent=”off” da_has_close=”on” da_alt_close=”off” da_dark_close=”off” da_not_modal=”on” da_is_singular=”off” da_with_loader=”off” da_has_shadow=”on”][et_pb_row admin_label=”row” _builder_version=”4.16″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” global_colors_info=”{}”][et_pb_column type=”4_4″ _builder_version=”4.16″ custom_padding=”|||” global_colors_info=”{}” custom_padding__hover=”|||”][et_pb_text _builder_version=”4.17.6″ _module_preset=”default” global_colors_info=”{}”]Dear Readers,<\/p>\n
In this article, we are going to learn how to send an email automatically by Excel Excel VBA. While executing the Excel Macro, sometimes you may need to send some Details or Results directly to your email ID or any other ID using Excel Macro. If you have some report or details which you want to send in a mail without log-in to your email account. . In Microsoft Excel, to use this Object you need to Add CDO Reference.<\/p>\n Add CDO Reference<\/p><\/div>\n Note:<\/strong> Without adding this reference you can Run the below Code. For running this Code, you can use below line to create Object runtime:<\/p>\n In this article, i will explain you 2 simple methods, to send an email by Excel VBA.<\/p>\n <\/a><\/p>\n \n<\/a><\/p>\n \n<\/a><\/p>\n For sending HTML Body you just need to Change the Email Properties which you are setting above<\/p>\n \n<\/a><\/p>\n [\/et_pb_text][et_pb_blurb title=”You may also like reading these articles: ” use_icon=”on” font_icon=”||fa||900″ _builder_version=”4.17.6″ _module_preset=”0249c68e-4de8-4f44-84ff-a9b1850785b6″ hover_enabled=”0″ global_colors_info=”{}” sticky_enabled=”0″]<\/p>\n [\/et_pb_blurb][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":" Dear Readers, In this article, we are going to learn how to send an email automatically by Excel Excel VBA. While executing the Excel Macro, sometimes you may need to send some Details or Results directly to your email ID or any other ID using Excel Macro. If you have some report or details which […]<\/p>\n","protected":false},"author":45,"featured_media":1063,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[5205,1682],"tags":[],"class_list":["post-12108","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-send-email","category-popular-articles"],"yoast_head":"\n
\nThis you are going to achieve by using CDO Object<\/strong>.<\/p>\nWhat is CDO (Collaboration Data Objects)??<\/h1>\n
\nThis Object is basically used for Active Messaging, Enables user to Access Global Server Objects and Address List<\/strong> For more about this read this Microsoft Help<\/a>.<\/p>\n<\/a>
\nSet myMail = CreateObject("CDO.Message")\n<\/code><\/pre>\n
\n
1. Excel VBA: Send email from Gmail<\/h1>\n
\n\nSub SendEmailUsingGmail()\n \n On Error GoTo Err\n \n Dim NewMail As Object\n Dim mailConfig As Object\n Dim fields As Variant\n Dim msConfigURL As String\n \n Set NewMail = CreateObject(\"CDO.Message\")\n Set mailConfig = CreateObject(\"CDO.Configuration\")\n \n ' load all default configurations\n mailConfig.Load -1\n \n Set fields = mailConfig.fields\n \n'Set All Email Properties\n \n With NewMail\n .Subject = \"Test Mail from LearnExcelMacro.com\"\n .From = \"email@gmail.com\"\n .To = \"email2@gmail.com;email3@gmail.com\"\n .CC = \"email4@gmail.com\"\n .BCC = \"\"\n .textbody = \"\"\n End With\n \n msConfigURL = \"http:\/\/schemas.microsoft.com\/cdo\/configuration\"\n \n With fields\n 'Enable SSL Authentication\n .Item(msConfigURL & \"\/smtpusessl\") = True\n \n 'Make SMTP authentication Enabled=true (1)\n .Item(msConfigURL & \"\/smtpauthenticate\") = 1\n \n 'Set the SMTP server and port Details\n 'To get these details you can get on Settings Page of your Gmail Account\n .Item(msConfigURL & \"\/smtpserver\") = \"smtp.gmail.com\"\n .Item(msConfigURL & \"\/smtpserverport\") = 465\n .Item(msConfigURL & \"\/sendusing\") = 2\n \n 'Set your credentials of your Gmail Account\n .Item(msConfigURL & \"\/sendusername\") = \"email@gmail.com\"\n .Item(msConfigURL & \"\/sendpassword\") = \"********\"\n \n 'Update the configuration fields\n .Update\n \n End With\n NewMail.Configuration = mailConfig\n NewMail.Send\n MsgBox (\"Mail has been Sent\")\n \nExit_Err:\n \n Set NewMail = Nothing\n Set mailConfig = Nothing\n End\n \nErr:\n Select Case Err.Number\n \n Case -2147220973 'Could be because of Internet Connection\n MsgBox \" Could be no Internet Connection !! -- \" & Err.Description\n \n Case -2147220975 'Incorrect credentials User ID or password\n MsgBox \"Incorrect Credentials !! -- \" & Err.Description\n \n Case Else 'Rest other errors\n MsgBox \"Error occured while sending the email !! -- \" & Err.Description\n End Select\n \n Resume Exit_Err\n \nEnd Sub\n\n<\/code><\/pre>\n
2. Excel VBA: Send email from Yahoo<\/h1>\n
\n\nSub SendEmailUsingYahoo()\n \n On Error GoTo Err\n \n Dim NewMail As Object\n Dim mailConfig As Object\n Dim fields As Variant\n Dim msConfigURL As String\n \n Set NewMail = CreateObject(\"CDO.Message\")\n Set mailConfig = CreateObject(\"CDO.Configuration\")\n \n ' load all default configurations\n mailConfig.Load -1\n \n Set fields = mailConfig.fields\n \n'Set All Email Properties\n \n With NewMail\n .Subject = \"Test Mail from LearnExcelMacro.com\"\n .From = \"email@yahoo.co.in\"\n .To = \"email1@gmail.com;email2@yahoo.com\"\n .CC = \"email@email.com\"\n .BCC = \"\"\n .textbody = \"\"\n End With\n \n msConfigURL = \"http:\/\/schemas.microsoft.com\/cdo\/configuration\"\n \n With fields\n 'Enable SSL Authentication\n .Item(msConfigURL & \"\/smtpusessl\") = True\n \n 'Make SMTP authentication Enabled=true (1)\n .Item(msConfigURL & \"\/smtpauthenticate\") = 1\n \n 'Set the SMTP server and port Details\n 'To get these details you can get on Settings Page of your yahoo Account\n .Item(msConfigURL & \"\/smtpserver\") = \"smtp.mail.yahoo.com\"\n .Item(msConfigURL & \"\/smtpserverport\") = 465\n .Item(msConfigURL & \"\/sendusing\") = 2\n \n 'Set your credentials of your yahoo Account\n .Item(msConfigURL & \"\/sendusername\") = \"email@yahoo.co.in\"\n .Item(msConfigURL & \"\/sendpassword\") = \"******\"\n \n 'Update the configuration fields\n .Update\n \n End With\n NewMail.Configuration = mailConfig\n NewMail.Send\n MsgBox (\"Mail has been Sent\")\n \nExit_Err:\n \n Set NewMail = Nothing\n Set mailConfig = Nothing\n End\n \nErr:\n Select Case Err.Number\n \n Case -2147220973 'Could be because of Internet Connection\n MsgBox \" Could be no Internet Connection !! -- \" & Err.Description\n \n Case -2147220975 'Incorrect credentials User ID or password\n MsgBox \"Incorrect Credentials !! -- \" & Err.Description\n \n Case Else 'Rest other errors\n MsgBox \"Error occured while sending the email !! -- \" & Err.Description\n End Select\n \n Resume Exit_Err\n \nEnd Sub\n \n<\/code><\/pre>\n
Excel VBA to Send Email with HTML Body<\/h1>\n
\n\nWith NewMail\n .Subject = "Test Mail from LearnExcelMacro.com"\n .From = "email@yahoo.com"\n .To = "email2@gmail.com;email3@yahoo.com"\n .CC = "email@email.com"\n .BCC = ""\n .HTMLBody = "Write your complete HTML Page"\nEnd With\n<\/code><\/pre>\n
Excel VBA to Send Email with an Attachment<\/h1>\n
\nWith NewMail\n .Subject = "Test Mail from LearnExcelMacro.com"\n .From = "vishwamitra01@yahoo.com"\n .To = "vishwamitra02@gmail.com;info@learnexcelmacro.com"\n .CC = "vishwamitra01@gmail.com"\n .BCC = ""\n .HTMLBody = "Write your complete HTML Page"\n\n' For multiple Attachment you can add below lines as many times\n\n .AddAttachment "C:\\ExcelMacro-help.xls"\n .AddAttachment "C:\\ExcelMacro-help2.xls"\nEnd With\n<\/code><\/pre>\n
\n