The server rejected the sender address - c#

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

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.

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 )

How to send email in c# with Port id 465 and ssl "true"

Can't send email by using this code,
Exception thrown "The Operation has time out."
Code:
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress("xxx#gmail.com");
mailMessage.From = new MailAddress("yyy#domainName.com", "No-Reply");
mailMessage.Subject = "Test Mail";
mailMessage.Body = "E-Mail Testing...........";
mailMessage.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
//client.Timeout = 200000;
client.UseDefaultCredentials = false;
client.Credentials =
new System.Net.NetworkCredential("yyy#domainName.com", "--Password--");
client.Port = 465;
client.Host = "mail.domainName.com.np";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(mailMessage);
The EnableSsl option will NOT implicitly use SSL. That means it will not immediately begin SSL negotiations after connecting.
The EnableSsl option will enable explicit SSL. Which means SSL will be negotiated with the server using the STARTTLS command as described in RFC 3207.
System.Net.Mail does not support implicit SSL. You may have another port available from the server that you can use explicit SSL on. Otherwise you have to reconfigure your server.

Problems sending e-mail in web app

I have been trying for 2 days to get my ASP.NET webforms application to send an e-mail.
I have tried this using both outlook and gmail. I got the smtp information for both from this tutorial:
When I try using port 587 in the example below I get an error saying:
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
When I try using port 465 in the example below:
My application just hangs forever and my page never gets back my email function which is in the PageLoad.
A couple of things to note just incase one of these is messing me up:
I am using a standard VS2013 dev environment (running my web app in debug mode using F5)
I am using the same e-mail address for from, to, and gmail login credentials)
My ISP does not block these ports. (comcast cable)
I even went to the google DisplayUnlockCaptcha page to Allow access to my account
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
using (MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress))
{
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
}
This code should work fine for you
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
You'll need to set the delivery mode otherwise gmail will return an error
EDIT:
Throwing an 'using' around 'MailMessage' might also be a smart thing to do
It turns out that it is because of a GMail security setting.
https://www.google.com/settings/security/lesssecureapps
You have to enable access for less secure apps.
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("xxx#gmail.com");
mail.From = new MailAddress("yyy#gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yyy#gmail.com", "pw"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}``
http://www.c-sharpcorner.com/UploadFile/47548d/how-to-send-bulk-email-using-Asp-Net/

How to send email without SMTP port, Host and enable SSL?

I have the following code which if I send an mail its showing email ending fail how to solve this one?
public void Sendemail(string toaddr)
{
if (Editor1.Content == null)
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Subject or Body of Email Should not be Empty...!');", true);
}
else
{
string[] arryaddr = toaddr.Split(',');
MailMessage ms = new MailMessage();
ms.IsBodyHtml = true;
//ms.To.Add(new MailAddress("admin#digitalprintonline.co.uk"));
ms.To.Add(new MailAddress(toaddr));
string fromaddr = "admin#digitalprintonline.co.uk";
for (int i = 0; i < arryaddr.Length; i++)
{
ms.Bcc.Add(new MailAddress(arryaddr[i].ToString()));
}
ms.From = new MailAddress(fromaddr);
string subject = txtSubject.Text;
ms.Subject = subject;
string body = Editor1.Content;
ms.Body = body;
ms.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential("admin#digitalprintonline.co.uk", "dpo123");
try
{
smtp.Send(ms);
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Email Sent Successfully...!');", true);
}
catch (Exception ex)
{
}
}
}
I have the following email settings
smtp from="admin#dpoweddings.co.uk"
network host="217.199.175.121"
port="25"
userName="admin#digitalprintonline.co.uk"
password="*****"
enableSsl="false"
SMTPClient uses SMTP (protocol) to send emails. Thus it requires the details about the SMTP Server. Consider the code below
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username#gmail.com", "password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
In the above code, we have provided the following information to the SMTP client to work properly
Host: The smtp server details. As you can see, by default you use #gmail.com but here it is specified smtp.gmail.com
Port: The port to connect to the smtp server
EnableSsl: If ssl connection is required
Credentials: The account using whose credentials the mail should be sent.
Your code is missing the Host and Port (which are also required along with Credentials, EnableSsl is false, by default)
To know about details (host, port, Ssl requirement) of SMTP server at digitalprintonline.co.uk, you will have to contact the administrator there. Alternatively, you can use GMail SMTP server details along with your GMail account credentials.
You don't need to have a server on your local machine in order to send a message to a SmtpClient. Your email service provider such as smtp.gmail.com has the server and your SmtpClient communicates with that server.

Categories

Resources