简体   繁体   English

Sendgrid电子邮件Excel VBA SMTP

[英]Sendgrid Email Excel VBA SMTP

I'm trying to integrate Sendgrid SMTP with excel for sending emails directly from my excel sheet. 我正在尝试将Sendgrid SMTP与excel集成在一起,以便直接从excel工作表发送电子邮件。 The code given below returns an error saying "Message couldnot be sent to SMTP Server". 下面给出的代码返回一个错误,指出“无法将邮件发送到SMTP服务器”。 Error Image 错误图片

 Sub Manufactureremail1() 'sends email to manufacturer using Sendgrid 'Application.ScreenUpdating = False 'While (True) Dim iMsg As Object Dim iConf As Object Dim strbody As String ' Dim Flds As Variant Set iMsg = CreateObject("CDO.Message") Set iConf = CreateObject("CDO.Configuration") iConf.Load -1 ' CDO Source Defaults Set Flds = iConf.Fields With Flds .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _ = "smtp.sendgrid.net" .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587 .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myusername" .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword" .Update End With With iMsg Set .Configuration = iConf .To = Application.WorksheetFunction.VLookup(Selection.Cells(2, 1), Worksheets("Contacts").Range("email_data"), 2, False) .CC = "" '.BCC = Application.WorksheetFunction.VLookup(Selection.Cells(2, 3), Worksheets("Contacts").Range("email_data"), 2, False) .From = """Yash"" <yashagarwal080@gmail.com>" .Subject = "Order of " & Selection.Cells(2, 3) '.addattachment Worksheets("Contacts").Range("I6") '.addattachment Worksheets("Contacts").Range("I7") .htmlBody = "Message" .send 'MsgBox "Emails Sent!", vbInformation, "Success" End With 'DoEvents 'Wend 'Application.ScreenUpdating = True End Sub 

Set your configuration to use SSL by adding: 通过添加以下内容,将您的配置设置为使用SSL:

.Item(" http://schemas.microsoft.com/cdo/configuration/smtpusessl ") = 1 .Item(“ http://schemas.microsoft.com/cdo/configuration/smtpusessl”)= 1

Yash, I tested your code with out mail server config and it worked fine to me only two things I had done. 是的,我在没有邮件服务器配置的情况下测试了您的代码,对我来说,只有两件事对我来说很好。 1. I had referenced Microsoft CDO in the tools -> Reference. 1.我已经在工具->参考中引用了Microsoft CDO。 2. I changed the port address to 25 2.我将端口地址更改为25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM