Sending Two Mails At a time in asp.net - c#

I need to send two mails one to client and one to admin in asp.net
Below is the code i used but it didn't send two mails , it just sent mail to client only
I had made two functions one for admin one for client but it doesn't work.
using System;
using System.Net.Mail;
namespace Mail
{
public partial class Mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void admin()
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add("email#gmail.com");
Msg.Subject = txtSubject.Text;
Msg.Body = "UserName " + txtName.Text + " UserEmail " + txtEmail.Text + "</br>";
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("email#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Thanks for Contact us";
// Clear the textbox valuess
txtName.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
public void client()
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add(txtEmail.Text);
Msg.Subject = "Thanks you for downloading our product";
Msg.Body = "we loves your privacy , this product is in BETA Mode , Do Feedback us about this Product http://www.google.com >";
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("email#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Thanks for Contact us";
// Clear the textbox valuess
txtName.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
admin();
client();
}
}
}

Related

fail send smtp e-mail to the user with error: Failure sending mail

I created a registration form in the asp.net and everything is working fine, then, I added the following code to be able to send an email to the user and the host, it is still working but I didn't received any email, do you know what is the problem with my code?
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com",587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("##########", "###########");
MailMessage mailmsg = new MailMessage();
mailmsg.To.Add(TxtEmail.Text);
mailmsg.From = new MailAddress("################");
mailmsg.Subject = "The email is confirmed";
mailmsg.Body = "Dear "+ TxtName.Text + ",\n\nyou have been registred successfully";
client.Send(mailmsg);
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('message has been sent successfully');", true);
MailMessage mailmsg2 = new MailMessage();
mailmsg2.To.Add("################");
mailmsg.From = new MailAddress("##############");
mailmsg.Subject = "New user has registred";
mailmsg.Body = "Dear Admin, \n\nthere are a new user registred in the website";
client.Send(mailmsg);
}
catch (Exception ex)
{
Response.Write("Couldn't send email: " + ex.Message);
}
}
I am getting the output:
Couldn't send email: Failure sending mail

how to write a simple code that sends email

i developed a simple website that stores in the user name and sends that to my emailid and then downloads a file.File is getting downloaded but not mailing me the username.
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("mygmailid");
mailMessage.From =new MailAddress("mydomainbasedemailid");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
SmtpClient smtpClient=new SmtpClient("mail.mydomain.com",587);
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
}
Try to set the Smtp.Credentials like this:
string HOSTLOGIN = "YourHostLogin";
string HOSTPW = "YourTopSecretPasswort";
var credentials =
new System.Net.NetworkCredential() { UserName = HOSTLOGIN, Password = HOSTPW };
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
client.EnableSsl = true;

How to send data trough email

I need to send list box items to email. Code is running fine, but when it gets to Send method it fails to send it.
protected void ProcessButton_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("someone#live.com");
mailMessage.From = new MailAddress("myAdress#live.com");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = OrderListBox.Items.ToString();
SmtpClient smtpClient = new SmtpClient("smtp.live.com");
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send email - error: " + ex.Message);
}
}
You can write list in file and send it as attachment (gmail example):
protected bool ProcessButton_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new NetworkCredential("myEmail#gmail.com", "password");
//if you have double verification on gmail, then generate and write App Password
client.EnableSsl = true;
MailMessage message = new MailMessage(new MailAddress("myEmail#gmail.com"),
new MailAddress(receiverEmail));
message.Subject = "Title";
message.Body = $"Text";
// Attach file
Attachment attachment;
attachment = new Attachment("D:\list.txt");
message.Attachments.Add(attachment);
try
{
client.Send(message);
// ALL OK
return true;
}
catch
{
//Have problem
return false;
}
}
and write this on begining of code:
using System.Net;
using System.Net.Mail;
You can choose smpt host adress and port if you want.

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.

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.

Categories