I'm writing a program to send internal emails in VB.Net using the official Microsoft Graph package. At first I got it to send calendar invites and that worked fine for months, but when I tried to add the ability to send emails I keep getting an Internal Server Error.
From looking at other times people have got this error from Graph, it appears to be when a malformed request is sent or when values that don't quite make sense are given. I'm using the constructors from the package and using basically the same construction method as every example online, so the structure of the request can't be wrong, and the only fields that I'm using just take text and don't do anything with it, so I don't think that can be the issue either.
I have the correct permissions in Azure: Screenshot of permissions in Azure, including Mail.Send
Here's my code for constructing the email:
Function DefineImportantEmail(Locations As String, Recipients As List(Of (String, String))) As Message
Dim title = "IMPORTANT OUTAGE NOTIFICATION"
Dim message = "Outage at " & Locations
Dim RecipientList = New List(Of Recipient)
For Each recipient In Recipients
RecipientList.Add(New Attendee With {
.EmailAddress = New EmailAddress With {
.Address = recipient.Item1,
.Name = recipient.Item2
},
.Type = AttendeeType.Optional
})
Next
Return New Message With {
.Subject = title,
.Body = New ItemBody With {
.ContentType = BodyType.Text,
.Content = message
},
.ToRecipients = RecipientList
}
End Function
And here's the code I use to send the email:
Sub SendImportantEmail(Locations As String, Recipients As List(Of (String, String)))
Dim SaveToSentItems = True
Dim graphClient = GetGraphClient()
Dim EventObject = DefineImportantEmail(Locations, Recipients)
Dim request = graphClient.Users("xxxxx#xxxxxxxxxxxxxxx.com").SendMail(EventObject, SaveToSentItems).Request().PostAsync()
request.Wait()
End Sub
The GetGraphClient function works for any other API endpoint I've tried, so I haven't included it. The censored user is from our organisation and is also the user I sent calendar invites from in the first version of this program.
Here's the full error message I get:
(Code: ErrorInternalServerError
Message: An internal server error occurred. The operation failed.
ClientRequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
)
---> Status Code: InternalServerError
Microsoft.Graph.ServiceException: Code: ErrorInternalServerError
Message: An internal server error occurred. The operation failed.
ClientRequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Related
I have an application I have built in Visual Studio using VB.NET that pulls mail messages from an Outlook mailbox and saves the message information into a database and downloads the attachments (if any) into a folder. Before saving the mail message to my database, if the message is from a certain user, I forward the message to another mailbox (and thus, I don't save the message). This is all working fine, except when I try to forward a message with attachments.
As stated in the title, I am using OpenPOP to pull the mail and SMTP to transfer the mail. When I try to create the SMTP attachment from the OpenPOP message I get the following error:
System.InvalidCastException: Conversion from type 'MessagePart' to type 'String' is not valid.
The message is thrown in the AddAttachments function (below) on the line:
myAttachment = New Attachment(attachment) (in the For Each statement)
Public Sub ForwardMessage(
ByVal msgPOP As OpenPop.Mime.Message,
toAddress As String,
fromAddress As String,
subject As String,
body As String
)
Dim smtpServer As New System.Net.Mail.SmtpClient(Me.serverName)
Dim msgSMTP As New MailMessage()
msgSMTP.Sender = New MailAddress(fromAddress)
msgSMTP.To.Add(New MailAddress(toAddress))
msgSMTP.Subject = subject
msgSMTP.Body = body
msgSMTP.IsBodyHtml = True
Dim attachments As Object
attachments = AddAttachments(msgPOP, msgSMTP)
msgSMTP.Attachments.Add(New Attachment(attachments))
smtpServer.Send(msgSMTP)
End Sub
I finally figured it out with some help from a friend. The AddAttachments function below has been edited to show the fix.
Public Function AddAttachments(
ByVal msgPOP As OpenPop.Mime.Message,
ByVal msgSMTP As MailMessage
) As MailMessage
Dim attachments As Object = msgPOP.FindAllAttachments()
Dim myAttachment As Attachment = Nothing
For Each attachment As OpenPop.Mime.MessagePart In attachments
Dim sName As String = attachment.FileName
Dim sContentType As String = attachment.ContentType.MediaType
Dim stream As MemoryStream = New MemoryStream(attachment.Body)
myAttachment = New Attachment(stream, sName, sContentType)
msgSMTP.Attachments.Add(myAttachment)
Next
Return msgSMTP
End Function
I have spent hours researching this issue and I have not found one solution yet. I tried changing the application data type to String and OpenPOP.MIME.MessagePart to no avail. I tried adding "ToString" to the attachment variable and received the following error:
System.InvalidCaseException: Operator '&' is not defined for type 'MessagePart' and string ".ToString".
I have been reading up on MIME to see if that would offer some ideas, though I have not been able to connect the dots. I am assuming this is possible and hoping someone will be able to share the solution, and I will be happy with either VB.NET or C#.NET.
Thank you very much in advance and I appreciate your time.
The solution is in the edited AddAttachments function in my original post above.
So I am working on setting up a new (private) bot for Microsoft Teams that should be able to post messages in a channel on-demand. I already have a bot coded for Google Hangouts Chat, but Microsoft Teams is giving me a really hard time.
I've been searching for over 10 hours now all over the web, and I am very confused.
Right now, all I want to do is post cards to a Microsoft Teams Channel. So I created the connectorclient, I used the baseuri provided when the bot joined the channel (Starts with smba.trafficmanager.net) with my MSAppID and MSAppPassword. Then, I fill in as much information as I can (Maybe too much?) and I submit the information using the connector's .conversations.createconversation.
Namespaces used: Microsoft.bot.connector, Microsoft.bot.connector.teams.models
Here's the code:
Dim Connector As New ConnectorClient(New Uri("https://smba.trafficmanager.net/amer/"), "MSAPPID", "MSAPPPASSWORD")
Dim conversation As New ConversationParameters
Dim activity2 = Activity.CreateMessageActivity
Dim bot As New ChannelAccount
bot.Id = "BOTID"
bot.Name = "EDD Bot Test"
conversation.Bot = bot
Dim chaninfo As New ChannelInfo
chaninfo.Id = "CHANID"
chaninfo.Name = "General"
Dim teaminfo As New TeamInfo
teaminfo.Id = "TEAMID"
teaminfo.Name = "EDD"
activity2.Text = "Test"
activity2.ServiceUrl = "https://smba.trafficmanager.net/amer/"
activity2.Type = ActivityTypes.Message
activity2.From = bot
activity2.ChannelId = "msteams"
Dim tenantdata As New TenantInfo
tenantdata.Id = "TENANTID"
Dim teamschanneldata As New TeamsChannelData
teamschanneldata.Channel = chaninfo
teamschanneldata.Team = teaminfo
teamschanneldata.Tenant = tenantdata
activity2.ChannelData = teamschanneldata
conversation.Activity = activity2
Response.Write(JsonConvert.SerializeObject(conversation))
Try
Dim reqresp As ConversationResourceResponse = Connector.Conversations.CreateConversation(conversation)
Response.Write("ActivityID: " & reqresp.ActivityId & ", ServiceURL: " & reqresp.ServiceUrl & ", ID: " & reqresp.ServiceUrl)
Catch ex As ErrorResponseException
Response.Write(ex.Response.Content & " " & ex.Response.ReasonPhrase)
End Try
This is what the API returns:
{"message":"Authorization has been denied for this request."} Unauthorized
Keep in mind, I'm not even 100% sure I'm using the right method to create the channel message, I figured it was either CreateConversation or ReplyToActivity.
I gave the app Users.ReadWriteAll permissions too, so am I missing something? That error leads me to think it doesn't have anything to do with the ConversationParameters payload but something to do with authentication.
Problem was solved by using MicrosoftAppCredentials.TrustserviceUrl for both the serviceUrl and the endpoint message.
I research regarding retrieving or displaying emails from gmail without using outlook and it led me here on using Gmail API. I'm confused because I don't have idea on how to solve my problem.
Here's the code:
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
I'm using the blank file from this GitHub Link that enables this code above. I was stuck on this error:
Still I don't have the idea what's the output of this API because it's my first time using this one. I have my id and secret already from Gmail API i just removed it from the code. I guess it was really hard if I don't have someone I can do brainstorming regarding on this matter.
Hoping someone have encountered this problem or anyone is interested. Thanks.
I'm using vb.net (4.0) to interact with the DocuSign API. I'm trying to make a process that allows a user to add 30 days to the current expiration date instead of logging into DocuSign.net to correct the envelope. The code seems to work fine (doesn't throw any errors) but the correction doesn't happen on DocuSign's side.
Me.EnvelopeID is the Envelope's ID
DocuService is the namespace of the DocuSign API Service Reference.
Me.AuthorizationString is the Username, Password, Account# and Integrator Key to send as HTTP headers.
Private Sub UpdateExpiration()
'Get envelope details
Dim orig As DocuService.Envelope = ExecuteSoap(Function(client) client.RequestEnvelope(Me.EnvelopeID, False), Me.AuthorizationString)
Dim cor As New DocuService.Correction
cor.EnvelopeID = Me.EnvelopeID
cor.Reminders = orig.Notification.Reminders
cor.Expirations = orig.Notification.Expirations
cor.Expirations.ExpireAfter = (Integer.Parse(orig.Notification.Expirations.ExpireAfter) + 30)
'Execute Correction
Dim cord As DocuService.CorrectionStatus = Me.ExecuteSoap(Function(client) client.CorrectAndResendEnvelope(cor), Me.AuthorizationString)
'If I add a break point on the next line and check the values of cord,
'there is a returned CorrectionStatus object but every property in the object is "Nothing"
Dim check As DocuService.Envelope = ExecuteSoap(Function(client) client.RequestEnvelope(Me.EnvelopeID, False), Me.AuthorizationString)
Console.WriteLine(check.Notification.Expirations.ExpireAfter & " " & (Integer.Parse(orig.Notification.Expirations.ExpireAfter) + 30))
If check.Notification.Expirations.ExpireAfter = (Integer.Parse(orig.Notification.Expirations.ExpireAfter)) Then
'Success :)
MsgBox("success!")
Else
'Failure :(
MsgBox("failure!")
End If
End Sub
Private Function ExecuteSoap(Of TResult)(func As Func(Of DSAPIServiceSoapClient, TResult), authorizationString As String) As TResult
Using client As New DocuService.DSAPIServiceSoapClient(My.Settings.DocusignMode)
Using scope As OperationContextScope = New System.ServiceModel.OperationContextScope(client.InnerChannel)
Dim hp As HttpRequestMessageProperty = New HttpRequestMessageProperty
hp.Headers.Add("X-Docusign-Authentication", authorizationString)
OperationContext.Current.OutgoingMessageProperties(HttpRequestMessageProperty.Name) = hp
Return If(func IsNot Nothing, func(client), Nothing)
End Using
End Using
End Function
We use the same ExecuteSOAP function and AuthorizationString to create & send envelops, and do recipient updates so I know these are correct. I'm not sure whats wrong!
This is most likely 1 of 3 possibilities.
I'm surprised its not error-ing out, but you should not be putting your accountId in the http auth header. See page 19 the SOAP PDF guide:
http://www.docusign.com/sites/default/files/DocuSignAPI_Guide.pdf
There is something else not configured correctly with your SOAP API call. Inspect your raw outgoing request and ensure the xml is what you expect. Post the raw request here if not sure.
A bug with DocuSign. First rule out the other two options and if no dice still post a comment here and I can get a bug logged on DocuSign's side. Also a good test would be to make the correction call through the REST api to see if you can get that to work.
I have an application was working fine and all the email functionality was working fine.
From yesterday, I started getting below error
Error Message:Service not available, closing transmission channel. The server response was: 4.3.2 Service not available, closing transmission channel
My VB.net code for sendmail is given below:
Public Sub SendMessage(ByVal toAddress As String, ByVal ccAddress As String)
Try
Dim message As New MailMessage()
Dim client As New SmtpClient()
'Set the sender's address
message.From = New MailAddress(fromAddress)
If (toAddress.Trim.Length > 0) Then
For Each addr As String In toAddress.Split(";"c)
message.To.Add(New MailAddress(addr))
Next
End If
If (ccAddress.Trim.Length > 0) Then
For Each addr As String In ccAddress.Split(";"c)
message.CC.Add(New MailAddress(addr))
Next
End If
message.BodyEncoding = Encoding.UTF8
message.Subject = Subject
message.Body = Body
message.IsBodyHtml = True
client.Send(message)
Catch ex As Exception
ErrorHandler.WriteError(ex.Message)
End Try
End Sub
Please suggest what can be cause behind this error and do let me know how can I fix this issue.
There isn't anything wrong with your code. This part of the error message:
4.3.2 Service not available, closing transmission channel
Is actually coming from your mail server, and the framework is simply passing the error message on to your application, and throwing it as part of the exception.
4.x.x errors are usually temporary, and are meant to be retried. Typically mail servers are overloaded when they throw a 400 error.
Check your email and see if it starts working.
Instead of (or in addition to) smtp authentication, some email servers will allow mail to be sent for 30 minutes (or some length) after a client computer checks POP email. If this is the case, it can make an application that does not use smtp authentication appear work sometimes and not work sometimes, with no change in the code.