Mail sending Error c# [duplicate] - c#

This question already has answers here:
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
(21 answers)
Closed 8 years ago.
How to solve this SMTP Error problem? When I'm sending mail I'm facing this error message. Sending mail using local system is not a problem. Does anyone know the solution to this problem?
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at admin_booking.btninvoicemail_Click(Object sender, EventArgs e) in c:\inetpub\vhosts\starlineroadways.com\httpdocs\admin_booking.aspx.cs:line 596
code:
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("xx#gmail.com", "xx");
System.Net.Mail.MailMessage oMsg = new System.Net.Mail.MailMessage();
MailAddress #add = new MailAddress(txtsendemail.Text);
oMsg.From = new MailAddress("xx");
oMsg.To.Add(#add);
oMsg.Subject = "xxx";
oMsg.Body = msgbody2;
oMsg.IsBodyHtml = true;
smtp.Send(oMsg);

You can try this.
MailMessage Mail = new MailMessage();
Mail.From = new MailAddress("xx#gmail.com");
Mail.To.Add(txtsendemail.Text);
Mail.Subject = "xxx";
Mail.Body = msgbody2;
SmtpClient smpt = new SmtpClient();
smpt.Credentials = new NetworkCredential("xx#gmail.com", "xx");
smpt.Port = 587;
smpt.Host = "smtp.gmail.com";
smpt.EnableSsl = true;
smpt.Send(Mail);

Related

smtp fails to send mail sometimes in c#

I have gone through some questions on this topic. All the answers relate when sending email fails all the time. In my case, it fails only sometimes with exception message:
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...
If I try second time it works. I'm using the following configuration.
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("emailid", "displayname");
mail.To.Add("TOAddress");
mail.Subject = subject1;
mail.Body = body1;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient("Outlook.office365.com", 587))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("emailid", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
checked a similar question here , given solutions not working.
Try this(second answer): Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
I used this code to send the email:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sender#gmail.com");
msg.To.Add("receiver#gmail");
msg.Subject = "Hello";
msg.Body = "Test";
SmtpClient smt = new SmtpClient();
smt.Host = "smtp.gmail.com";
System.Net.NetworkCredential ntcd = new NetworkCredential();
ntcd.UserName = "sender#gmail.com";
ntcd.Password = "senderPassword";
smt.Credentials = ntcd;
smt.EnableSsl = true;
smt.Port = 587;
smt.Send(msg);
Also check if your virus scanner doens't block your email from sending.

Send mail outside the network

How to send a mail outside the network. Currently i'm using the below code:
MailMessage objMailMsg = new MailMessage();
MailAddress FromAddress = new MailAddress("from#testmail.net");
MailAddress ToAddress = new MailAddress("to#testmail.net");
objMailMsg.From = FromAddress;
objMailMsg.To.Add(ToAddress);
objMailMsg.Subject = "Test";
objMailMsg.Body = "This is a test mail";
objMailMsg.IsBodyHtml = true;
int port = 25;
string IPaddr = "10.1.0.125";
SmtpClient smtpClient = new SmtpClient();
smtpClient.Port = port;
smtpClient.Host = IPaddr;
smtpClient.Send(objMailMsg);
On running, I'm getting an exception: Mailbox unavailable. The server response was: 5.7.1 Unable to relay
Where should I change. In code or is it SMTP related?

Emailing from Gmail error on DisplayName property

Bellow is my code when i remove DisplayName Property of MailAddress() it work fine but Receiving end mail show EmailID on Display Name like emailID#gmail.com.
MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("emailID#domainName.com");
// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient();
smtp.Port = 587; // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential(mail.From.ToString(), "password"); // [4] Added this. Note, first parameter is NOT string.
smtp.Host = "smtp.gmail.com";
mail.IsBodyHtml = true;
mail.Subject = Subject;
mail.Body = Body;
mail.To.Add(new MailAddress(To));
smtp.Send(mail);
mail.Dispose();
When i add Display name of MailAddress() i receive this error message.
System.Net.Mail.SmtpException: The SMTP server requires a secure
connection or the client was not authenticated. The server response
was: 5.5.1 Authentication Required. Learn more at at
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,
String response) at System.Net.Mail.SmtpTransport.SendMail(MailAddress
sender, MailAddressCollection recipients, String deliveryNotify,
Boolean allowUnicode, SmtpFailedRecipientException& exception) at
System.Net.Mail.SmtpClient.Send(MailMessage message) at ***
Where i am wrong?
var client = new SmtpClient
{
Host = "smtp.googlemail.com",
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true,
Credentials = new NetworkCredential(SmtpUserName, SmtpUserPassword);
};
client.Send(mail);
where mail is an instance of MailMessage
private static SecureString _smtpPassword;
public static SecureString SmtpUserPassword
{
get
{
if (_smtpPassword != null)
return _smtpPassword;
_smtpPassword = new SecureString();
foreach (var c in "your password here")
_smtpPassword.AppendChar(c);
_smtpPassword.MakeReadOnly();
return _smtpPassword;
}
}

SMTP Mail with ASP.net Error 5.5.1 Authentication Required

Good day, I'm a beginner from using ASP.net and SMTP Mailer
Heres my Question, I always encounter this Error when i send email from my local
and searched and tried the solutions around the net but not so lucky,
I hope someone point out what codes do i need and where i encounter this errror
Message = "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
Heres my Code:
protected void btnSendEmail_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Value, txtName.Value);
smtpClient.Credentials = new System.Net.NetworkCredential("myUser#gmail", "password");
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.gmail.com";
smtpClient.EnableSsl = true;
//Default port will be 25
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add("myEmail#gmail.com");
message.Subject = txtSubject.Value;
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1#yoursite.com")
message.CC.Add("myEmail#gmail.com");
// You can specify Address directly as string
message.Bcc.Add(new MailAddress("myEmail#gmail.com"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = txtaMessage.Value;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.HeadersEncoding = System.Text.Encoding.UTF8;
// Send SMTP mail
smtpClient.Send(message);
lblSuccess.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblSuccess.Text = "Send Email Failed.";
}
}
i tried to make a simple codes for sending email try this
MailMessage mm = new MailMessage();
mm.From = new MailAddress("fromEmail");
mm.To.Add("toEmail");
mm.CC.Add("ccEmail");
mm.Subject = "StringSubject";
mm.Body = "BodySubject";
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "UsernameString";
NetworkCred.Password = "PasswordString";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
try
{
smtp.Send(mm);
}
catch (Exception)
{
}
Try this reference ASP Simple SMTP for C# and VB it helps me a lot for may smtp problem
Please have a look on google support team, what they are saying regarding sending mail from application.
https://support.google.com/mail/answer/78775?hl=en
Also following link can help you.
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
Goto Account of Gmail , then select
Connected apps & sites
Allow less secure apps: ON(if this is off you cannot send mails through apps,or your websites )

The server rejected the sender address

I was able to send emails with local smtp but when trying to send with gmail, it isnt working.
ERROR:
"The server rejected the sender address. The server response was: 530 5.7.0
Must issue a STARTTLS command first. pj7sm14546972pbb.96 - gsmtp\r\n"
C#:
public static void SendEmail()
{
MailMessage mailMsg = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
mailMsg.From = "donotreply#admin.com"; //also tried smtpusername here
mailMsg.To = strToAddress;
mailMsg.Subject = strSubject;
mailMsg.Body = strBody;
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(smtpusername, smtppassword);
//smtpusername & smtppassword are valid gmail credentials
SmtpMail.SmtpServer = "smtp.gmail.com";
SmtpMail.Send(mailMsg);
}
If the EnableSsl property is set to true, and the SMTP mail server
does not advertise STARTTLS in the response to the EHLO command, then
a call to the Send or SendAsync methods will throw an SmtpException.
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx

Categories

Resources