Question About Email

I realize this may be an incredibly stupid question and some of you may actually hurt yourselves smacking your forehead in disbelief saying, “DOH! This guy’s hopeless!” but please humor me and my lack of general understanding of all that’s involved in the process of creating an email with an attachment.

I have created a routine that successfully sends an email with the desired attachment (see below) … it works fine. However, this routine (and every example I found in XOJO documentation) requires the user to know their email SMTP server address. Not every user in the environment where my app will be deployed “know” their SMTP address. Sure, they could ask their IT folks to get the answer, but I’m wondering is there any way that the program could “programmatically” get their SMTP address and not require the user to know it off-hand? In essence, I’d like my email routine to appear to the user the same as if they were sending an email from their email client (e.g., Outlook). Now, I realize that their email client “knows” its SMTP address since it was part of the account setup which is why it doesn’t “ask” the user to enter it. Or another way of putting it … can I design a routine that would NOT require the user to enter the SMTP server address???

Remember … don’t hurt that forehead too badly with them “DOH’s” now ^_^;

[code] Dim d As New Date
Dim copyTo As String = txfSendCopyToAddress.Text // cc address
Dim mailTo As String = txfSendToAddress.Text // mail destination address
Dim mailFrom As String = txfSendFromAddress.Text // mail originator addresss
Dim SurveyOriginator As String // name of the Scope Person (mail originator) from the Customer Profile screen

// Open the SQL DB “QuoteCustomer” table and get the name of the person submitting this proposal
Dim db As SQLDatabaseMBS = App.mDB
Dim rsQ As RecordSet = db.SQLSelect(“SELECT * FROM QuoteCustomer WHERE ProposalID=’” + currentQuoteID + “’”)
SurveyOriginator = rsQ.Field(“ScopePerson”).StringValue


SendMailSocket.Address = “” // my SMTP email server
SendMailSocket.Port = 25 // Check your server for the property port # to use

// Create the actual email message
Dim mail As New EmailMessage
Dim TextBody As String
TextBody = "Quote: " + Proposal + EndOfLine
TextBody = TextBody + "Requested by: " + SurveyOriginator + EndOfLine
TextBody = TextBody + "Request Date: " + d.ShortDate + EndOfLine
TextBody = TextBody + "Request Time: " + d.ShortTime + EndOfLine + EndOfLine
TextBody = TextBody + “Excel spreadsheet with quote survey data attached.”

// append User Additional Comments
TextBody = TextBody + EndOfLine + EndOfLine + “Additional Comments:” + EndOfLine + txaUserComments.Text

mail.FromAddress = mailFrom

mail.Subject = “Refacing Scope Quote”
mail.BodyPlainText = TextBody
mail.headers.appendHeader “X-Mailer”,“Refacing Scope Quote”

if copyTo <> “” then mail.AddCCRecipient(copyto)

Dim file As EmailAttachment

// add attachments
file = New EmailAttachment
file.LoadFromFile(SpecialFolder.Desktop.Child(“Refacing Scope Proposal.xls”))

if SpecialFolder.Desktop.Child(“Refacing Scope Proposal.xls”) <> nil AND SpecialFolder.Desktop.Child(“Refacing Scope Proposal.xls”).exists then
MsgBox “File not found”
end if

// Add the message to the SMTPSocket and send it

Check the forum for mentions of Mailchimp, SendGrid or similar. These companies usually have a free tier if you don’t send thousand of mails each day. Then you use the servername and password comination that they supply.

Yes, specific mail clients have specific servers so it is easy for users. But you can also see how to enter your email information on your phone, let’s say an Android:

  • you open mail app (usually Gmail)
  • select Add account
  • then the options are: ‘Google’, ‘Outlook, Hotmail, and Live’, ‘Yahoo’, ‘Exchange and Office 365’ and ‘Other’

Maybe you can do something like that?

If the user say Google or input a @gmail account, then you use gmail smtp servers, if they say hotmail or have @hotmail account you use those.

I use a third party SMTP service ( and code the app to use my account credentials for that. Very inexpensive. Free accounts come with a monthly allowance of 1,000 emails, and an hourly limit of 25 emails. There’s no hourly limit with paid accounts. I pay $5/mnth and have a 2,000 message limit/month, with no hourly limit. And has very fast response time to support questions via email.

Thanks to both of you, Beatrix and Ralph for your inputs. Unfortunately, this particular customer of mine is absolutely anal about 3rd -party solutions. They have had network security breaches that have made them so gun-shy that they even hate to hire me to create an application for them (and I’ve been a consultant for 16 years with them!). I did look at what both of you suggested and I do see how that could work. Perhaps if I reach a dead-end with any other possible alternative, they might reconsider … so, I’ll keep these suggestions in my back pocket.

You are not telling us whether this is a desktop application or a web application. I the case of a web application, you can use a generic mail account, say gmail (or whatever you prefer, or one of the paid services mentioned above). You log the application with the account information and for the rest, your application works just fine. Now, it is a question of selecting an appropriate service for the expected volume.

For a desktop application, it is a bit more difficult. The user needs to tell you what service they will use with the application. I would not use the client,s company email, but a freely available service, simply because for public services, you can find and build into the application the SMTP address, port and security protocols. Therefore I would ask the user to select one service from a list and to provide credentials at setup time or as part of the user-specific configuration. The user supplies the account credentials for the service. You record the credentials in a parameter file (INI, SQLite, or other) and you use the appropriate pre-defined ip addresses that are published for these services. The user would need to select one of the services supported by your application. Most common would be GMail, Hotmail ( and perhaps a few others.

Surely, there will be a client who does not want to use any of the services supported. You will have to have a plan to deal with such exceptions: issue an update with an additional service option, tell the client to use what is supported (that may not be well received…), or offer some other alternative.

Thanks for your input, Alberto. Your idea is certainly one major step forward from where I’m at right now. My only concern is that list of “specific servers” you mention. My experience with Time Warner Cable (now Spectrum) is they had all different kinds of POP/SMTP server addresses depending on where you were in the country and who your “local” provider of TWC services was. For instance, Time Warner South Carolina where I’m at had one set of addresses while Time Warner Ohio had a completely different set. I’ll look into that again to see if the situation has changed.

Sorry for the omission, Louis … it would have helped if I did indicate web vs. desktop. In this particular case, the application IS desktop. Your idea is somewhat akin to the suggestion Alberto made. It’s sounding more and more palatable by the moment. Thanks for your input!

Yet all of these suggestions still require that either the user KNOW the server, or that the application is restricted to use only a server that was predetermined. None of these responses (that I can see) answer the actual question…
To which as far as I know is “No your application cannot programmaticly determine the proper mail server (or user name or password) required to send email”

Unfortunately, you’re right Dave in that the suggestions are a step or more better than what I have, but not the “I know my email address; I know your email address; I’ll send you an email; Done!” scenario I’d hoped for.

On an interesting side note, the thought occurred to me that the SMTP address might be inclusive in the Windows Registry info and possibly attainable from there. In Googling and reading though, I see where there IS info in the registry to that effect, but it seems to be all over the place in terms of content and format based on the email client used … which renders its usage as not very feasible from what I see.

Off the top of my head.

User enters email address and password into your app:


Get from the email using string cutting or regex (which every you prefer)

In Windows call:

nslookup -q=mx

Mac/Linux call:

dig MX

Parse out the returned data to find the server.

Try a TCP port 25 connection to see if it has a listening SMTP server or fire a test email through it with the password you gave above with a sender and recipient the same as the entered email above. If the email arrives you know things are working (not strictly 100% true if I were being pedantic but hey, better than nothing :slight_smile: )

At a guess, this is how Outlook would do it it when you enter an email address and it sets half the stuff up.

If their internal DNS is set up correctly this should work. There are things that could cause this method to fail which is why Outlook allows for manual entry of these details, just in case.

I tried this (macOS)
and it returns THREE(3) servers that “look” like the email servers…
but none of them match what my ISP required me to enter into the mail client (close but not the same)
not to mention that two of them are in fact not correct (from the client point of view)		267	IN	MX	100		267	IN	MX	100		267	IN	MX	100

where in fact what is required [not to mention port number]

And Cox is VERY VERY PICKY… wrong port. it fails. try to use EAST when on west coast, it fails, etc.

Like Dave, I tried it with Windows, Julian. Although I do get returned results, they contain bits and pieces of the correct info but nothing that I could use (parsing or otherwise).

I have used MailgunX and they have a free tier.
Someone (@Phillip Zedalis ??) wrote a MailgunX class to consume the API.

[quote=410082:@]Off the top of my head.

Parse out the returned data to find the server.

Try a TCP port 25 connection to see if it has a listening SMTP server or fire a test email through it with the password you gave above with a sender and recipient the same as the entered email above. If the email arrives you know things are working (not strictly 100% true if I were being pedantic but hey, better than nothing :slight_smile: )

This would 100% fail sending mail to my domains.
One of the first spam tests on incoming mail is to lookup those MX servers for the sending domain.
All devices on the internal network are allowed to send mail.
Otherwise, if the IP/Domain of the sending system isn’t an official/DNS mail server, my system will reject the message.
This stops almost all bot-net spammers from dynamic IPs.
The proper way is to send to the local mail server which will then forward to the destination.
And back to the original question…How to find THAT server.

Well, at least I’m feeling less and less like an idiot for asking the original question … :slight_smile: It just seems strange to me that with the ability to programmatically access just about any piece of info/data on a computer, the “registered” SMTP address of the email client remains unavailable to obtain in the same manner.

I think you’re getting fixated on getting an IP address, when all you really need is the host address. Let DNS resolve that to the correct IP address. SMTPSocket.Address will accept the host name as well as an IP address. Eg.,

Not sure I’m understanding you, Tim. I do use the host address and not the IP address in my routine …

SendMailSocket.Address = "" // my SMTP email server

But my point is that the user would have to know that host address and enter same. Time Warner Cable (now Spectrum), as an example of one provider that comes to mind, has a plethora of POP and SMTP server addresses depending where you are in the country. I had to look mine up on their website each time I set up my Outlook email on my desktop computer or mobile devices (which had a different server address than the desktop even in the same locale). The average user of the application I created here has probably never set up an email account on their own and might not even know what an “SMTP outgoing server” is, let alone the host address.

What am I missing in what you say?

No, I misunderstood the rest of the thread.

It’s pretty standard procedure to have a configuration option where the user enters their data once and you save it for future use. It’s the way they set up their email clients in the first place (unless they’re using something like Microsoft Exchange). But if they are using SMTP, then at some point, either they or their IT people had to configure the client. Every time you change email clients or change computers, you have to configure the new one. Yours is no different. Collect the info and save it somewhere.

Here is a link to a discussion to get the SMTP address from an Outlook Config (and other links on this page):

Here is another one on how to export Outlook settings from the Registry:

You may or may not be able to directly use these techniques but maybe it will help you find your own way.

Try searching in your favorite search engine for “extract outlook configuration” and you can find more.