Sending e-mail to multiple recipients - c#

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.

Related

C# smtp doesn't want to send an email

when sending an email, I get an exception:
Sending mail failed.
I've set the host to "smtp.gmail.com" and port to 587 but it doesn't work.
Code:
public void SendActivationLink(string email)
{
using (MailMessage mail = new MailMessage())
{
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Port = 587;
MailMessage message = new MailMessage();
MailAddress from = new MailAddress("myemail#gmail.com", "Voc");
message.From = from;
message.To.Add(email);
message.Subject = "Title";
message.Body = "Jello";
smtpClient.Host = "smtp.gmail.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "mypassword");
try
{
smtpClient.SendAsync(message, email);
}
catch (SmtpException ex)
{
throw new ApplicationException("exeption" + ex.Message);
}
}

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;

C# Send E-Mail issue

I'm trying to send an e-mail in C#,and I'm having some issues.Whenever I try to send an e-mail,I get a message "Error: Failure to send mail".
Here is my code:
try
{
client.Host = "smtp.gmail.com";
client.Port = 465;
client.UseDefaultCredentials = false;
client.Credentials = smtpCreds;
client.EnableSsl = true;
MailAddress sendFrom = new MailAddress("from#domain.com");
MailAddress sendTo = new MailAddress("to#domain.com");
MailMessage msg = new MailMessage(sendFrom,sendTo);
msg.Subject = "Subject";
msg.Body = "Body";
client.Send(msg);
}
catch (Exception e)
{
MessageBox.Show("Error:" + e.Message);
}
Also I have this declaration:
public SmtpClient client = new SmtpClient();
public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("mail", "password");
Hope you can help me.
you can try this and make sure you are using valid login credential and you have internet connection:
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
Try this code
using System.Net.Mail;
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("sender#gmail.com");
mail.To.Add("reciever#gmail.com");
mail.Subject = ("e mail subject");
mail.Body = ("message body");
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("sender's username", "sender's password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");

Get email from textbox and send using Gmail

Have a look at my email code behind.
protected void Button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add("color.shadow#yahoo.com");
mail.From = new MailAddress("abc#gmail.com");
mail.Subject = "Reservation Status";
string Body = "Greeting from us." +
" You may view your booking details at your profile now." +
" Have a nice day." +
"Thank you.";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("localhost", 25);
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
(abc#gmail.com", "abcdef");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Send(mail);
Label1.Text = "Mail Send...";
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
In this code, I have to manually enter the receiver email. My question is how to get email entered onto text box instead of mail.To.Add("color.shadow#yahoo.com");
Thanks in advance!
Change mail.To.Add("color.shadow#yahoo.com"); to mail.To.Add(textBoxEmail.Text); if you have created a textbox called textBoxEmail.

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

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

Categories