So our company has decided to eliminate our internal SMTP server and move mail relay to a service called SendGrid which means I have to modify all my internal apps to use this service to end out error emails. I’m pulling my hair out because I cannot get it to work. It is supposed to work with no encryption on port 587 and that appears to be the case with the VB code one of my coworkers has tested but it is not working in Xojo.
Here is the SendGrid relevant documentation page :
This is the VB code that is working:
[code]Sub Main()
Using mail As New System.Net.Mail.MailMessage
mail.From = New System.Net.Mail.MailAddress(“do_not_reply@company.com”)
mail.To.Add(“user.name@company.com”)
mail.Subject = “New SMTP Test”
mail.Body = “This is a test.”
Using smtp As New System.Net.Mail.SmtpClient("smtp.sendgrid.net", 587)
smtp.UseDefaultCredentials = False
smtp.Credentials = New System.Net.NetworkCredential("username", "password")
Try
smtp.Send(mail)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using
End Using
End Sub
[/code]
Here is the equivalent code I’m using:
[code]MailSocket As SMTPSecureSocket
Sub ConnectionEstablished(greeting as string) Handles ConnectionEstablished
Window1.TextArea1.Text = Greeting
End Sub
Sub MailSent() Handles MailSent
MsgBox(“Mail sent.”)
End Sub
Sub ServerError(ErrorID as integer, ErrorMessage as string, Email as EmailMessage) Handles ServerError
MsgBox(“There was an error sending the email. ErrorID=” + Str(ErrorID) + ", " + ErrorMessage)
Window1.TextArea1.Text = Window1.TextArea1.Text + EndOfLine + ErrorMessage
End Sub
SendButton As PushButton
Sub Action() Handles Action
// Connect to SendGrid
MailSocket.Address = “smtp.sendgrid.net”
MailSocket.Port = 587
MailSocket.Username = “username”
MailSocket.Password = “password”
// Create EmailMessage
Dim mail As New EmailMessage
mail.FromAddress = “noreply@company.com”
mail.AddRecipient(ToField.Text)
mail.Subject = SubjectField.Text
mail.BodyPlainText = MessageArea.Text
mail.Headers.AppendHeader(“X-Mailer”,“SMTP Test”)
// Send it
MailSocket.Messages.Append(mail)
MailSocket.SendMail
End Sub[/code]
This always generates the following ConnectionEstablished and ServerError output and the email message doesn’t get sent:
[quote]SG ESMTP service ready at ismtpd0091p1mdw1.sendgrid.net
535 Authentication failed: The provided authorization grant is invalid, expired, or revoked[/quote]
The authentication fails regardless of whether I use the code above or add TLS encryption. I’ve also tried a SSL connection but with that I get no ConnectionEstablished feedback and it isn’t working at all . Everything I try from searching examples fails. Does anyone have any advice? I don’t have a way to contact SendGrid directly because the master account is in someone else’s name. I have only been provided with a username and password to send mail with and that is the same as the one that is working with the VB code.
Note, I can successfully send to our still functioning SMTP server without a login on port 25 so something else is going on with the login that is different from Xojo to VB.