SSL help needed

My app enables users to send mails to their clients, using the smtp settings of the user. I am struggling to do this for years now, sometimes it works and now it doesn’t. Where do I get the SSL certificate, or does that mean, I have to provide for the user to enter their SSL certificate password?

Is it better, to share my own smtp settings to enable them to send e-mails - and again, where to I find my certificate?

Also, in the past - if it worked - I could get it running for “normal” ISP’s but not for iCloud… Tried to do it with the app specific password, but no good…

Please show your code. I recently added code in my app so that users can send themselves emails. I had to add a check to get the SSL version like this with the Chilkat plugin:

dim theSocket as new Chilkat.Socket
dim success as Boolean = theSocket.Connect(SMTPServer, Port, False, 1 * 1000)
if not success then
  globals.theErrorLog.DialogErrorProceed(kErrorMailSent + " " + kError + " " + parseError(theSocket.LastErrorText))
  Return False
end if

// Check the negotiated TLS version
Dim negotiatedTlsVersion As String = theSocket.AlpnProtocol
If negotiatedTlsVersion <> "h2" Then
  mailman.UncommonOptions = "DisableTls13"
End If
call theSocket.Close(1 * 1000)

Thanks Beatrix, here is my code:

MailReady.Backdrop = EmailGreen25
StatusLabel.Text = “Connection for e-mail successfully established”
try
If VARServerEmail = “” then
'do nothing if empty
else
Var i As Integer
Var s As String
// set up the socket
MailSocket.Address = VARServerSMTP
MailSocket.Port = VARServerPort
MailSocket.Username = VARServerUsername
MailSocket.Password = VARServerPassword
MailSocket.SSLEnabled = VARServerSSL
// populate the email message
Var mail As New EmailMessage
mail.FromAddress = VARServerEmail
mail.Subject = “Manual test of mail connection”
mail.BodyPlainText = “Test is successful!”
mail.Headers.AppendHeader(“X-Mailer”,“Yoga Studio © 2023”)
// add recipients
s = ReplaceAll(“nickolbe@icloud.com”, “,”, Chr(13))
s = ReplaceAll(s, Chr(13)+Chr(10), Chr(13))
For i = 1 To CountFields(s, Chr(13))
mail.AddRecipient(Trim(NthField(s, Chr(13), i)))
Next
// send the email
MailSocket.Messages.AddRow(mail)
MailReady.Backdrop = EmailGreen25
MailSocket.SendMail
// Check if the email sending was successful
'If MailSocket.Error = “” Then
If MailSocket.WriteError = False then
MessageBox “Email sent successfully!”
Else
MessageBox "Failed to send email: " + MailSocket.WriteError.ToString
’ Change colour indicator red
MailReady.Backdrop = EmailRed25
End If
end if
Catch e As IOException
MessageBox "IO Error: " + e.Message
’ Change colour indicator red
MailReady.Backdrop = EmailRed25
Catch e As NilObjectException
MessageBox "Null Object Error: " + e.Message
’ Change colour indicator red
MailReady.Backdrop = EmailRed25
Catch e As RuntimeException
MessageBox "Runtime Error: " + e.Message
’ Change colour indicator red
MailReady.Backdrop = EmailRed25
End try

Please edit your post and:

  1. Select all the code with the mouse

  2. Click on the </> button

Thanks.

Then you’ll have:

MailReady.Backdrop = EmailGreen25
StatusLabel.Text = “Connection for e-mail successfully established”

which is more easily readable.

1 Like

Your code is missing SSLConnectionType. You only get this information which SSLConnectionType to use with my code.

These days you can assume that everything these days uses some form of SSL. So

MailSocket.SSLEnabled = VARServerSSL

is not necessary anymore.

1 Like

Is it not enough to do it in the MailSocket?
Mailbox

That should be fine. Have you checked the server responses with the Error and ServerError events? Sometimes you get messages telling you what the problem is.

When Microsoft was rolling out the feature where the rejected connections that weren’t secure enough, they were doing it at random so that some would send and some would not. The SSL negotiation was eventually fixed in Xojo 2022r2, however there is now a known issue with SendGrid.

Which mail services are failing is an important factor when debugging this kind of problem.

1 Like

Which button?

In the post editor on the forum.

You can also use three backticks if you’re familiar with Markdown formatting.

2 Likes
MailReady.Backdrop = EmailGreen25
StatusLabel.Text = "Connection for e-mail successfully established"
try
  If VARServerEmail = "" then
    'do nothing if empty
  else
    // set up the socket
    MailSocket.Address = VARServerSMTP
    MailSocket.Port = VARServerPort
    
    MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
    MailSocket.SMTPConnectionType = SMTPSecureSocket.SMTPConnectionTypes.SSLTLS
    
    MailSocket.SSLEnabled = VARServerSSL
    MailSocket.Username = VARServerUsername
    MailSocket.Password = VARServerPassword
    // populate the email message
    Var mail As New EmailMessage
    mail.FromAddress = VARServerEmail ' from e-mail address
    mail.Subject = "Manual test of mail connection"
    mail.BodyPlainText = "Test is successful!"
    mail.Headers.AppendHeader("X-Mailer","Yoga Studio © 2023 Nic Kolbe")
    // add recipients
    mail.AddRecipient("nickolbe@icloud.com") // The recipient's email address
    // send the email
    MailSocket.Messages.AddRow(mail)      
    MailReady.Backdrop = EmailGreen25
    MailSocket.SendMail
    // Check if the email sending was successful
    'If MailSocket.Error = "" Then
    If MailSocket.WriteError = False then
      MessageBox "Email sent successfully!"
    Else
      MessageBox "Failed to send email: " + MailSocket.WriteError.ToString
      ' Change colour indicator red
      MailReady.Backdrop = EmailRed25
    End If
  end if
Catch e As IOException
  MessageBox "IO Error: " + e.Message
  ' Change colour indicator red
  MailReady.Backdrop = EmailRed25
Catch e As NilObjectException
  MessageBox "Null Object Error: " + e.Message
  ' Change colour indicator red
  MailReady.Backdrop = EmailRed25
Catch e As RuntimeException
  MessageBox "Runtime Error: " + e.Message
  ' Change colour indicator red
  MailReady.Backdrop = EmailRed25
End try

Wow, clever… I’ll try that, thanks!

The missing SSLConnectionType and SMTPConnectionType was the solution! Thanks for that Beatrix!