Emailing in vb.net - SMTP server issues, or anonymous emailing? - vb.net

I've got an emailing system in my application which I have used fine and have seen it working.
The customer has also managed to send emails using the program, however, once they've paid for the application, they're then selling it on to other customers.
They've just taken it in to test with the first customer and they're having troubles sending emails, so I investigated the error log and saw the following message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [BN6PR2201CA0027.namprd22.prod.outlook.com]
Now, after Googling this exact error message, the top result was this question, in which it's clear to see that the OP was trying to send a message from an email address that was hard-coded as "emailaddress". However, that isn't the case for me.
My code is as follows:
recipient = eMail
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential(senderAdd, senderPass)
Smtp_Server.EnableSsl = False
Smtp_Server.Host = SMTPserver
AddHandler Smtp_Server.SendCompleted, AddressOf sendComplete
e_mail = New MailMessage()
e_mail.From = New MailAddress(senderAdd)
e_mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess
e_mail.To.Add(eMail)
e_mail.Subject = subj
e_mail.IsBodyHtml = False
e_mail.Body = bod
Smtp_Server.SendAsync(e_mail, userState)
Is there anything in here which would be causing this error? If so, what is it?
The variables are all set from a database table, and if there are any null values then the system will pop a message box to alert the user of this and then exit the subroutine and not send the email, so it's not an issue with null values - The only thing I can think of is that the credentials are wrong, but they've supposedly been verified by 3 different people as correct.

You might need to specify the domain. Use this constructor of the NetworkCredential class and pass in the domain.
Smtp_Server.Credentials = New Net.NetworkCredential(senderAdd, senderPass, "expectedDomainName")
Difficult to say for sure though because we can't reproduce this issue under your same conditions.

Related

No longer able to send e-mails with VBA and google smtp - Additional Info

Similar problem to "Resca's" Utility to send email to Gmail stopped working after google implemented OAuth. I have an excel workbook using VBA to ONLY SEND individual gmails to multiple “google group” members. It had been working for 4 years until Gmail added Oauth.
I have done tests with OAuth code from "Email Architect". I can test the app in test mode and it will send the email if I click passed the alarms and add include read. Compose, send and delete permissions. When I configure it as production I get not verified message and go through a similar routine that "test" needed. When I try to verify the Gmail API it requires a domain which I don't have. This is run under a single user xxx#gmail with owner permission from my disk. After seeing Resca's post, I think I may be over doing the OAuth.
I am not a programmer, but have vintage experience with assembler, macro and micro code, but not high level languages. Self taught VBA/Excel
I got parts of this code from "Jean-François Corbett" # https://stackoverflow.com Tonyyy at MRexcel.com
Set CDO_Mail = CreateObject("CDO.Message")
On Error GoTo Error_Handling
Set CDO_Config = CreateObject("CDO.Configuration")
CDO_Config.Load -1
Set SMTP_Config = CDO_Config.Fields
With SMTP_Config
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxx#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxx"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
'25 & 465 are valid ports and sometimes may fail. Change to the other if you can't connect
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Update
End With
With CDO_Mail
Set .Configuration = CDO_Config
End With
CDO_Mail.Subject = strSubject
CDO_Mail.From = strFrom
CDO_Mail.To = strTo
CDO_Mail.TextBody = strBody
CDO_Mail.CC = strCc
CDO_Mail.BCC = strBcc
CDO_Mail.Send
To be clear Google did not add Oauth2. The option of using Xoauth2 with the smtp server has always been there. Nor is google forcing you to use Xoauth2 with the smtp server.
What google did was remove the option for Less secure apps & your Google Account. Which allowed you to use the google account password to connect to the smtp server.
If you have 2fa enabled on the google account you can create an apps password and use that password instead of the actual password for the google account in your code.
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "AppsPassword"
If you do not want to use an apps password then yes you will need to configure Xoauth2. xoauth2-protocol Its been a very long time since i have touched VBA I dont think this is something i can help with. But a quick google search shows it should be supported Oauth

Email attachment changing to a text file

I have an issue where I'm sending a PDF file as an attachment, but when the email arrives with the client, it's arriving as a file with a name very similar to the MIME type, but with the correct content. I specify an attachment file name of "Statement.pdf", but it arrives as "application_pdf.txt" which then confuses the receiver as it doesn't open as a PDF. The mail sending is handled by a Windows Service written in Visual Basic.
Here is the main bit of code that actually sends it:
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential(mailAddress, mailPassword)
SmtpServer.Port = mailPort
SmtpServer.Host = mailServer
SmtpServer.EnableSsl = mailSSL
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
mail = New MailMessage()
If fromName <> "" Then mailName = fromName
mail.From = New MailAddress(fromAddr, mailName)
mail.IsBodyHtml = True
mail.To.Add(toAddr)
If replyTo <> "" Then
mail.ReplyToList.Add(replyTo)
End If
If cc <> "" Then
mail.CC.Add(cc)
End If
mail.Subject = subject
mail.Body = body
If attached <> "" And attachMIME <> "" And attachname <> "" Then
mail.Attachments.Add(Attachment.CreateAttachmentFromString(attached, attachMIME))
mail.Attachments.Last().ContentDisposition.FileName = attachname
End If
SmtpServer.Send(mail)
The various variables in here are passed in from the calling code and I'm confident that they're correct, in that if the password, port etc were incorrect the email wouldn't send. In particular the attachMIME is "application/pdf" which appears to be correct according to pdfa.org.
The PDF is generated on another machine, and passed to this one using FTP in binary mode. This service then reads the PDF into a string and passes it into the mail sending function:
Dim attachBytes() As Byte = Nothing
attachBytes = My.Computer.FileSystem.ReadAllBytes(FileDir & "\" & msg.attachFile)
mailAttachFile = ""
For Each attbyt As Byte In attachBytes
mailAttachFile &= Chr(attbyt)
Next
I thought I'd cracked it because I was originally using ReadAllText which strips out any non-printable characters, and I imagined that the mail transport somewhere was checking the PDF for validity, seeing the unprintable characters had changed and presuming it was some sort of mal-formed file. Unfortunately changing to ReadAllBytes didn't seem to help, and the PDF file is still arriving as a file named "attachment_pdf.txt", still with the correct content. If I save the file from my email client and then rename it, it displays perfectly as a PDF in Acrobat Reader. My customer is receiving emails from other companies with PDF attachments, so it's not just that his email provider doesn't trust PDFs.
I don't simply use AddAttachment(filename) to load the attachment because the code works in two ways - one where the service downloads the email files from a remote FTP server, and the other (this one) where the files are sent to the machine running the service and the service opens them locally. In order to keep the actual mail sending the same, loading the files is done further "up" the code and the resulting email message and attachment passed in here via a function call. So by the time I get to the first code block I posted above, the code doesn't know (or care) where the message and attachment came from.
Is there anything glaringly obvious in what I have posted that might be causing this issue? I should add that I have the same code running on another machine, using gmail as the mail transport (which this one does not) and the other method I mentioned above and it sends a PDF attachment every day without issue.
(Edited to clarify that the attachment file contents are not changing, just the attachment name is changing, which confuses the receiver.)

Overload Resolution failed because

First, I know next to nothing about coding, but...
We have a vb file that we use so users can enter requests which in turn sends emails to various distribution lists. I did not create this file, it has been here long before I arrived.
It has worked fine until now and "nobody" made any changes.
The error we get is:
error BC30516: Overload resolution failed because no accessible 'New' accepts this number of arguments. msg = New System.Net.Mail.MailMessage("itrequest#bnhc.org")
The code goes like this:
' third Message
msg = New System.Net.Mail.MailMessage("itrequest#******.org")
msg.IsBodyHtml = True
subj = "New Hire Form Confirmation"
There are two other emails before this one, both coded the same way, just different destinations.
Any help is appreciated!
If this was working before then someone definitely modified it. You are creating an Instance MailMessage() without providing all of the parameters
msg = New System.Net.Mail.MailMessage("itrequest#******.org")
lists the sending email only however MailMessage requires a sending and receiving email
Dim msg = New System.Net.Mail.MailMessage("itrequest#******.org", "recipient#google.com")
replace recipient#google.com with your email recipient and you will no longer have this issue
I hope this helps

Can't Send Email using Visual Basic from Windows 7 pc: "The operation has timed out"

When trying to send an email using a Visual Basic Console App I get an error: "The operation timed out". I'm sending from a Windows 7 pc. Can this be done?
I've found many posts that say it fails because there is no SMTP client on Windows 7. However, I downloaded smpt4dev and get the same results when I try to connect to that via "localhost".
But I can telnet to the server from the same pc and get no errors. I can also send an email using these telnet commands:
telnet SMTP.domain.xyz 25
helo SMTP.domain.xyz
mail from: f#domain.com
rcpt to: t#domain.com
data
subject: test email
.
quit
I have tried this with a few different smtp servers and I get the same results. I know that my server name, port, username and pw are correct.
Imports System.Net.Mail
Try
Dim SMTPClientObj As New Net.Mail.SmtpClient
SMTPClientObj.UseDefaultCredentials = False
SMTPClientObj.Credentials = New System.Net.NetworkCredential(username, password)
SMTPClientObj.Host = "mysmtpserver"
SMTPClientObj.Port = 25
SMTPClientObj.EnableSsl = False
Dim e_mail As New MailMessage()
e_mail.From = New MailAddress("test#test.com")
e_mail.To.Add("test2#test.com")
e_mail.Subject = "Email Sending Test"
e_mail.IsBodyHtml = False
e_mail.Body = "Test email from VB"
SMTPClientObj.Send(e_mail)
Dim x As New Mail.SmtpStatusCode
Console.WriteLine("SmtpStatusCode: " & CStr(x))
Console.WriteLine("Mail Sent.")
Catch ex As Exception
Dim x As New Mail.SmtpStatusCode
Console.WriteLine("SmtpStatusCode: " & CStr(x))
MsgBox(ex.Message)
MsgBox(ex.ToString)
MsgBox(ex.InnerException)
End Try
At this point I'd just like to be able to get more details on the error instead of the simple timed out message. But I've never used the SmtpStatusCode before and am not sure how to use it.
The ex.InnerException prompt is always blank.
Can you give me any pointers on how to get more detailed error info? Or what I need to do to get this to work on my windows 7 pc?
Thank you
Turns out Digital Guardian was blocking port 25. McAfee wasn't the problem nor was Group policy.
Thanks to everyone for their assistance.

VB .Net Sending email using Gmail account

Why does this timeout?
Dim s As New SmtpClient
s.Host = "smtp.gmail.com"
s.Port = 465
s.EnableSsl = True
s.Timeout = 5000
s.Credentials = New NetworkCredential("my.name#gmail.com", "mypassword")
Dim m As New MailMessage
m.To.Add("my.name#gmail.com")
m.From = New MailAddress("my.name#gmail.com")
m.Body = "Test Message"
m.Subject = "Test Subject"
s.Send(m)
These settings come straight from my Outlook Express test setup, and it can send fine.
Set really long timeout = does nothing
Change port numbers to 587 or 25 = does nothing
One thought: Outlook Express has a "My server requires authentication" option, which I couldn't see an obvious equivalent for with SmtpClient. Could it be related to that?
Thanks in advance
Dave
--Trindaz on Fedang #vb.net-smtp
Edit: You might need to add the following line: s.UseDefaultCredentials = False before the line starting with s.Credentials...
Do you have a firewall or some kind of anti-virus program running that might be blocking the connections?
A good place to start is to just do a simple connect from the command line.
telnet smtp.gmail.com 465
Note, depending on Windows version you might have to enable the telnet client first, see this link for details.
You have to go into your gmail account and turn on access for less secure apps and change the port to 587