Everything was working until a few days ago.
Host : smtp.yandex.com
Port : 587
SmtpClient client = new SmtpClient(_emailConfiguration.SmtpServer, _emailConfiguration.Port);
client.UseDefaultCredentials = false;
client.TargetName = "Asam";
client.Credentials = new NetworkCredential(_emailConfiguration.From, _emailConfiguration.Password);
MailMessage message = new MailMessage(new MailAddress(_emailConfiguration.From, "Asam"), new MailAddress(email));
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
message.IsBodyHtml = true;
message.Subject = subject;
message.Body = body;
try
{
await client.SendMailAsync(message);
}
catch (System.Exception ex)
{
Console.WriteLine(ex);
}
But I'm getting this error : Bad sequence of commands. The server response was: 5.5.4 Error: send AUTH command first.
I logged in from https://mail.yandex.com/ my password is correct.
I am trying to send an email through a one.com mail server. With receiving one I have no problems.
however with sending I keep getting an SMTP Exception.
System.ApplicationException: 'SmtpException has occured: "failure sending mail"'
UPDATE: after a tip in the comments here is the inner Exception
WebException: Unable to connect to the remote server
This is my code
public void email_send(string sendTo)
{
try
{
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(sendTo));
msg.From = new MailAddress("myMail#mydomein.be", "my name");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("myMail#mydomein.be", "myPassword");
client.Port = 587;
client.Host = "mailout.one.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(msg);
Console.WriteLine("email was sent successfully!");
}
catch (SmtpException ex)
{
throw new ApplicationException
("SmtpException has occured: " + ex.Message);
}
catch (Exception ex)
{
throw ex;
}
}
I also have the following information form my email provider (one.com):
As discussed in the comments, it looks like you can only use mailout.one.com from One.com-hosted websites - it refuses connections from elsewhere.
Instead you need to use send.one.com from other connections, which supports the same set of ports.
I have an console app and I am trying to send mail from it.
My code.
MailMessage message = new MailMessage(MailSender, "ToMe#me.com");
message.Subject = "Using the new SMTP client.";
message.Body = #"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
try
{
client.Send(message);
}
catch (Exception ex)
{
string t = ex.Message;
}
Got this from here
I must be missing something since I am getting:
Failure sending mail.
What am I doing wrong here?
EDIT:
inner Exeption.
InnerException = {"The remote name could not be resolved:
'smtp.google.com'"}
You can try to use
smtp.gmail.com
instead of
smtp.google.com
Also try to make sure that you are providing the correct credentials along with the correct port. A server parameter info:
Source
So you can try something like this
MailMessage message = new System.Net.Mail.MailMessage();
string fromEmail = "youremailaddress#xyz.com";
string password = "yourPassword";
string toEmail = "recipientemailaddress#abc.com";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Using the new SMTP client.";
message.Body = "Using this new feature, you can send an e-mail message from an application very easily.";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using(SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail, password);
smtpClient.Send(message.From.ToString(), message.To.ToString(), message.Subject, message.Body);
}
#Ra3IDeN ...hey brother try this...
SmtpClient smtpClient = new SmtpClient("mail.yourwebsitename.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("demo#yourwebsitename.com.com", "yourIdPassword");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//code for: From ,CC & To
mail.From = new MailAddress("demo#yourwebsitename.com", "yourwebsite");
mail.To.Add(new MailAddress("demo#yourwebsitename.com"));
mail.CC.Add(new MailAddress("youremailid#gmail.com"));
smtpClient.Send(mail);
If you're trying to send email from a console application (your higher-level problem), I recommend using PostMark. Why:
NuGet - You can get the PostMark NuGet package and send email with a nice API. Convenient and simple.
Not marked as SPAM - You can configure your "server" with verification (including spf and signing). So your email will more likely reach the destination in their inbox rather than their SPAM box.
Free - to a point. I think it's 1000 emails for free then $1 per 1000. So that's pretty good. Compare that to any other vanilla SMTP server for rent. PostMark is cheap
Consistent - From Workstation DEV to server LIVE, the PostMark API is consistently accessible. I cannot stress how good that is. Often a server host will offer SMTP server endpoint but it will only work from inside their network, meaning you have to configure another SMTP server when you're doing DEV work on your workstation (or it simply wont work).
Async Interface - I'm not sure if built-in smtp client in .Net is async...
Tracking - Hey look at that, they have a tracking feature built-in. That's snazzy.
Example code for sending (source):
var message = new PostmarkMessage()
{
To = "recipient#example.com",
From = "sender#example.com",
TrackOpens = true,
Subject = "A complex email",
TextBody = "Plain Text Body",
HtmlBody = "<html><body><img src=\"cid:embed_name.jpg\"/></body></html>",
Tag = "business-message",
Headers = new HeaderCollection{
{"X-CUSTOM-HEADER", "Header content"}
}
};
var imageContent = File.ReadAllBytes("test.jpg");
message.AddAttachment(imageContent, "test.jpg", "image/jpg", "cid:embed_name.jpg");
var client = new PostmarkClient("server token");
var sendResult = await client.SendMessageAsync(message);
MailMessage msg = new MailMessage("YourEmail#gmail.com", "DestinationEmail#something.com");
msg.Subject = message.Subject;
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Message Content here as HTML", null, MediaTypeNames.Text.Html));
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32( 587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("YourEmail#gmail.com", "YourPassword");
smtpClient.EnableSsl = true;
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object s,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
smtpClient.Credentials = credentials;
smtpClient.Send(msg);
This code works fine on my local machine ut when I deploy it. it gives Failure sending mail error.. Please Help...
MailAddress addrsTo = new MailAddress(toEmail);
MailAddress addrsFrom = new MailAddress("XXX#XXX.com", "XXX Title");
MailMessage mailmsg = new MailMessage(addrsFrom, addrsTo);
mailmsg.Subject = mailSbjct;
mailmsg.Body = "XXX Body";
SmtpClient smtp = new SmtpClient("mail.XXX.com");
smtp.EnableSsl = false;
smtp.Port = 26;
smtp.Credentials = new NetworkCredential("XXX#XXX.com", "XXXXXXX");
try {
smtp.Send(mailmsg);
} catch (Exception exc) {
throw new XXXException(1234, "---" + exc.Message);
}
you can try this, if you are using gmail :
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
As you have mentioned in your question works fine on my local machine.
It is suggesting that the problem is of credential which you are providing to send the mail.
Edit 1
If you are using you own domain credential then it is not going to work on the server.
The user IIS should have enough authority to send mail.
IIS User More detail
http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Here is a SO link
How to send email through IIS7?
The email is not being sent from C# code using the gmail smtp settings. It is giving an error of "Server requires a Secure connection or the Client was not authenticated."
The email and password are same as used for Login..
Following is the complete code.
MailMessage mail = new MailMessage("<from>","<to>");
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = new NetworkCredential("<email>", "<password>");
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Timeout = 20000;
client.UseDefaultCredentials = false;
mail.Body = "Test email from C# Code";
mail.Subject = "Test Email";
Console.WriteLine("Attempting to Send Email");
try {
client.Send(mail);
Console.WriteLine("Email sent... ");
}
catch (System.Net.Mail.SmtpFailedRecipientException ex) {
Console.WriteLine("Could not send email to the mentioned recipient" + ex.Message);
}
catch (System.Net.Mail.SmtpException ex) {
Console.WriteLine("Could not send Email..\n" + ex.Message + "\n" + ex.StackTrace);
}
Console.ReadLine();
Any help appreciated :) -- Kind regards,
You have to swap the following lines.
client.Credentials = new NetworkCredential("<email>", "<password>");
client.UseDefaultCredentials = false;
You have to set the UseDedaultCredentials property at first.
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("<email>", "<password>");
That's the way, it should work.