visual basic gmail submission not working - email

I have the following piece of code which does not seem to work. The error message is below the code.
Dim smtpserver As New SmtpClient()
Dim mail As New MailMessage()
smtpserver.Credentials = New Net.NetworkCredential("myemail#gmail.com", "mypassword")
smtpserver.Port = 465
smtpserver.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("myemail")
mail.To.Add("myemail#gmail.com")
mail.Subject = "Test"
mail.Body = "testing "
smtpserver.Send(mail)
MsgBox("Mail Sent")
Here is the error message:
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.
w6sm4604909qas.26 - gsmtp
What does this mean? Why is this not working?

Gmail does not accept unencrypted and/or unauthenticated email submission. Your code needs to use STARTTLS like the error message states, and authenticate before trying to send anything.

Related

Problem sending emails via PHPMailer due to DNS settings

This is my first post so please be gentle.
I've been trying to send automatic SMTP emails via PHPMailer using my webhost as the server. Below is the script I am using.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require '../PHPMailer/src/Exception.php';
require '../PHPMailer/src/PHPMailer.php';
require '../PHPMailer/src/SMTP.php';
//Load Composer's autoloader
//require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 4; //SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->Host = 'surrey.redbackinternet.net';
$mail->SMTPAuth = true;
$mail->Username = 'noreply#mydomain.com';
$mail->Password = 'password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
//Recipients
$mail->setFrom('noreply#mydomain.com', 'xxx');
$mail->addAddress('contact#mydomain.com');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Previously, I was using Gmail as the server, which worked fine, but now I want to change this. When I run the script with the new details I get authentication error - "SMTP ERROR: Password command failed: 535 Incorrect authentication data". The full PHPMailer script log is shown in the image below.
PHPMailer log
I know the username and passwords used are correct and have seen this error seems to be returned whenever a connection cannot be established. I have a hunch it's related to my email DNS settings, though I could be completely wrong here. When I check my DNS and MX settings using dnschecker.org all seems ok as far as I can naively tell. But using hardenize.com I see my domain fails their mail server and email TLS checks. The information provided is lacking but it does say "A network error occurred while we were trying to communicate with a server. Error message: java.net.SocketTimeoutException: Read timed out". I should add here that my site uses Cloudflare as a CDN.
Any ideas on what the issue is or how to resolve it?
Thanks in advance.

Not receiving email on Gmail using Gmail SMTP but receiving on web server using my server SMTP

I am trying to send contact form to my gmail when user submit the form, but I have not receiving any email on Gmail when I am using Gmail SMTP server, I am trying both tls with port 587 & ssl with port 465.
But when I used my server SMTP it's working properly and I also received email on my web server. Code is working on PLESK but not working on Cpanel. Code is here:
<?php
require('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "**********#gmail.com";
$mail->Password = "**********";
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("**********#gmail.com");
$mail->Subject = "Website Contact Us Form Email";
$mail->WordWrap = 80;
$mail->Body = "User Name: ".$_POST["name"]."<br/><br/>"."User Phone Number: ".$_POST["phone"]."<br/><br/>"."User Email: ".$_POST["email"]."<br/><br/>"."User Message: ".$_POST["message"]."<br/><br/>";
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']);
}
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "<p class='error'>Problem in Sending Mail.</p>";
} else {
echo "<p class='success'>Contact Mail Sent.</p>";
}
?>
Assuming your authentication credentials are correct, it sounds like this could be a networking issue. Have you confirmed that your firewall(s) have permitted outbound traffic on port 587? The local mail agent may be listening on localhost and sending the mail back to you that way.
Also, since SMTP is used to SEND messages, I believe the from address should actually be the same as the authenticated account (you're sending this email to yourself, not from the user's email account). However, if you're using two different email's this doesn't apply completely.

Sending email from specific server gives socket forbidden error

I get this error
An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.xxx.xxx:587 when sending email from a godaddy server.
I have seen many questions like this but what's unique here is that, this code works on my local computer. It also works on my other Go daddy hosting Server.
This original server has TLS 1.0, because i needed TLS 1.2, I purchased a deluxe hosting plan and moved my code to this new server, then i start getting this error. I've searched everywhere and used every combination of port 587, 465, 25 along with ssl = false or true.
Any ideas please?
using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
MailMessage mail = new MailMessage();
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("********#gmail.com", "********");
smtpClient.EnableSsl = true;
string fromEmail = "********#gmail.com";
mail.From = new MailAddress(fromEmail, "System");
mail.To.Add(new MailAddress(toEmail));
mail.Body = body.ToString();
mail.Subject = subject;
smtpClient.Send(mail);
}
Sounds like a firewall or AV or other port blocking software preventing outbound connections to port 587. Check your server config and look in the windows event log as there might be an entry in there indicating who did the blocking.
Try to use port 2525 for 587, 465, 25. Some cloud providers disable all outbound traffic from 587, 465, 25 ports.
It seems like Godaddy is blocking emails from its servers when you use an outside smtp like smtp.gmail.com. At least that seems like the case with this Plesk Hosting Account. The other Economy Hosting works well with Gmail smtp.
Also, the emails will only send from Godaddy server, running the code locally on Visual studio gave an error.
I changed my code to this:
using (SmtpClient smtpClient = new SmtpClient("relay-hosting.secureserver.net", 25))
{
MailMessage mail = new MailMessage();
smtpClient.Credentials = new NetworkCredential("yourdomain#yourdomain.com", "****");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
string fromEmail = "yourdomain#yourdomain.com";
mail.From = new MailAddress(fromEmail, "Name");
mail.To.Add(new MailAddress(toEmail));
mail.Body = body.ToString();
mail.Subject = subject;
smtpClient.Send(mail);
}

Send mail in C# without Password

I send email using below code. Its working fine. But how to send email without password. If I give empty string in senderPassword, I got below error
"Additional information: Transaction failed. The server response was: 5.7.1 : Client host rejected: Access denied".
I googled and found similar questions, but I didn't get correct solution. Please help me to solve this.
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.Host = "myhost.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential(senderID, senderPassword);
smtp.Timeout = 30000;
MailMessage message = new MailMessage(senderID, To, Subject, Body);
smtp.Send(message);
You need to change the authentication on the SMTP server. If you don't own the SMTP server then this is out of your control.

.NET - Mail server doesn't send mail through SmtpClient.Send

I wrote a single console application (just a part of a site code, but it must work apart too, and it has the same fault result as inside the site) (C#):
MailMessage message = new MailMessage("login#ourDomenInPunycode", "toMail")
{
Subject = "Hello",
Body = "Hello world"
};
SmtpClient client = new SmtpClient();
client.Host = "ourIP";
client.Credentials = new System.Net.NetworkCredential("login#ourDomenInPunycode", "ourPassword");
client.Port = 25;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
So, isn't to send e-mail should be trivial? But wherever I send mail from local machine through our mail server (just running this console application), the following exception appears:
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Relaying to denied (authentication required)
If I change the "login#ourDomenInPunycode" data to my own mailbox (at gmail or something else - no matter), all works fine. It also not depend from "toMail" address.
So, what could be wrong with our mail server? Any special settings? We use Windows Server 2008 virtualized inside another Windows Server 2008 and Kerio Connect 7 as mail server at virtual Windows Server 2008. All other mail programs like Outlook works well with sending e-mails from our mail server.
All articles which I read in Internet about SmtpClient's settings have only these (above) trivial settings and code, nothing special.
UPDATE
I done some fixes in text above.
Here is a part of log of our mail server when I tried to send mail through console application launched from the mail server virtual PC ("mail.ourDomen.local" related to "ourIP" above):
Task 215 handler BEGIN
Task 215 handler starting
SMTP server session begin; client connected from mail.ourDomen.local:49399
Sent SMTP greeting to mail.ourDomen.local:49399
Command EHLO OurMailServer
Sent reply to EHLO: 250 mail.ourDomenInPunycode ...
Command MAIL FROM:<login#ourDomenInPunycode>
Sent reply to MAIL: 250 2.1.0 Sender <login#ourDomenInPunycode> ok
Command RCPT TO:<toMail>
Sent reply to RCPT: 550 5.7.1 Relaying to <toMail> denied
Connection to SMTP server mail.ourDomen.local lost: connection closed by remote host.
SMTP server session end
Task 215 handler END
"Sent reply to RCPT: 550 5.7.1 Relaying to denied" -
Why this happened?
Well, we use this description
https://kb.kerio.com/article/550-571-relaying-to-email%40addresscom-denied-authentication-required-411.html .
Although we know about this settings, but we tangled with our virtual machines. We have a virtual machine for the web server and another one for the mail server. Permissions were configured for the mail server virtual machine only in the Kerio Connect, not for the web server. We just added permission for the virtual machine of the web server and the mail is sent normally.
And the "ourIP" in the
SmtpClient client = new SmtpClient();
client.Host = "ourIP";
is the IP of our virtual machine of the mail server. No settings of IP of the web server virtual machine in the SmtpClient object.
As suggested your mail server needs to be configured to allow "Relaying" over port 25. It is the "Relaying" setting/config you are looking for.
The idea/purpose behind "Relaying" is stop your server being (ab)used for sending spam.
Try the code without setting client.Host and client.DeliveryMethod properties.
//used this referances
using System.Net.Mail;
using System.Net;
using System.IO;
try
{
string em_from = "your seding e mail";
string em_to = Ricever e mail Address;
SmtpClient Smtp_Server = new SmtpClient();
MailMessage e_mailx = new MailMessage();
Smtp_Server.UseDefaultCredentials = false;
Smtp_Server.Credentials = new System.Net.NetworkCredential("sender email address", "sender passsword");
Smtp_Server.Port = 25; //your mail server port
Smtp_Server.EnableSsl = false;
Smtp_Server.Host = "192.XXX.XX.XX"; //your mail server IP
e_mailx = new MailMessage();
e_mailx.From = new MailAddress(em_from);
e_mailx.To.Add(em_to);
e_mailx.Bcc.Add("BCC Address");//you cad add both BCC and CC addresss
e_mailx.IsBodyHtml = false;
e_mailx.Subject = esub;
e_mailx.Body = ebody;
e_mailx.Attachments.Add(new Attachment(emsg)); //emsg mean attach file name with location
Smtp_Server.Send(e_mailx);
return 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return 0;
}