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.
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.
this is my code:
try {
SmtpClient client = new SmtpClient("smtp.aruba.it", 25);
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("receiver#pec.it", "password");
MailMessage msg = new MailMessage();
msg.To.Add("receiver#pec.it"); //Receiver
msg.From = new MailAddress("sender#pec.it"); //Sender
msg.Subject = "Object"; //Oggetto Mail
msg.Body = ""; //Corpo del testo
Attachment data = new Attachment(fileName);
msg.Attachments.Add(data);
client.Send(msg);
MessageBox.Show("Attachment send correctly");
}
catch (SmtpException ex)
{
throw new ApplicationException
("SmtpException has occured: " + ex.Message);
}
I tried to use this code but it doesn't work. I tried with gmail host and it works, so the code should work. The problem is smtp for sure but I couldn't find the right way to send it via pec aruba.
Another problem is the attachment. The name of attachment is the entire filename path and i want that is only the name of the file. How i can resolve this two problem?
Use smtps.aruba.it instead smtp.aruba.it and port 587. It works for me ( I had same problem)
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 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.
I have created a Windows application which is used to send emails. i have given credentials. i turned on google/settings/lesssecure apps. Eventhough its not sending. Its showing the error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required here is my code.
MailMessage message = new MailMessage();
message.From = new MailAddress("Selvacsebe23#gmail.com");
string[] mailaddress = new string[count];
int i;
if (textSubject.Text != string.Empty)
{
message.Subject = textSubject.Text;
if (textBody.Text != string.Empty)
{
message.To="Selvakesavan#gmail.com"
message.IsBodyHtml = true;
string tmpBody = "Hello " + "," + "<br/> <br/>" + textBody.Text + "<br/> <br/>" + "Thanks and Regardds";
message.Body = tmpBody;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("selvacsebe23#gmail.com", "mypassword");
message.Priority = MailPriority.High;
client.EnableSsl = true;
client.Send(message);
MessageBox.Show("Mail has sent successfully !!!", "Success !");
}
else
{
MessageBox.Show("Please Enter Body of the Message !");
}
}
else
{
MessageBox.Show("Please Enter Subject !");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Failure !");
log.Fatal(ex.Message);
}
}
If you turn on 2-step-verification, then you need to login using app-specific-password. You can create it here:
https://support.google.com/accounts/answer/185833?hl=en. If you use your normal password, then you will get exception : 5.5.1 Authentication Required. You don't need lots of code, this code is enough to send email without attachment:
const string from = "user1#gmail.com";
const string to = "user2#yahoo.com";
const string subject = "This is subject";
const string body = "This is body";
const string appSpecificPassword = "akdfkajsdhklakdfh";
var mailMessage = new MailMessage(from, to, subject, body);
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(from, appSpecificPassword);
smtpClient.Send(mailMessage);
}