I got an old application that is using aspnet membership controls changepassword and createuserwizard etc. These send emails in the end, via gmail, after sending the email successfully, I get an application_error
The SMTP server requires a secure connection or the client was not authenticated...
The requirement is I can go back to the normal flow of application and ignore this error.
Note that the email is being sent successfully to the client. There is no exception within the procedure/catch.
Protected Sub ChangePwd_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles ChangePwd.SendingMail
Dim obj As SecurePassword = New SecurePassword()
Select Case ConfigurationSettings.AppSettings("WhichSMTP")
Case "gmail"
Try
obj.SendViaGmail(e.Message.Subject, e.Message.Body, e.Message.To.ToString())
Catch ex As Exception
Response.Write("Error sending email via gmail smtp: " & ex.Message)
End Try
End Select
End Sub
SecurePassword.vb
Public Function SendViaGmail(subject As String, body As String, recipients As String)
Dim fromEmail As String = ConfigurationSettings.AppSettings("ContactEmail")
Dim smtpClient As SmtpClient = New SmtpClient("smtp.gmail.com") With {
.Port = 587,
.DeliveryMethod = SmtpDeliveryMethod.Network,
.Credentials = New NetworkCredential(fromEmail, "apppasswordgeneratedviagoogle"),
.EnableSsl = True
}
Dim mailMessage As MailMessage = New MailMessage With {
.From = New MailAddress(fromEmail),
.Subject = subject,
.Body = body,
.IsBodyHtml = True
}
mailMessage.[To].Add(recipients)
If smtpClient IsNot Nothing Then
smtpClient.Send(mailMessage)
End If
End Function
I got the above code from here
few things to note,
I am running this code from my development machine (IISExpress).
previously there was no code written under
Case "gmail"
Update:
I have tried catching it in page_error, it actually comes here but it shows me an empty page now instead of successtemplate from changepassword control
Private Sub Page_Error(sender As Object, e As EventArgs) Handles Me.Error
Dim exc As Exception = Server.GetLastError()
' Handle specific exception.
If exc.GetType = GetType(SmtpException) Then
ChangePwd.SuccessTemplate.InstantiateIn(ChangePassword)
'ChangePwd.ChangePasswordTemplateContainer.Visible = False
End If
' Clear the error from the server.
Server.ClearError()
End Sub
Related
complete code is as below I am fetching the value and sending it in mail but value hard coded ISMS: need to be in bold.
Public Function CCC_GetISMS_Details( ByVal dbPwOHelperPwO As ISingleDbObject) As String
Dim scriptErrorBase As String = "Script Error [CCC_GetISMS_Details]"
Dim fkPwo As IForeignKey = dbPwOHelperPwO.GetFK("UID_PersonWantsOrg")
Dim orderDetail2 As String = String.Empty
Dim DetailStrg As New StringBuilder
'there is a related pwo
If Not fkPwo.IsEmpty Then
Dim pwo As ISingleDbObject = fkPwo.Create()
'orderDetail2 = pwo.GetValue("OrderDetail2").String
'User Story 9672 to fetch the right ISMS Group from request and send it in approval mail
orderDetail2 = pwo.ObjectWalker.GetValue("FK(CCC_UID_CSMGroup).GroupName")
If Not String.IsNullOrEmpty(orderDetail2) Then
DetailStrg.AppendLine(String.Format("ISMS: {0}",orderDetail2))
End If
Else
Throw New Exception(scriptErrorBase & " no related person want org record!")
End If
Return DetailStrg.ToString()
End Function
DetailStrg.AppendLine($"<b>ISMS:</b> {orderDetail2}")
I found several variations to send email via Gmail in VBA and watched a YouTube demo using the sub below.
I tried smtpserverport 25 and 465.
I tried smtpserver smtp.gmail.com and smtp.office365.com.
I have the Microsoft CDO Reference selected.
I verified the Gmail password and get no error messages, no compile errors.
I call the sub from the main routine above via send_email_via_gmail. I added a Msgbox just past the send statement to make sure the sub is called and it does reach it but I never get the emails. I have O365.
Sub send_email_via_gmail()
Dim myMail As CDO.Message
Set myMail = New CDO.Message
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxxxx#gmail.com"
myMail.Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxxx"
myMail.Configuration.fields.Update
With myMail
.Subject = "Test email from Stock App"
.From = "xxxxxxx#gmail.com"
.To = "xxxxxxx#comcast.net"
.TextBody = "Test email from Me to Me"
End With
On Error Resume Next
myMail.Send
Set myMail = Nothing
End Sub
When I remove the On Error statement I do get a run-time error
The message could not be sent to the SMPT server. The transport error code was 0x80040217. The server response was not available.
Don't turn off 2-step verification ... create a custom gmail password for this app.
https://support.google.com/accounts/answer/185833?hl=en
I am trying to use VBA-Web for submitting REST API via Excel VBA and running into issues
https://github.com/VBA-tools/VBA-Web
I tried to modify this example to use HttpBasicAuthenticator but unsuccessful it says HTTP error 401. When I use Postman to submit the rest request it is fine.
https://github.com/revisohq/api-samples/tree/master/rest/excel
If I use this Url that does not need to authenticate. It works as expected
https://rest.reviso.com/customers?demo=true
Private Sub Worksheet_Activate()
Dim Client As New WebClient
Dim Request As New WebRequest
Client.TimeoutMs = 30000 ' 30 seconds
Client.BaseUrl = "https://postman-echo.com/basic-auth"
'Client.BaseUrl = "https://rest.reviso.com/customers?demo=true"
Request.Format = WebFormat.Json
Dim Auth As New HttpBasicAuthenticator
Auth.Setup _
Username:="postman", _
Password:="password"
Dim Response As WebResponse
Set Response = Client.Execute(Request)
If Response.StatusCode <> Ok Then
Sheets("Test").Range("A1") = Response.StatusDescription
Exit Sub
End If
Dim Json As Object
Set Json = WebHelpers.ParseJson(Response.Content)
FillData Json
End Sub
How can I read GMail emails via VBA without having Outlook installed on the PC ?
I googled it but I didn't find any solution which does not relies on Outlook.
You can use the google api to archive this without outlook.
There is VBA-Web on github that supports the gmail api.
Here is an example for gmail with vba-web:
Attribute VB_Name = "Gmail"
' Setup client and authenticator (cached between requests)
Private pGmailClient As WebClient
Private Property Get GmailClient() As WebClient
If pGmailClient Is Nothing Then
' Create client with base url that is appended to all requests
Set pGmailClient = New WebClient
pGmailClient.BaseUrl = "https://www.googleapis.com/gmail/v1/"
' Use the pre-made GoogleAuthenticator found in authenticators/ folder
' - Automatically uses Google's OAuth approach including login screen
' - Get API client id and secret from https://console.developers.google.com/
' - https://github.com/timhall/Excel-REST/wiki/Google-APIs for more info
Dim Auth As New GoogleAuthenticator
Auth.Setup CStr(Credentials.Values("Google")("id")), CStr(Credentials.Values("Google")("secret"))
Auth.AddScope "https://www.googleapis.com/auth/gmail.readonly"
Auth.Login
Set pGmailClient.Authenticator = Auth
End If
Set GmailClient = pGmailClient
End Property
' Load messages for inbox
Function LoadInbox() As Collection
Set LoadInbox = New Collection
' Create inbox request with userId and querystring for inbox label
Dim Request As New WebRequest
Request.Resource = "users/{userId}/messages"
Request.AddUrlSegment "userId", "me"
Request.AddQuerystringParam "q", "label:inbox"
Dim Response As WebResponse
Set Response = GmailClient.Execute(Request)
If Response.StatusCode = WebStatusCode.Ok Then
Dim MessageInfo As Dictionary
Dim Message As Dictionary
For Each MessageInfo In Response.Data("messages")
' Load full messages for each id
Set Message = LoadMessage(MessageInfo("id"))
If Not Message Is Nothing Then
LoadInbox.Add Message
End If
Next MessageInfo
End If
End Function
' Load message details
Function LoadMessage(MessageId As String) As Dictionary
Dim Request As New WebRequest
Request.Resource = "users/{userId}/messages/{messageId}"
Request.AddUrlSegment "userId", "me"
Request.AddUrlSegment "messageId", MessageId
Dim Response As WebResponse
Set Response = GmailClient.Execute(Request)
If Response.StatusCode = WebStatusCode.Ok Then
Set LoadMessage = New Dictionary
' Pull out relevant parts of message (from, to, and subject from headers)
LoadMessage.Add "snippet", Response.Data("snippet")
Dim Header As Dictionary
For Each Header In Response.Data("payload")("headers")
Select Case Header("name")
Case "From"
LoadMessage.Add "from", Header("value")
Case "To"
LoadMessage.Add "to", Header("value")
Case "Subject"
LoadMessage.Add "subject", Header("value")
End Select
Next Header
End If
End Function
Sub Test()
Dim Message As Dictionary
For Each Message In LoadInbox
Debug.Print "From: " & Message("from") & ", Subject: " & Message("subject")
Debug.Print Message("snippet") & vbNewLine
Next Message
End Sub
You can pull GMAIL email data into Excel by adding a custom reference library EAGetMail.
You can find the install link and a demo macro here.
I would like to access the inbox in a gmail account using CDO in VBA.
I have already managed to send a mail message but do not know how to fetch the inbox messages into an excel sheet.
If possible I would like to be able to identify the tags of each message as well.
While the question asks for CDO in particular, from this similar SO question it doesn't look like this is possible directly with CDO.
As an alternative approach to fetching inbox mail messages, Google has recently released a Gmail API that could be consumed with Excel. Below is an example using VBA-Web:
' Setup client and authenticator (cached between requests)
Private pGmailClient As WebClient
Private Property Get GmailClient() As WebClient
If pGmailClient Is Nothing Then
' Create client with base url that is appended to all requests
Set pGmailClient = New WebClient
pGmailClient.BaseUrl = "https://www.googleapis.com/gmail/v1/"
' Use the pre-made GoogleAuthenticator found in authenticators/ folder
' - Automatically uses Google's OAuth approach including login screen
' - Get API client id and secret from https://console.developers.google.com/
' - https://github.com/timhall/Excel-REST/wiki/Google-APIs for more info
Dim Auth As New GoogleAuthenticator
Auth.Setup "Your client id", "Your client secret"
Auth.AddScope "https://www.googleapis.com/auth/gmail.readonly"
Auth.Login
Set pGmailClient.Authenticator = Auth
End If
Set GmailClient = pGmailClient
End Property
' Load messages for inbox
Function LoadInbox() As Collection
Set LoadInbox = New Collection
' Create inbox request with userId and querystring for inbox label
Dim Request As New WebRequest
Request.Resource = "users/{userId}/messages"
Request.AddUrlSegment "userId", "me"
Request.AddQuerystringParam "q", "label:inbox"
Dim Response As WebResponse
Set Response = GmailClient.Execute(Request)
If Response.StatusCode = WebStatusCode.Ok Then
Dim MessageInfo As Dictionary
Dim Message As Dictionary
For Each MessageInfo In Response.Data("messages")
' Load full messages for each id
Set Message = LoadMessage(MessageInfo("id"))
If Not Message Is Nothing Then
LoadInbox.Add Message
End If
Next MessageInfo
End If
End Function
' Load message details
Function LoadMessage(MessageId As String) As Dictionary
Dim Request As New WebRequest
Request.Resource = "users/{userId}/messages/{messageId}"
Request.AddUrlSegment "userId", "me"
Request.AddUrlSegment "messageId", MessageId
Dim Response As WebResponse
Set Response = GmailClient.Execute(Request)
If Response.StatusCode = WebStatusCode.Ok Then
Set LoadMessage = New Dictionary
' Pull out relevant parts of message (from, to, and subject from headers)
LoadMessage.Add "snippet", Response.Data("snippet")
Dim Header As Dictionary
For Each Header In Response.Data("payload")("headers")
Select Case Header("name")
Case "From"
LoadMessage.Add "from", Header("value")
Case "To"
LoadMessage.Add "to", Header("value")
Case "Subject"
LoadMessage.Add "subject", Header("value")
End Select
Next Header
End If
End Function
Sub Test()
Dim Message As Dictionary
For Each Message In LoadInbox
Debug.Print "From: " & Message("from") & ", Subject: " & Message("subject")
Debug.Print Message("snippet") & vbNewLine
Next Message
End Sub