Trying to send mail with c# Error: too many errors - c#

I am trying to send mail to approx. 2000 users. I am sure there is nothing wrong with credentails.
This is my code:
SmtpClient client = new SmtpClient();
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "---";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("---", "---");
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.Body = "dfsdf";
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.Subject = "sdfsdf";
msg.From = new MailAddress("---");
msg.Bcc.Clear();
MembershipUserCollection muc = Membership.GetAllUsers();
foreach (MembershipUser mu in muc)
{
msg.Bcc.Add(mu.Email);
}
try
{
client.Send(msg);
}
catch (Exception ex)
{
Label_info.Text = ex.Message;
if (ex.InnerException != null) Label_info.Text += "<br />" + ex.InnerException.Message;
return;
}
This is the exception i get:
Service not available, closing transmission channel. The server response was: 4.7.0 [my smtp name] Error: too many errors

Related

null value when sending email via MVC ASP.NET

I have this MVC C# App and a controller where trying to send a email, but I always got this error
this is the code in my action´controller, it doesn´t receive any model so I add specific values
[HttpPost]
public ActionResult Index2()
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add("valid_email#hotmail.com");
mail.From = new MailAddress("valid_email#gmail.com");
mail.Subject = "PruebaMVC";
string Body = "PruebaMVC";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("valid_email#gmail.com", "correctPWD");
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
throw new ArgumentNullException("Exception in sendEmail:" + ex.Message);
}
return View();
}
could you please tell what is wrong?
edit: this is the exception I got
As i have used below code and it's working fine for me and i have configured smtp in web.config
ContentType HTMLType = new ContentType("text/html");
NetworkCredential cred = new NetworkCredential(StringConstant.NetworkUserName, StringConstant.NetworkPwd);
MailMessage msg = new MailMessage();
msg.BodyEncoding = System.Text.Encoding.Default;
msg.To.Add(ToEmail);
msg.Priority = System.Net.Mail.MailPriority.High;
msg.Subject = Subj;
msg.Body = strBody;
msg.IsBodyHtml = true;
if (attachement != null)
{
msg.Attachments.Add(attachement);
}
System.Net.Mail.AlternateView HTMLView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(strBody, HTMLType);
msg.From = new MailAddress(StringConstant.MailFrom); // Your Email Id
SmtpClient client = new SmtpClient(StringConstant.SmtpFrom, StringConstant.SmtpPort);
client.Credentials = cred;
client.EnableSsl = true;
client.Send(msg);

How can I use Google to send email? [duplicate]

I am getting an error when trying to send an e-mail through my web service. I have tried enabling access to less secure apps disabling 2-step verification and logging into the account via a web browser. None of the solutions on SO have worked for me. I am still getting:
Error: 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.
What can I do to fix this issue?
namespace EmailService
{
public class Service1 : IService1
{
public string SendEmail(string inputEmail, string subject, string body)
{
string returnString = "";
try
{
MailMessage email = new MailMessage();
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
// set up the Gmail server
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "mypassword");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
// draft the email
MailAddress fromAddress = new MailAddress("cse445emailservice#gmail.com");
email.From = fromAddress;
email.To.Add(inputEmail);
email.Subject = body;
email.Body = body;
smtp.Send(email);
returnString = "Success! Please check your e-mail.";
}
catch(Exception ex)
{
returnString = "Error: " + ex.ToString();
}
return returnString;
}
}
}
Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.
Also please go to this link and click on Continue Allow access to your Google account
also I edit it little bit :
public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("YourMail#gmail.com");
msg.To.Add(ReciverMail);
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("YourMail#gmail.com", "YourPassword");
client.Timeout = 20000;
try
{
client.Send(msg);
return "Mail has been successfully sent!";
}
catch (Exception ex)
{
return "Fail Has error" + ex.Message;
}
finally
{
msg.Dispose();
}
}
If the above code don't work , try to change it like the following code :
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("YourMail#gmail.com", "YourPassword");

Sending e-mail to multiple recipients

I want to send e-mail to multiple recipients in C# through Gmail. This is my code, but this code only send email to one address. How should I modify it?
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || richTextBox1.Text == "")
{
MessageBox.Show("Please fill out the boxes!");
return;
}
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("tamogatas.dolgozoadatbazis#gmail.com");
message.To.Add(new MailAddress(Form1.cimzett));
message.Subject = textBox1.Text;
message.Body = richTextBox1.Text + Environment.NewLine + "This message was sent from " + (Login.loginnev);
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("USERNAME#gmail.com", "PASSWORD");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
MessageBox.Show("The mail was sent successfully!");
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
}
Thanks for the answers!
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || richTextBox1.Text == "")
{
MessageBox.Show("Please fill out the boxes!");
return;
}
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("tamogatas.dolgozoadatbazis#gmail.com");
message.To.Add(new MailAddress(Form1.cimzett)); <---- THIS LINE
message.To.Add(new MailAddress("lalala#gmail.com"));
message.To.Add(new MailAddress("lalala3#gmail.com"));
message.To.Add(new MailAddress("lalala2#gmail.com"));
message.Subject = textBox1.Text;
message.Body = richTextBox1.Text + Environment.NewLine + "This message was sent from " + (Login.loginnev);
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("USERNAME#gmail.com", "PASSWORD");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
MessageBox.Show("The mail was sent successfully!");
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
}
String[] emails={"n1#gmail.com","n2#gmail.com","n3#gmail.com","n4#gmail.com"};
foreach(var items as emails)
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(items);
message.To.Add(new MailAddress(Form1.cimzett));
message.Subject = textBox1.Text;
message.Body = richTextBox1.Text + Environment.NewLine + "This message was sent from " + (Login.loginnev);
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("tamogatas.dolgozoadatbazis#gmail.com", "adminisztrator0");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
MessageBox.Show("The mail was sent successfully!");
}
There are a few problems with your code, however to address the primary issue it looks like you are relying on richTextBox1.Text to supply the email, however this is an assumption
So where you are doing:
message.To.Add(new MailAddress(Form1.cimzett));, maybe change Form1.cimzett to richTextBox1.Text?
In addition you can declare all of the SmtpClientcode outside of the Button logic, and instead declare it in this class.

A C# gmail MailSender

I have created an email sending function but it keeps getting timed out.
try
{
send.Enabled = false;
MailMessage message = new MailMessage();
message.From = new MailAddress(senderemail.Text);
message.Subject = subject.Text;
message.Body = body.Text;
foreach (string str in recipients.Text.Split(';'))
{
message.To.Add(str);
}
try
{
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential(senderemail.Text, senderpassword.Text);
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Send(message);
MessageBox.Show("Sent Successfully", "Details");
}
catch
{
SmtpClient client2 = new SmtpClient();
client2.Credentials = new NetworkCredential(senderemail.Text, senderpassword.Text);
client2.Host = "smtp.gmail.com";
client2.Port = 465;
client2.EnableSsl = true;
client2.Send(message);
MessageBox.Show("Sent Successfully", "Details");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
finally
{
send.Enabled = true;
}
After a port analysis using telnet, I am clueless as to why the code won't execute properly.
Try
client.UseDefaultCredentials = false;

How to send an e-mail with C# through Gmail

I am getting an error when trying to send an e-mail through my web service. I have tried enabling access to less secure apps disabling 2-step verification and logging into the account via a web browser. None of the solutions on SO have worked for me. I am still getting:
Error: 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.
What can I do to fix this issue?
namespace EmailService
{
public class Service1 : IService1
{
public string SendEmail(string inputEmail, string subject, string body)
{
string returnString = "";
try
{
MailMessage email = new MailMessage();
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
// set up the Gmail server
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "mypassword");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
// draft the email
MailAddress fromAddress = new MailAddress("cse445emailservice#gmail.com");
email.From = fromAddress;
email.To.Add(inputEmail);
email.Subject = body;
email.Body = body;
smtp.Send(email);
returnString = "Success! Please check your e-mail.";
}
catch(Exception ex)
{
returnString = "Error: " + ex.ToString();
}
return returnString;
}
}
}
Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.
Also please go to this link and click on Continue Allow access to your Google account
also I edit it little bit :
public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("YourMail#gmail.com");
msg.To.Add(ReciverMail);
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("YourMail#gmail.com", "YourPassword");
client.Timeout = 20000;
try
{
client.Send(msg);
return "Mail has been successfully sent!";
}
catch (Exception ex)
{
return "Fail Has error" + ex.Message;
}
finally
{
msg.Dispose();
}
}
If the above code don't work , try to change it like the following code :
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("YourMail#gmail.com", "YourPassword");

Categories