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

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")

Related

Send simple email from 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.

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

vb.net reconnecting lost connection automatically to mysql server

Any idea on reconnecting lost connection automatically using vb.net (windows form) to mysql server.
I am planning to develop an application. To be specific, it is a timer for an internet cafe.
The case is if the server downloaded some updates and need to restart, the client pc will reconnect automatically when the server boot up. Any idea? Thanks
I'm using this code to connect:
Dim DatabaseName As String = "xxx"
Dim server As String = "xxx"
Dim userName As String = "xxx"
Dim password As String = "xxx"
If Not conn Is Nothing Then conn.Close()
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
Try
conn.Open()
MsgBox("Connected")
Catch ex As Exception
MsgBox(ex.Message)
End Try
conn.Close()

Problems with DataAdapter at VB.Net in a connection to SQL Server 2008

I´m experimenting troubles on a local connection to SQL Server 2008, it´s throwing me the next especific error:
A network-related or instance-specific error ocurred while establishing a connection to SQL Server. The server was not found or was not accesible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections (error 40)
I´ve tried with most of solutions concerning to SQL Services and Firewall Solutions, so i think the problem is specific in the source code, so it is:
Private Sub cargar_Combo(ByVal ComboBox As ComboBox, ByVal sql As String)
Dim strConexion As String = "Data Source=Angel-PC\SQLEXPRESS1;Initial Catalog=sistemaReferencias;Integrated Security=True"
Dim conexion As New SqlConnection(strConexion)
Try
conexion.Open()
Dim cmd As New SqlCommand(sql, conexion)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
ComboBox.DataSource = ds.Tables(0)
ComboBox.DisplayMember = ds.Tables(0).Columns(1).Caption.ToString
ComboBox.ValueMember = ds.Tables(0).Columns(0).Caption
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If conexion.State = ConnectionState.Open Then
conexion.Close()
End If
End Try
End Sub
I know that the String connection is right, because i used it in another method, the error comes specifically at the "da.Fill(ds)" line, those are the basics
I really appreciate any help you can provide.
Go to your desktop. Right click and add a new text file, "test.txt".
Rename the text file to test.udl, you will get a warning, just accept it.
Double click test.udl, and you will have an interactive dialog to configure your connection string.
Once you have configured the connection string. Click save. The right click the test.udl file and open in notepad. This will give you the connection string that you need to put into your app.

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