I am trying to send mail from Web API using SMTP (GODaddy). It failed to send the mail and returning the exception as follows
Code :
public void SendMail()
{
try
{
MailMessage mail = new MailMessage("support#abc.com","toMail");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "this is email body";
SmtpClient client = new SmtpClient("relay-hosting.secureserver.net");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("support#abc.com", "****");
client.Port = 25;
client.Send(mail);
}
catch (SmtpFailedRecipientException Ex)
{
Ex.FailedRecipient.ToString();
}
}
Can anyone help me to fix this.
Related
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 have the following code I use to send emails from my application:
var config = DeserializeUserConfig(perfilAcesso.GetClientConfigPath() + "Encrypted");
using (SmtpClient client = new SmtpClient())
{
client.Host = config.GetClientSMTP();
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(config.GetClientEmail(), config.GetClientPassword());
using (MailMessage mail = new MailMessage())
{
mail.Sender = new MailAddress(config.GetClientEmail(), config.GetClientName());
mail.From = new MailAddress(config.GetClientEmail(), config.GetClientCompany());
mail.To.Add(new MailAddress("emailToReceive"));
mail.Subject = "[PME] SOS - Equipamento Parado";
mail.Body = "";
client.Send(mail);
MessageBox.Show("Email enviado com sucesso!");
}
}
I have set up three possible SMTP hosts for the user to choose from: Gmail ("smtp.gmail.com"), Outlook ("smtp.live.com") and Yahoo ("smtp.mail.yahoo.com").
When I try to send and email using a Yahoo account, this exception is thrown:
System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: Requested mail action not taken: mailbox unavailable.
I know for a fact that when sending emails with Gmail and Outlook accounts, the method works perfectly, because I tried it several times.
What am I doing wrong? Any help will be greatly appreciated!
Step 1
client.Port = 587;
Step 2
go to https://login.yahoo.com/account/security
Step 3
enable Allow apps that use less secure sign-in
Step 4 : full code
using System;
using System.Net.Mail;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
using (SmtpClient client = new SmtpClient())
{
client.Host = config.GetClientSMTP();
client.EnableSsl = true;
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(config.GetClientEmail(), config.GetClientPassword());
using (MailMessage mail = new MailMessage())
{
mail.Sender = new MailAddress(config.GetClientEmail(), config.GetClientName());
mail.From = new MailAddress(config.GetClientEmail(), config.GetClientCompany());
mail.To.Add(new MailAddress(config.emailToReceive));
mail.Subject = "Test 2";
mail.Body = "Test 2";
var isSend = false;
try
{
client.Send(mail);
isSend = true;
}
catch (Exception ex)
{
isSend = false;
Console.WriteLine(ex.Message);
}
Console.WriteLine(isSend ? "All Greeen" : "Bad Day");
Console.ReadLine();
}
}
}
}
}
if you add the same emails
mail.To.Add(new MailAddress(config.emailToReceive));
mail.To.Add(new MailAddress(config.emailToReceive));
you will git Error
Bad sequence of commands. The server response was: 5.5.0 Recipient already specified
if you want to reuse MailMessage
mail.To.Clear();
Are you sure that your from/to addresses are correct?
From and sender have to be your Yahoo addresses.
Here's a sample that works:
public static void Main(string[] args)
{
using (SmtpClient client = new SmtpClient())
{
client.Host = "smtp.mail.yahoo.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("my-yahoo-login", "yahoo-password");
using (MailMessage mail = new MailMessage())
{
// This works
mail.Sender = new MailAddress("my-email-address#yahoo.co.uk", "Tom Test");
mail.From = new MailAddress("my-email-address#yahoo.co.uk", "Tom Test");
mail.To.Add(new MailAddress("my-email-address#outlook.com"));
/* This does not
mail.Sender = new MailAddress("my-email-address#outlook.com", "Tom Test");
mail.From = new MailAddress("my-email-address#outlook.com", "Tom Test");
mail.To.Add(new MailAddress("my-email-address#yahoo.co.uk"));
*/
mail.Subject = "Test mail";
mail.Body = "Test mail";
client.Send(mail);
Console.WriteLine("Mail sent");
}
}
}
If you put your non-Yahoo address in Sender and From fields (the commented code) you'll get the same exception.
When I run this code, I get an error :
System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server
---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of
time, or established connection failed because connected host has
failed to respond 65.55.163.152:587
Code :
static void Main(string[] args)
{
string smtpAddress = "smtp.live.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "xxxxx#hotmail.co.uk";
string password = "xxxxxxxxxxx";
string emailTo = "myname#businessemail.co.uk";
string subject = "Daily Email Check";
string body = "Email reached business exchange server from an external hotmail email account";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = false;
try
{
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
catch (Exception e)
{
Console.WriteLine("Error\n\n {0}", e);
Console.ReadKey();
}
}
}
Try this;
using System.Net.Mail;
...
MailMessage mail = new MailMessage("xxxxx#hotmail.co.uk", "myname#businessemail.co.uk");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.live.com";
mail.Subject = "Daily Email Check";
mail.Body = "Email reached business exchange server from an external hotmail email account";
client.Send(mail);
I can't send a email message using gmail settings. i already tried client.Host ="localhost" it's working but not in client.Host ="smtp.gmail.com".. Please help me guys.. I need use client.Host ="smtp.gmail.com".. thanks
here's my C# code:
string from = "aevalencia119#gmail.com"; //Replace this with your own correct Gmail Address
string to = "aevalencia191#gmail.com"; //Replace this with the Email Address to whom you want to send the mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to); mail.From = new
MailAddress(from, "One Ghost" ,System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ; mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient(); //Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "iseedeadpoeple");
client.Port = 587; // Gmail works on this port client.Host ="smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
} HttpContext.Current.Response.Write(errorMessage
);
} // end try
here's the error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Much thanks guys!
You need to get and send mail to GMail by using SSL secutiry certificate
MailMessage msgMail = new MailMessage("a#gmail.com", "b#mail.me", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("a#gmail.com", "a");
try
{
smtp.Send(msgMail);
}
catch (Exception ex)
{
}
reference: http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/28b5a576-0da2-42c9-8de3-f2bd1f30ded4
public static string sendMail(string to, string title, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
if (to == "")
to = "aevalencia119#gmail.com";
MailAddressCollection m = new MailAddressCollection();
m.Add(to);
mail.Subject = subject;
mail.From = new MailAddress( "aevalencia119#gmail.com");
mail.Body = body;
mail.IsBodyHtml = true;
mail.ReplyTo = new MailAddress("aevalencia119#gmail.com");
mail.To.Add(m[0]);
smtp.Host = "smtp.gmail.com";
client.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("aevalencia119#gmail.com", "####");
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Send(mail);
return "done";
}
catch (Exception ex)
{
return ex.Message;
}
}
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("myname#gmail.com", "Lenin");
smtpClient.Host = "localhost";
//smtpClient.Host = "";
//smtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("myname#gmail.com");
message.Subject = "Feedback";
//message.CC.Add("admin1# gmail.com");
//message.CC.Add("admin2# gmail.com");
// message.Bcc.Add(new MailAddress("admin3# gmail.com"));
// message.Bcc.Add(new MailAddress("admin4# gmail.com"));
message.IsBodyHtml = false;
message.Body = txtComments.Text;
smtpClient.Send(message);
MessageBox.Show("Email successfully sent.");
}
catch (Exception ex)
{
MessageBox.Show("Send Email Failed." + ex.Message);
}
please help to sent email to different server .. gmail.com/yahoo.com/inbox.com ..etc using windows application.
thanks for the help.
SmtpClient smtpClient = new SmtpClient(host, port);
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("username#gmail.com", "password");
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 465 or 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(MailMessage);
try this one.