SMTP – Client was not Authenticated to Send anonymous mail during Mail From Error #
I have to send mails using my web application. Given the below code showing The SMTP server requires a secure connection or the client was not authenticated. The server response was:
5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM.
Code:
protected void btnsubmit_Click(object sender, EventArgs e)
{
Ticket_MailTableAdapters.tbl_TicketTableAdapter tc;
tc = new Ticket_MailTableAdapters.tbl_TicketTableAdapter();
DataTable dt = new DataTable();
dt = tc.GetEmail(dpl_cate.SelectedValue);
foreach (DataRow row in dt.Rows)
{
string eml = (row[“Emp_Email”].ToString());
var fromAddress = “emailAddress”;
var toAddress = eml;
const string fromPassword = “*****”;
string body = “Welcome..”;
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = “smtp.office365.com”;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.UseDefaultCredentials = false;
smtp.Timeout = 600000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
}
}
Here is Some Solutions :
Solution 1: #
var sClient = new SmtpClient(“domain-com.mail.protection.outlook.com”);
var message = new MailMessage();
sClient.Port = 25;
sClient.EnableSsl = true;
sClient.Credentials = new NetworkCredential(“user”, “password”);
sClient.UseDefaultCredentials = false;
message.Body = “Test”;
message.From = new MailAddress(“test@test.com”);
message.Subject = “Test”;
message.CC.Add(new MailAddress(“dude@good.com”));
sClient.Send(message);
Solution 2: #
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(mailOut, pswMailOut);
client.Port = 587; // 25 587
client.Host = “smtp.office365.com”;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(mailOut, displayNameMailOut);
mail.To.Add(new MailAddress(mailOfTestDestine));
mail.Subject = “A special subject”;
mail.Body = sb.ToString();
client.Send(mail);
Solution 3: #
Started working after adding property:
mail.smtp.starttls.enable=true
Using:
mail.smtp.host=smtp.office365.com
mail.smtp.port=587
mail.transport.protocol=smtp
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.user=xxx@example.com
mail.smtp.password=xxx
mail.smtp.from=yyy@example.com