What is wrong with this code that should send some email with google? - vb.net

Shared Sub sendMail(ByVal title As String, ByVal content As String)
Dim SmtpServer As New SmtpClient("smtp.gmail.com", 465)
SmtpServer.EnableSsl = True
SmtpServer.Credentials = New Net.NetworkCredential(username, password)
Dim mail As New MailMessage(username, username, title, content)
SmtpServer.Send(mail)
End Sub
I got the result of
System.Net.Mail.SmtpException: 'Failure sending mail.'
IOException: Unable to read data from the transport connection: net_io_connectionclosed.
What did I do wrong? Should I enable IMAP first at google?
My code is based on How do I send a gmail email in vb.net?
If I use port 587 instead of 465 I got
System.Net.Mail.SmtpException: 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at'
Using port 25 yield
SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.200.109:25
Using 465 yield the original error

Related

SMTP Error "The remote certificate is invalid according to the validation procedure."

I have written a VB.NET code that should sent email via my server. My server uses SSL which is self signed (I think) (R3/Let's Encrypt). The following code gives error "The remote certificate is invalid according to the validation procedure.". If I turn off SSL then I get error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first". How to solve it ?
Dim MyMailMessage As New MailMessage()
MyMailMessage.From = New MailAddress("support#mysite.com", "My Site")
MyMailMessage.Subject = "Test"
MyMailMessage.Body = "<BR><B>This is a test email</B><BR><BR>Bye"
MyMailMessage.IsBodyHtml = True
Dim SMTP As New SmtpClient("mail.mysite.com")
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential("support#mysite.com", "password_here")
' add to address
MyMailMessage.To.Add("abc#gmail.com")
' Send Email
SMTP.Send(MyMailMessage)
MyMailMessage.To.Clear()

The server response was: Authentication required even though authentication is provided

IONOS are not helpful. Especially as all of a sudden, a form stops sending an email.
This article explains and helps very well Sending email from webform using SMTP server
The error I get is the same: The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required
Here's my VB - I took on board the advice here and made the From the same as the authentication but still no luck. Originally it was objMM.From = New MailAddress(txtEmail.Text). I did also read that port 25 could work but this didn't work either. It was originally 587.
Sub btnSendFeedback_Click(sender as Object, e as EventArgs)
'Create an instance of the MailMessage class
Dim objMM as New MailMessage()
'Set the properties - send the email to the person who filled out the
'feedback form.
objMM.To.Add("receiver#receiver.com")
objMM.From = New MailAddress("website#domain.co.uk")
'Send the email in text format
objMM.IsBodyHtml = False
'Set the priority - options are High, Low, and Normal
objMM.Priority = MailPriority.Normal
'Set the subject
objMM.Subject = "Subject"
'Set the body
objMM.Body = " various long boring fields here . . . "
Dim smtp As New SmtpClient()
smtp.Host = "smtp.ionos.com"
smtp.EnableSsl = True
smtp.UseDefaultCredentials = False
smtp.Credentials = New Net.NetworkCredential("website#domain.co.uk", "passwordhere")
smtp.UseDefaultCredentials = True
smtp.Port = 587
'Send method of the SmtpMail class
smtp.Send(objMM)
End Sub
Is there anything else wrong here I wonder? IONOS are sure it is a script problem.
The fine manual for SmtpClient.Credentials says:
Some SMTP servers require that the client be authenticated before the server will send email on its behalf. To use your default network credentials, you can set the UseDefaultCredentials to true instead of setting [smtpClient.Credentials] property. If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server. If the UseDefaultCredentials property is set to false and the Credentials property has not been set, then mail is sent to the server anonymously
As a result, I would expect your code to contain only one mention of UseDefaultCredentials, for purposes of setting it to False..
Unless of course, you have these same credentials configured as the default credentials in the CredentialCache.. in which case mentioning them again here is redundant

Use CDO Send Emails in Excel VBA Not Working

I am trying to send email within Excel VBA. Based on my research on Internet, I enabled Microsoft CDO for Windows 2000 Library and composed my VBA codes as below:
Sub SendCDOMail()
Dim objCDOMsg As Object
Set objCDOMsg = CreateObject("CDO.Message")
'CDO Configuration
With objCDOMsg.Configuration.Fields
'
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Server port (typically 25, 587)
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
'SMTP server IP or Name
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'SMTP Account User ID
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx#bbbb.com"
'SMTP Account Password
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxxx"
'Use SSL for the connection (False or True)
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
.Update
End With
'CDO Message
objCDOMsg.Subject = "TEST Subject"
objCDOMsg.From = "xx#xx.com"
objCDOMsg.To = "xx#xx.com"
objCDOMsg.TextBody = "TEST Body"
objCDOMsg.Send
End Sub
However, I received the error saying:
The server rejected the sender address. The server response was: 530.5.7.57 SMTP;
Client was not authenticated to send anonymous mail during MAIL FROM
I also tried the same configurations using PowerShell and it worked (smtpserver, username, password, server port...).
I don't know where it might go wrong.
Here it is: https://technet.microsoft.com/en-us/library/mt210446(v=exchg.150).aspx - note that this is in the Office365 section for "How to set up a multifunction device or application to send email using Office 365,", so there may be other things in there for you too.
This indicates that you are connecting to the SMTP client submission endpoint (smtp.office365.com), which can't be used for direct send. For direct send, use the MX endpoint for your Office 365 tenant, which ends with "mail.protection.outlook.com." You can find your MX endpoint by following the steps in Full configuration instructions for direct send.

Email Function will not work properly in Visual Studios

I am trying to generate an email function where it sends the user an email, however for some reason it is just not working properly and it does not give any error messages.
It just brings up a message box stating that "There is a problem at this time. Please try again later". This is message box which appears when the email fails to be sent, however could you please help to resolve by issue.
Error Message says
It says that 'sends the email. SMTP.Send(email)- An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at –
Please view the code below.
imports System.Net.Mail
Public Class EmailF
'assigns a new message to the email
Dim email As New MailMessage
'smtp is the server that has been used to send the email
Dim smtp As New SmtpClient
'assigns a new email address
Dim mail As System.Net.Mail.MailAddress
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
'selects which email address to send the email from...
email.From = New MailAddress("blood4all.business#gmail.com")
email.Subject = SubBox.Text
email.Body = MessBox.Text
email.To.Add(ToBox.Text)
'the smtp outlook host.
Dim SMTP As New SmtpClient("smtp.gmail.com")
'the smtp port.
SMTP.Port = 587
SMTP.EnableSsl = True
'the login details for the gmail account.
SMTP.Credentials = New System.Net.NetworkCredential("blood4all.business#gmail.com", "Blood123")
'starts the progress bar.
Timer1.Start()
ProgressB.Show()
'sends the email.
SMTP.Send(email)
'pop up box conirms that the email has been sent.
MsgBox("The e-mail has been sent!")
Catch ex As Exception
MsgBox("There is a problem at this time. Please try again later")
ProgressB.Hide()
End Try
End Sub
The exception message is :
An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at ...
The credentials you pass are used only if you set the UseDefaultCredentials property to false before you set the new credentials. Otherwise your Windows credentials will be used.
Changing the value of this property clears the Credentials property, which is why you need to set it before passing the new credentials:
SMTP.UseDefaultCredentials = False
SMTP.Credentials = New System.Net.NetworkCredential("blood4all.business#gmail.com", "Blood123")
EDIT
You'll also have to "allow less secure applications" from your GMail account settings, as described in this SO question and the GMail support site. Normally, GMail requires applications to use either an application key.
SMTP (the protocol) doesn't have this functionality so you need to modify your account settings to allow connecting without an application key
I have now got this working as I created a new gmail account, maybe my all gmail account consisted of network and security issues which has now been amended. Thank for help and much appreciated.

Getting Error while sending the email in .NET

I have an application was working fine and all the email functionality was working fine.
From yesterday, I started getting below error
Error Message:Service not available, closing transmission channel. The server response was: 4.3.2 Service not available, closing transmission channel
My VB.net code for sendmail is given below:
Public Sub SendMessage(ByVal toAddress As String, ByVal ccAddress As String)
Try
Dim message As New MailMessage()
Dim client As New SmtpClient()
'Set the sender's address
message.From = New MailAddress(fromAddress)
If (toAddress.Trim.Length > 0) Then
For Each addr As String In toAddress.Split(";"c)
message.To.Add(New MailAddress(addr))
Next
End If
If (ccAddress.Trim.Length > 0) Then
For Each addr As String In ccAddress.Split(";"c)
message.CC.Add(New MailAddress(addr))
Next
End If
message.BodyEncoding = Encoding.UTF8
message.Subject = Subject
message.Body = Body
message.IsBodyHtml = True
client.Send(message)
Catch ex As Exception
ErrorHandler.WriteError(ex.Message)
End Try
End Sub
Please suggest what can be cause behind this error and do let me know how can I fix this issue.
There isn't anything wrong with your code. This part of the error message:
4.3.2 Service not available, closing transmission channel
Is actually coming from your mail server, and the framework is simply passing the error message on to your application, and throwing it as part of the exception.
4.x.x errors are usually temporary, and are meant to be retried. Typically mail servers are overloaded when they throw a 400 error.
Check your email and see if it starts working.
Instead of (or in addition to) smtp authentication, some email servers will allow mail to be sent for 30 minutes (or some length) after a client computer checks POP email. If this is the case, it can make an application that does not use smtp authentication appear work sometimes and not work sometimes, with no change in the code.