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

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;
}

Related

Liferay 7 MailService.sendMail from a custom portlet doesn't work only on HOST SERVER

I have a strange problem with a custom portlet on Liferay 7 to solve:
MailService.sendMail is working from MY COMPUTER with google smtp and a personal account: this means that the code is working...
On my HOST SERVER sending emails with the final-smtp works correctly: I tried both root and liferay user with telnet final-smtp port.
On the Liferay server on HOST SERVER, send e-mails works correctly: if I forget the password, Liferay send me the e-mail.
But ... if I try to send email with my portlet from Liferay on HOST SERVER it doesn't work without any error. I'm using Liferay MailService.sendMail. I post the code but it works (on MY COMPUTER).
I get the service in this way:
#Reference(unbind = "-")
protected void setMailService(MailService mailService) {
_mailService = mailService;
}
And the calling code is in the following:
InternetAddress fromAddress = null;
String newsletterPrefix = null;
InternetAddress toAddress = null;
try {
String smtpUser = PropsUtil.get(
"newsletter.send.mail.smtp.user");
String smtpToUser = PropsUtil.get(
"newsletter.send.mail.smtp.to.user");
if (Validator.isNotNull(smtpUser)) {
fromAddress = new InternetAddress(smtpUser);
}
if (Validator.isNotNull(smtpToUser)) {
toAddress = new InternetAddress(smtpToUser);
}
}
catch (Exception e) {
_log.error(e, e);
result = false;
}
MailMessage mailMessage = new MailMessage(
fromAddress, toAddress, subject, body, true);
mailMessage.setBCC(addressList);
_mailService.sendEmail(mailMessage);
There could be several reasons behind this, some not even code related.
I see you are using this code for a newsletter, which suggests you are using a smtp service that is meant for this.
It could be that your server is in fact sending the email, with success, but the smtp server is simply blocking, rejecting or marking to resend later. Moreover, that server might be configured to not send an error message, or sending an error message in the form of a successful delivery, but the data contains the error.
I would start checking you mail server configuration, and the accounts permissions, then its logs.
Also, you might consider using plugins for mass mail delivery, like this one: https://www.e-systems.tech/blog/-/blogs/connecting-liferay-to-mailgun
Few things you can do to debug this problem:
Make sure you are deploying the intended code on HOST machine. (Silly suggestion, but many times this is the problem.)
Try to set following package's Log level to ALL/DEBUG to see if it shows any problem in logs.com.liferay.mail.service

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);
}

PHPMailer not able to send send email with ec2

I'm using PHPmailer to send account verification mail, I'm using AWS ec2 instance, however, that mailer is working fine in localhost but when I upload that to server emails are not going,
at first, i used SendGrid credentials to send emails, failed, then tried Gmail SMTP, failed, and somewhere I read that ec2 can't send emails, then I created SES also, still can't able to send.
searched on the web abt that but no answers are fixing my problem,
in localhost, in can send emails with the same code and with SendGrid of Gmail credentials, why I can't send with the server?
my PHP mailer code is:
$sub = "Thankyou For registration! Confirm Your mail to Login";
$mailBody = "<h1>You are successfully registered<br />Visit site to login</h1>";
require 'mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = "tls://email-smtp.us-east-1.amazonaws.com"; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "smtp_username"; // SMTP username
$mail->Password = "smtp_password"; // SMTP password
// $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom("my_mail_id#gmail.com", "SMTP_REPLAY_NAME");
$mail->addReplyTo("my_mail_id#gmail.com", "SMTP_REPLAY_NAME");
$mail->addAddress("recipient_mail_id#gmail.com"); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sub;
$mail->Body = $mailBody;
if(!$mail->send()) {
echo 'Message could not be sent.';
} else {
echo 'Message has been sent';
}
it shows Message has been sent but I cant receive emails, checked in spam folder also, no clue of mail!
even I have openSSL certificate also! opened SMTP port for both inbound and outbound in security group of ec2, everything working fine but PHPMailer!
Get your protocols straight. In the Host you're specifying tls, but telling it to connect to Port = 465, which will not work with TLS. Either change your Port to 587 (preferred) or change your encryption method to ssl. Enabling debug output (SMTPDebug = 2) will let you in on what's happening in the conversation with the server.
A perusal of the troubleshooting guide would probably help.

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.

visual basic gmail submission not working

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.