#excel #vba #email #gmail
#excel #vba #Адрес электронной почты #gmail
Вопрос:
Я пытаюсь отправить письмо с помощью службы gmail (не Outlook) с изображением в теле в виде встроенного изображения с помощью vba.
Я добавил библиотеку Microsoft CDO Windows 2000
Option Explicit
Sub SendEmailUsingGmail()
Dim NewMail As Object
Dim mailConfig As Object
Dim fields As Variant
Dim msConfigURL As String
Dim rng As Range
Set rng = Nothing
On Error Resume Next
Set rng = Selection.SpecialCells(xlCellTypeVisible)
On Error GoTo Err:
'late binding
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
.Sender = ""
.From = "Sourav Bhattacharya"
.To = ""
.CC = ""
.BCC = ""
.Subject = "Demo Spreadsheet Attached"
.Textbody = "Let me know if you have questions about the attached spreadsheet!"
.Addattachment "C:UsersallsoDesktopnew vba projectstemp.jpg"
.HTMLBody = .Textbody amp; "<html><p>CPS Daily progress Report of Nov 2020</p>" amp; _
"<img src=""cid:temp.jpg"" height='300' width='300'>"
End With
msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
With fields
.Item(msConfigURL amp; "/smtpusessl") = True 'Enable SSL Authentication
.Item(msConfigURL amp; "/smtpauthenticate") = 1 'SMTP authentication Enabled
.Item(msConfigURL amp; "/smtpserver") = "smtp.gmail.com" 'Set the SMTP server details
.Item(msConfigURL amp; "/smtpserverport") = 465 'Set the SMTP port Details
.Item(msConfigURL amp; "/sendusing") = 2 'Send using default setting
.Item(msConfigURL amp; "/sendusername") = "" 'Your gmail address
.Item(msConfigURL amp; "/sendpassword") = "" 'Your password or App Password
.Update 'Update the configuration fields
End With
NewMail.Configuration = mailConfig
NewMail.Send
MsgBox "Your email has been sent", vbInformation
Exit_Err:
'Release object memory
Set NewMail = Nothing
Set mailConfig = Nothing
End
Err:
Select Case Err.Number
Case -2147220973 'Could be because of Internet Connection
MsgBox "Check your internet connection." amp; vbNewLine amp; Err.Number amp; ": " amp; Err.Description
Case -2147220975 'Incorrect credentials User ID or password
MsgBox "Check your login credentials and try again." amp; vbNewLine amp; Err.Number amp; ": " amp; Err.Description
Case Else 'Report other errors
MsgBox "Error encountered while sending email." amp; vbNewLine amp; Err.Number amp; ": " amp; Err.Description
End Select
Resume Exit_Err
End Sub
Я могу отправить письмо, и изображение отлично прикрепляется к почте
Однако в теле письма вместо изображения отображается пробел
Я пробовал искать во многих местах, однако не смог найти решение
Может кто-нибудь, пожалуйста, помогите
Спасибо, Сурав Бхаттачарья
Комментарии:
1. Я думаю, что это бит CID, который может вызывать проблемы blog.mailtrap.io /…
2. Мне удалось сделать это, создав HTML-файл, однако все же хотелось бы знать точный способ без использования HTML-файла