Send simple email from VB.net - vb.net

I usually send email using a full-featured library (Chilkat) but I want to try to compose the email manually to examine differences. I'm using this for starters:
Imports System.Net.Mail
Dim mail As New MailMessage()
mail.From = New MailAddress("MyMail#Gmail.com")
mail.To.Add("MyMail#yahoo.com")
mail.Subject = "Test1 "
mail.IsBodyHtml = False
mail.Body = "This is a test."
Dim smtpServer As New SmtpClient
smtpServer.Credentials = New Net.NetworkCredential("MyMail#Gmail.com", "MyMailPWD")
smtpServer.Port = 465
smtpServer.Host = "smtp.gmail.com"
smtpServer.EnableSsl = True
smtpServer.Timeout = 3000
smtpServer.Send(mail)
MsgBox("The mail Is sent!", MsgBoxStyle.Information)
But I keep getting a timeout error on the Send command. The credentials are all good because it works fine using Chilkat. I also know that I am reaching smtp.gmail.com because it's not happy if I switch to port 587. How can I get a detailed report of the exchange for debugging purposes? Thanks.

Gmail no longer allows basic SMTP access by default. It used to be you could get around this by enabling a "Less Secure Apps" option, but now this is gone, too, or soon will be (certain types of accounts are phasing this out more slowly, but it IS going away).
Instead, you have to use a modern authentication mechanism like OAuth, or create a per-App password.

Related

How to run an application remotely while using a server's resources

I thought this would be a simple task but found I was very wrong apparently. We have a working application that resides in a Windows Server 2016 system This app needs to be triggered remotely by a general user who clicks on a shortcut pointed to the .exe on the server. The problem is that the app needs to be able to run using the server's resources so that an email can be sent (only servers are capable of sending emails).
It seems that the app runs with the user's resources so the email never gets sent. I'm not sure if I need to add some type of scripting (VB.Net) or if I need to configure the .exe/server to enable this function. This works when a user with Admin rights runs the app but we need a regular User to be able to do the same. I have tried using PsExec but still had the same problem. (The user is using Windows 10)
Does anyone have any ideas that can make this happen?
This is the code we're using for the mail function:
Dim client As New SmtpClient()
Dim mail As New MailMessage()
mail.From = New MailAddress("email#email.com")
client.Credentials = New Net.NetworkCredential("username", "password")
mail.[To].Add("email#email.com")
mail.[To].Add("email#email.com")
'set the content
mail.Subject = "Subject: " & Date.Now
mail.Body = "Body: " & Date.Now
client.Host = "host.host.host.com"
client.Port = "25"
client.UseDefaultCredentials = True
Try
client.Send(mail)
Catch exc As Exception
Finally
mail = Nothing
client = Nothing
End Try
Catch ex As Exception
' MsgBox("Error, DB Open " & ex.Message)
End Try

Change sql server from visual basic form

I want to let the users change the sql server connetion manually from a settings form because the application will be used in many company
i tried this:
i create a string in settings "sqlservername"
then in the loading form i put a textbox "txt_sqlservername" and savebutton
the button event
My.Settings.sqlservername = Me.txt_sqlservername.Text
My.Settings.Save()
MsgBox("Server Name Was Updated", MsgBoxStyle.Information, "Videos Database - New Server Name")
Me.Close()
and this is my connection string
Public SQLCon As New SqlConnection With {.ConnectionString = "Server=My.Settings.sqlservername;Database=Videos;User=sa;Pwd=123456;"}
when i open my main form nothing shown
any help?
You can give a form that asks for user input for the connection string and then save the connection string in the application settings.
You can use this connection string from the settings every time you define a connection in the application.
The string formatting is wrong where the connection is defined.Make correction like shown below:
Public SQLCon As New SqlConnection With {.ConnectionString = "Server=" & My.Settings.sqlservername & ";Database=Videos;User=sa;Pwd=123456;"}

VB.net Suggestions for SMTP client connection error, trying to send message through exchange server

So I'm trying to connect to our exchange server and send a message through vb.net using the smtpClient. Here is my code:
Dim smtp As New SmtpClient("exchangeserver.com")
Dim mail As New MailMessage
mail.From = New MailAddress("me#me.com")
mail.To.Add("me#me.com")
mail.Subject = "Test Email"
mail.Body = "Testing body."
Try
smtp.Send(mail)
Catch exc As Exception
Console.WriteLine(exc.ToString)
End Try
The exception I'm getting indicates that:
System.Net.Mail.SmtpException: Failure Sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions ##.##.#.##:25
Seems like I can't connect on that port? No permissions... should I try something else or revert back to sending emails through outlook?
one thing to check... you may need to set credentials... look into this
smtp.Credentials = New Net.NetworkCredential( "blah blah blah", "yada yada")

Sending email to users in vb.net

ok so im trying so send a email to a user who enters their email into a textbox. i got a basic idea of whats i should do but i am confused where i should put for , CMTPClient(),
the credentials, and from. what email should go there. i tryed puting in my email address and credentials but i keep getting this error " the SMTP server requires a sercure connection or the client was not autherticated". here is my code
Try
Dim username As String
username = TextBox1.Text
Dim SmtpServer As New SmtpClient("smtp.gmail.com")
Dim mail As New MailMessage()
SmtpServer.Credentials = New System.Net.NetworkCredential("what username goes
here", "what password goes here")
SmtpServer.Port = 587
mail = New MailMessage()
mail.From = New MailAddress("what email should i put here")
mail.To.Add(username)
mail.Subject = "Qustions"
mail.Body = "This is for testing your mother"
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
use SmtpServer.EnableSsl = True before calling SmtpServer.Send(mail)
"the SMTP server requires a secure connection or the client was not
authenticated"
means the SMTP Server works on a SSL Enabled (encrypted) connection
Keep in mind that gmail isn't just open for all to use. Otherwise, well, you know: spam. Right now spammers have to jump through all kinds of hoops to find or create usable smtp gateways. If any of the major providers left themselves open, think of all the spam you get right now and multiply it by about a 1000.
So you need to have an account with gmail to use their smtp server. More than that, if you send more than about 400 e-mails per day you may need either a special account with them, or an account with a special, reputable mass mailer. These mailers will charge you per message, but it's worth it, because otherwise more than a few messages per day will attract notice and result in anything you send being filtered as spam... and this while still in transit, before it even reaches the destination's mail host to be checked there as well. You're basically paying a nuisance fee, to ensure that your messages are in earnest, and not mass junk.
That out of the way, if you know your volume is low enough, and after you have a valid gmail account, you just need a few changes:
Try
Dim SmtpServer As New SmtpClient("smtp.gmail.com")
Dim mail As New MailMessage()
SmtpServer.Credentials = New System.Net.NetworkCredential("gmailusername#gmail.com", "your gmail password")
SmtpServer.EnableSsl = True
SmtpServer.Port = 587
mail = New MailMessage()
mail.From = New MailAddress("gmailusername#gmail.com")
mail.To.Add(TextBox1.Text)
mail.Subject = "Questions"
mail.Body = "This is for testing your mother"
SmtpServer.Send(mail)
MsgBox("mail sent")
Catch ex As Exception
MsgBox(ex.ToString)
End Try

"Pinging" an email address using VB.Net coding

Is there a way in VB.Net to "Ping" an email address to see if that email is a real one that does not give any errors?
If yes, can you show what the VB.Net coding looks like to implement this?
I plan to use this in an app that requires the Customer email and it would be nice to validate it as the call taker enters it into a form before saving the Customer details.
Here is the code we are using to send an email promotion to all of the customers in our customer table:
Private Sub RibbonButtonSendTestEmail_Click(sender As System.Object, e As System.EventArgs) Handles RibbonButtonSendTestEmail.Click
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
Dim strSqlStatement As String = "Select CustomerName, Email " & _
"From Customers "
Using objSqlCommand As SqlCommand = New SqlCommand(strSqlStatement, ObjConnection)
With objSqlCommand
' Open the SqlConnection before executing the query.
'---------------------------------------------------
Cursor = Cursors.WaitCursor
ObjConnection.Open()
Dim objDataReader As SqlDataReader = .ExecuteReader()
' Go through all the customers and send out the promotion emails.
'----------------------------------------------------------------
If objDataReader.HasRows Then
SmtpServer.Host = TextBoxSMTPServer.Text
SmtpServer.Port = TextBoxPort.Text
If TextBoxUseSSL.Text = "Yes" Then
SmtpServer.EnableSsl = True
Else
SmtpServer.EnableSsl = False
End If
If TextBoxUseDefaultCredentials.Text = "Yes" Then
SmtpServer.UseDefaultCredentials = True
Else
SmtpServer.UseDefaultCredentials = False
End If
SmtpServer.Credentials = New Net.NetworkCredential(TextBoxUserName.Text, TextBoxPassword.Text)
While objDataReader.Read()
Try
mail.To.Add(objDataReader("Email").ToString)
mail.From = New MailAddress(TextBoxEmailFrom.Text)
mail.Subject = "Promotion: " & TextBoxID.Text
mail.Body = "Dear " & objDataReader("CustomerName") & "," & vbCrLf & vbCrLf & TextBoxPromotionBodyText.Text
SmtpServer.Send(mail)
Catch exSMTP As SmtpException
MessageBox.Show("Sorry, I could not send an email for: " & _
vbCrLf & objDataReader("CustomerName") & "." & vbCrLf & _
"Please make sure it is correct.", _
"Error")
Catch exFormat As FormatException
MessageBox.Show("Sorry, this customer's email is not properly formatted: " & _
vbCrLf & objDataReader("CustomerName") & "." & vbCrLf & _
"Please make sure it is correct.", _
"Error")
End Try
End While
LabelEmail.Text = "Sent email promotions to the customers."
End If
objDataReader.Close()
ObjConnection.Close()
Cursor = Cursors.Default
End With ' objSqlCommand
End Using ' objSqlCommand
End Sub
Yes it's perfectly possible:
1 DNS Lookup the MX records for the domain
There may be multiples, you can pick anyone, although technically the one with the lowest preference indicator is the prefered one.
2 TCP Connect to the mail server (port 25)
Say hello: HELO
Identify yourself: mail from:<test#example.com>
Say who you're writing to: rcpt to<testaddress#example.com>
At this point the server will reply with a response, you'll get an OK or a 550 error with a message (like: The email account that you tried to reach does not exist)
Disconnect and the message will be dropped.
But dude, you want the VB code to do this? You just need a DNS talking piece and TCP connection building piece (or likely there are some SMTP libraries out there that'll do all this for you - or provide you with inspiration to figure it out yourself). Don't forget you can probably find a C# code example that does it and use Visual Studio's conversion tool to switch it to VB.
Note
Many domains have black holes/catch alls... the former will accept any email address and just delete it if it's invalid, the latter will accept any email address and forward them to a central account (no guarantees as to what happens then... sell the sender's address to spammers?) In both cases you won't get the 550 message, so you can never be certain.
There most reliable way to do this is to send a test email and have the recipient verify receipt by clicking on a link, which you then read and mark the email as active.
You should do primitive checks on the syntax of the email using regular expressions, but beyond that the most reliable way to validate an email is to attempt delivery and confirm the receipt.