System.Net.Mail Server Response was 5.7.1 - c#

I am building a windows forms application for a school that has a very tight network (meaning the person I am building it for has to ask their IT services to do everything).
This application sends emails out and I am using System.Net.Mail library to do so.
SMTPServer = new SmtpClient("SMTPAddress");
MailMessage mailObj = new MailMessage("admin#xyz.com", emailAddressTo);
mailObj.IsBodyHtml = true;
mailObj.Subject = "Subject";
mailObj.Body = "<h2>Test E-Mail Message from the TSENS</h2>";
SMTPServer.Credentials = new System.Net.NetworkCredential(SMTPUserName, SMTPPassword);
SMTPServer.DeliveryMethod = SmtpDeliveryMethod.Network; //This is new code
SMTPServer.Send(mailObj);
I'm wondering if this line: SMTPServer.DeliveryMethod = SmtpDeliveryMethod.Network; will solve the latest error message he got when trying to send out an e-mail:
Is there something else that I am missing?
Just in case you are wondering the SMTPUserName and Password is his e-mail username and password that he uses to send and receive mail.

I presume that the SMTPUsername his mailadress isn't "admin#xyz.com" which is used as the sender of the mailmessage.
The mailserver seems to validate if the person doing the send is allowed to send mails on behalf of admin#xyz.com which appearantly isn't the case.
According to the SMTP specs error 5.7.1 stands for "Unable to relay" which is what you try to do.

emailClient = new SmtpClient(yourEMAILSERVER);
emailClient.Send(yourMailMessageObject); // in your case "mailObj" that you have defined already
I would avoid using SMTPServer class or set its Credentials or DeliveryMethod properties.

Related

How to send email from a C# program

I would like to add email functionality to a WinForm program I'm writing in C#. I have an Android app that has email functionality. What it does is set up the email but then lets the user choose the email program, etc. Once that is chosen the email body is completed. But it's up to the use to select what email app they want to use.
I would like to do the same in Windows but I don't see how. I have tried the following (based on other questions and responses here) :
_from = new MailAddress("my email address", "xxxx");
_to = new MailAddress("xxxx3333#gmail.com", "yyyy");
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = true;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp msgMail = new MailMessage();
smtp.Body = text;
msgMail.Subject = "Subject";
msgMail.From = _from;
msgMail.To.Add(_to);
smtp.EnableSsl = true;
msgMail.Subject = _subject;
msgMail.Body = Text;
msgMail.IsBodyHtml = false;
try
{
mailClient.Send(msgMail);
}
catch (Exception ex)
{
string msg = "Exception caught in sending the email: " + ex.ToString();
showMessage(msg);
}
msgMail.Dispose();
But I get:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
With similar code in Android, my program just gets to an email form but lets the user decide what email add they will use.
Is there a way to do this in Windows?
There is an almost identical question and response here:
C# Windows Form Application - Send email using gmail smtp
And I think I've followed this but...doesn't work.
To directly answer your question - you probably haven't enabled less secure apps on the gmail account you are using.
Otherwise though, you could investigate the syntax of mailto if you want to let the user elect a mail client to use to send the email: https://www.labnol.org/internet/email/learn-mailto-syntax/6748/
From the link:
Send an email to Barack Obama with the subject “Congrats Obama” and some text in the body of the email message
<a href=”mailto:obama#whitehouse.gov?
subject=Congrats%20Obama&body=Enjoy%20your%20stay%0ARegards%20″>
This isn't directly related to C#/Windows - but I do know entering mailto:someone#somewhere.com at the Run prompt works:
Presumably then you could do something like: (untested)
Process.Run("mailto:someone#somewhere.com");
From the server response messages it looks like you have to provide login credentials before you are allowed to send.
Replace:
smtp.UseDefaultCredentials = true;
With:
smtp.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
This should do the trick.
You may have forgotten in your code to add the Host
Try to use this :
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = true;
smtp.Host = "SRVMAIL";

C# Sending mail with Yandex error 5.5.4 Error: send AUTH command first

I'm having issues sending an email using Yandex.
"SmtpException: Bad sequence of commands. The server response was: 5.5.4 Error: send AUTH command first."
I've tried all the solutions from: C# yandex mail send error 5.5.4 Error: send AUTH command first
Solutions I've tried:
EnableSsl to true
Changing smtpHost (tried smtp.yandex.com, smtp.yandex.ru, smtp.yandex.com.tr)
Changing smtpPort (tried 587, 25) (465 doesn't work as SSL deprecated)
Checked correct password is being used
Made sure "UseDefaultCredentials = false" is before the credentials being set
Tried adding in "SMTP.DeliveryMethod = SmtpDeliveryMethod.Network;"
This is pretty much everything accepted in the above question, however not one solution has worked for me.
Email Sender Class
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential($"{Configuration["EmailSettings:UserName"]}", $"{Configuration["EmailSettings:Password"]}");
client.Port = Convert.ToInt32(Configuration["EmailSettings:SmtpPort"]);
client.Host = Configuration["EmailSettings:SmtpHost"];
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress($"{Configuration["EmailSettings:UserName"]}");
mailMessage.To.Add("SomebodysEmail");
mailMessage.Body = "body";
mailMessage.Subject = "subject";
client.Send(mailMessage);
appsettings.json
"EmailSettings": {
"SmtpHost": "smtp.yandex.com.tr",
"SmtpPort": "25",
"UserName": "email",
"Password": "password"
},
In case anyone was curious, I've checked the credentials, port and host are being passed through properly from appsettings.json.
Thanks for your assistance!
Try to login into Mail account first through the web interface to finish your mailbox creation.
This worked in my case with same kind of error.

Failed to send an EMail with body contains ip address and port no

I have create function to send an email. This function was work successful on localhost but on server its failed without any exception. I know the problem comes from my Port on IP Address.
The sample body is string body = "<p>Please click here</p>Thank You."
The problem is : between IP Address and Port.
Successful send an email if i remove :.
Do you guys have any ideas?
public void Sent(string sender, string receiver, string subject, string body)
{
using (MailMessage mail = new MailMessage(sender, receiver))
{
using (SmtpClient client = new SmtpClient())
{
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "mail.companyName.com.my";
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
client.Send(mail);
}
}
}
You are doing it right, the code to send the mail is ok (you may want to revise the function name and make the smtp host name configurable, but that is not the point here).
The e-mail delivery fails on a relay, there is no immedieate feedback (no exception) to the client about this kind of failure.
The best bet is the IncreaseScoreWithRedirectToOtherPort property set in Set-HostedContentFilterPolicy in case your mail provider is Office365, or a similar spam filter mechanism in any other mail provider that is encountered down the mail delivery chain.
You can set a reply-to address and hope that the destination server will bounce a delivery failure that gives you more information. Or have the admin of the mail server look up the logs. More information here:
https://serverfault.com/questions/659861/office-365-exchange-online-any-way-to-block-false-url-spam
Try setting the 'mail.Body' to receive a Raw Html message instead of a encoded string, like:
mail.Body = new System.Web.Mvc.HtmlHelper(new System.Web.Mvc.ViewContext(), new System.Web.Mvc.ViewPage()).Raw(body).ToString();
Or put a using System.Web.Mvc at the beginning so it gets shorter and easier to understand:
using System.Web.Mvc
mail.Body = new HtmlHelper(new ViewContext(), new ViewPage()).Raw(body).ToString();

Don't Send Email by hosting

I use code below to send email, but I get this error every time
Failure sending mail.
My code:
MailMessage message = new MailMessage("donot-reply#mydomain.ir", "reception#yahoo.com", "test", "msg");
message.IsBodyHtml = true;
SmtpClient emailClient = new SmtpClient("mail.mydomain.ir",110);
emailClient.Credentials = new System.Net.NetworkCredential("donot-reply#mydomain.ir", "donot-replyA!1");
emailClient.EnableSsl = true;
emailClient.Send(message);
I can send email by this email address in thunder birds, but I do not know why I can't send the email in .NET
At a quick glance, your using an invalid Simple Mail Transfer Protocol port, try the following:
Port: 25
Port: 587
Without a Stack Trace or more information on your error we won't be much use.

Can i use IP Address as SMTP host rather then smtp.email.com

i am creating an Email sending sample application, and i want to use send email from different email address like "gmail, yahoo, hotmail" so i don't want to use "smtp.email.com" as host, because if i use "smtp.email.com" as host i will have to change my host name for every different company like("smtp.gmail.com" for gmail or "smtp.mail.yahoo.com" for yahoo.com ) so
Can i use IP Address as SMTP host rather then smtp.email.com.
Please give me a solution for this so that without changing smtp host name i can use different email company to send email.
this is my code:
try
{
// setup mail message
MailMessage message = new MailMessage();
message.From = new MailAddress(textBox1.Text);
message.To.Add(new MailAddress(textBox2.Text));
message.Subject = textBox3.Text;
message.Body = richTextBox1.Text;
// setup mail client
SmtpClient mailClient = new SmtpClient("smtp.gmail.com");//here i have to change SMTP host for different email company
mailClient.Credentials = new NetworkCredential(textBox1.Text,"password");
// send message
mailClient.Send(message);
MessageBox.Show("Sent");
}
catch(Exception)
{
MessageBox.Show("Error");
}
Sure you could use IP addresses instead of names, but remember then if they ever changed the IP you're goning to stop working.. BUT.... this needs to change depending on what you are sending the mail as unless you find some form of relay proxy thats open.. AS yahoo wont recveive gmail and gmail wont receive yahoo etc.. The reality is if you are sending as that it would end up changing wether you used an IP or a name.
Your webserver however will most likely send mails from your domain, rather than your gmail/yahoo accounts.. why not send it from your domain? eg noreply#myweb.com then the smtp server remains the same as its your web provider
Of course you could do
SmtpClient mailClient
if (textbox1.Text.Contains("gmail")
{
mailClient = new SmtpClient("smtp.gmail.com");/
mailClient.Credentials = new NetworkCredential(textBox1.Text,"password");
}
else if (textbox1.Text.Contains("somemail")
{
mailClient = new SmtpClient("smtp.somemail.com");/
mailClient.Credentials = new NetworkCredential(textBox1.Text,"password");
}
etc

Categories

Resources