
Option Explicit
Private Sub SendEmailWithCDO()
Dim bResponse As Boolean, cAttachs as Collection
set cAttachs = new collection
cAttachs.Add "c: estallegato1.pdf"
bResponse = BuildCDOEmail("destinatario@google.com;destinatario2@yahoo.it", "Nome mittente", "email_mittente@dominio.it", "mail.server.mittente.it", "Oggetto del messaggio", "Testo messaggio: hello world", cAttachs)
If bResponse Then MsgBox "All ok. Emailes sent succesfully!" Else MsgBox "Errors occured."
End Sub
Private Function BuildCDOEmail(sTo As String, sFrom As String, sSender As String, SMTP_SERVER As String, sSubject As String, sBody As String, cAttachs As Collection) As Boolean
Dim i As Integer
Dim Cdo2Configuration As CDO.Configuration, Cdo2Message As CDO.Message, Cdo2Fields, v
On Error GoTo Err_SendEmail
BuildCDOEmail = False
' Create a new Configuration object.
Set Cdo2Configuration = New CDO.Configuration
' Get a reference to the Configuration object's Fields collection
Set Cdo2Fields = Cdo2Configuration.Fields
' Set the Configuration object's properties through its Fields collection.
With Cdo2Fields
.Item(cdoSMTPServer) = SMTP_SERVER
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Update ' Important
End With
' Create a new message.
Set Cdo2Message = New CDO.Message
' Set the message's configuration.
Set Cdo2Message.Configuration = Cdo2Configuration
' Set the message content.
Cdo2Message.Subject = sSubject
Cdo2Message.TextBody = sBody
' Address the message.
Cdo2Message.Sender = sSender
Cdo2Message.From = sFrom
Cdo2Message.To = sTo
'AddAttachments Cdo2Message
For Each v In cAttachs
Cdo2Message.AddAttachment v
Next
' Send the message.
Cdo2Message.Send
Set Cdo2Message = Nothing
Set Cdo2Configuration = Nothing
Set Cdo2Fields = Nothing
BuildCDOEmail = True
Exit Function
Err_SendEmail:
BuildCDOEmail = False
End Function
|
Sub sendgmail()
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/smptserverport") = 587
.Item("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/sendusername") = "userCHIOCCIOLAgmail.com"
.Item("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxx"
.Update
End With
' build email parts
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
With cdomsg
.To = "aaaaCHIOCCIOLAbbb.it"
.From = "userCHIOCCIOLAgmail.com"
.Subject = "terza prova"
.TextBody = strbody
.CC = ""
.BCC = ""
.Send
End With
Set cdomsg = Nothing
End Sub |
Sub SendEmailUsingYahoo()
Dim NewMail As CDO.Message
Set NewMail = New CDO.Message
'Enable SSL Authentication
NewMail.Configuration.Fields.Item _
("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/smtpusessl") = True
'Make SMTP authentication Enabled=true (1)
NewMail.Configuration.Fields.Item _
("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'Set the SMTP server and port Details
'To get these details you can get on Settings Page of your Yahoo Account
NewMail.Configuration.Fields.Item _
("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mail.yahoo.com"
NewMail.Configuration.Fields.Item _
("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/smtpserverport") = 465
NewMail.Configuration.Fields.Item _
("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/sendusing") = 2
'Set your credentials of your Gmail Account
NewMail.Configuration.Fields.Item _
("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/sendusername") = "tuamail@yahoo.it"
NewMail.Configuration.Fields.Item _
("ACCATITIPI2puntiBARRABARRAschemas.microsoft.com/cdo/configuration/sendpassword") = "********"
'Update the configuration fields
NewMail.Configuration.Fields.Update
'Set All Email Properties
With NewMail
.Subject = "Test Mail da yahoo"
.From = "tuamailCHIOCCIOLAyahoo.it"
.To = "mail1CHIOCCIOLAyahoo.it;mail2CHIOCCIOLAyahoo.it"
.CC = "mail3CHIOCCIOLAvirgilio.it"
.BCC = ""
.textbody = "prova invio email da yahoo"
End With
NewMail.Send
MsgBox ("Mail has been Sent")
'Set the NewMail Variable to Nothing
Set NewMail = Nothing
End Sub |
With NewMail
.Subject = "Test Mail da yahoo"
.From = "tuamailCHIOCCIOLAyahoo.it"
.To = "mail1CHIOCCIOLAyahoo.it;mail2CHIOCCIOLAyahoo.it"
.CC = "mail3CHIOCCIOLAvirgilio.it"
.BCC = ""
.textbody = "prova invio email da yahoo"
' For multiple Attachment you can add below lines as many times
.AddAttachment "f:siti.xls"
.AddAttachment "C:duplicati1.xls"
End With |
Sub SendEmailUsingYahoo()
Dim Da, Text, Ogg, achiTo, achiCC, achiBCC, Att1, Att2, Att3, codice As String
Da = Range("A2") ' indirizzo accesso yahoo
codice = Range("Q2") 'password accesso yahoo
Text = Range("I2") ' Testo - Ricorda che nella cella di inserimento (I2) per andare a capo basta premere Alt Invio
Ogg = Range("H2") ' breve descrizione dell'oggetto del messaggio
achiTo = Range("B2") ' indirizzi di posta elettronica dei destinatari principali.
achiCC = Range("D2") ' indirizzi di posta elettronica dei destinatari in copia conoscenza
achiBCC = Range("F2") ' indirizzi di posta elettronica dei destinatari in copia conoscenza nascosta, ovvero destinatari che riceveranno il messaggio ma il cui indirizzo non apparirà tra i destinatari.
Att1 = Range("J2") ' percorso primo file allegato
Att2 = Range("K2") ' percorso secondo file allegato
Att3 = Range("L2") ' percorso terzo file allegato
Dim NewMail As CDO.Message
Set NewMail = New CDO.Message
'Enable SSL Authentication
NewMail.Configuration.Fields.Item _
("ht tp://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
'Make SMTP authentication Enabled=true (1)
NewMail.Configuration.Fields.Item _
("ht tp://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'Set the SMTP server and port Details
'To get these details you can get on Settings Page of your Yahoo Account
NewMail.Configuration.Fields.Item _
("ht tp://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mail.yahoo.com"
NewMail.Configuration.Fields.Item _
("ht tp://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
NewMail.Configuration.Fields.Item _
("ht tp://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Set your credentials of your Gmail Account
NewMail.Configuration.Fields.Item _
("ht tp://schemas.microsoft.com/cdo/configuration/sendusername") = Da ' user accesso yahoo che viene ritrovato in A2
NewMail.Configuration.Fields.Item _
("ht tp://schemas.microsoft.com/cdo/configuration/sendpassword") = codice ' password accesso yahoo che viene inserito in Q2
'Update the configuration fields
NewMail.Configuration.Fields.Update
'Set All Email Properties
With NewMail
.Subject = Ogg
.From = Da
.To = achiTo
.CC = achiCC
.BCC = achiBCC
.TextBody = Text
' .HTMLBody = "Write your complete HTML Page"
' For multiple Attachment you can add below lines as many times
.AddAttachment Att1
.AddAttachment Att2
.AddAttachment Att3
End With
NewMail.Send
MsgBox ("la Mail è stata inviata.")
'Set the NewMail Variable to Nothing
Set NewMail = Nothing
End Sub |
NewMail.Configuration.Fields.Item _
("ht tp://schemas.microsoft.com/cdo/configuration/sendpassword") = codice ' password accesso yahoo che viene inserito in Q2 |
