forked from farishadi/Excel_Macro_References
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendEmailViaVBAMethod1
More file actions
77 lines (62 loc) · 2.38 KB
/
SendEmailViaVBAMethod1
File metadata and controls
77 lines (62 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Public Sub emailAlgo()
Dim myGmailAddress As String
Dim myGmailPassword As String
Dim mbMsg As String
Dim mbTitle As String
Dim mbStat As Integer
Dim Mail As New Message
Dim Cfg As Configuration
ThisWorkbook.Activate
myGmailAddress = ActiveSheet.OLEObjects("TextBox1").Object.Value
myGmailPassword = ActiveSheet.OLEObjects("TextBox2").Object.Value
mbMsg = "Confirm that the file to load is the " & ActiveSheet.OLEObjects("TextBox4").Object.Value & "?"
UserName = myGmailAddress
Password = myGmailPassword
ToAddress = myGmailAddress
Subject = "Test EMAIL from FARIS HADI MSG P1"
'Subject = "Fwd: Agilent / Keysight and Cisco 02RI ,02RA,02RS,04RB Aging status as of " & Format(Now, "MM/DD/YYY")
HTMLMessage = "Hello All. Attached is the Agilent, Cisco and Keysight 02RI Aging Summary"
SmtpServer = "smtp.gmail.com"
Attachment = ActiveSheet.OLEObjects("TextBox3").Object.Value
'CHECK FOR EMPTY AND INVALID PARAMETER VALUES
If Trim(UserName) = "" Or InStr(1, Trim(UserName), "@") = 0 Or Trim(Password) = "" Then
MsgBox "Email or password is unavailable. Please check the fields and try again."
Exit Sub
End If
If ActiveSheet.OLEObjects("TextBox3").Object.Value = "" Then
MsgBox "No file found! Please re-run the macro to output .jpg file."
Else
mbStat = MsgBox(mbMsg, vbYesNo, mbTitle)
If mbStat = vbNo Then
MsgBox "User cancelled. Program terminate."
End
End If
'On Error Resume Next
Set Cfg = Mail.Configuration
'SETUP MAIL CONFIGURATION FIELDS
Cfg(cdoSendUsingMethod) = cdoSendUsingPort
Cfg(cdoSMTPServer) = SmtpServer
Cfg(cdoSMTPServerPort) = 465
Cfg(cdoSMTPAuthenticate) = cdoBasic
Cfg(cdoSMTPUseSSL) = True
Cfg(cdoSendUserName) = UserName
Cfg(cdoSendPassword) = Password
Cfg.Fields.Update
On Error GoTo ErrHandler
With Mail
.From = UserName
.To = ToAddress
.Subject = Subject
.HTMLBody = HTMLMessage
If Attachment <> "" Then
.AddAttachment Attachment, olByValue, 0
End If
'SEND EMAIL
.Send
End With
MsgBox "Done!"
Exit Sub
ErrHandler:
MsgBox "Email or password error! Please check and make sure your email and password is correct and try again. Try your previous password (from before your last reset) if you are still unsucessful."
Exit Sub
End Sub