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.
Related
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
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.
I am using gmail to send email from C# program, my question is that email found under sent items if logged in into gmail.com via browser?
if I am sent an email from c# program and that email fails(bounce back) then Is failover notification found if I am logged in into gmail.com via browser?
if yes , then is there any additional settings to receive that? I want to show that in gmail.com via browser when I am login.
I don't really understand what you are asking but if you just need the code to send emails through gmail it is pretty simple
void SendMail_Click(object sender, EventArgs e)
{
var email = new Dto.IEmail();
if (EmailBody.Text == string.Empty && EmailSubject.Text == string.Empty)
{
XtraMessageBox.Show("please fill email body and subject");
return;
}
email.Body = EmailBody.Text;
email.Subject = EmailSubject.Text;
email.EmailAddress = "email#gmail.com";
email.ToAddress = "email#gmail.com";
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(email.EmailAddress);
message.To.Add(new MailAddress(email.ToAddress));
message.Subject = email.Subject;
message.Body = email.Body + "\n From user " + GlobalClass.UserLogin.USERNAME + "\n with body: " + _reportedfile;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(email.EmailAddress, "gmailpassword");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
XtraMessageBox.Show("err: " + ex.Message);
}
}
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();
}
}
}
before asking i search some possible solution in this site.
I found this Solution that I implemented in my code
But I have an error: when I send a mail with my code (in localhost) I obtain "Invalid Handler" but username and password are correct. Someone can help me?thanks
This is the cdoce
private SendGmail(){
try{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("mymail#gmail.com", "Enquiry");
mail.To.Add(TextBox_mail.Text.ToString());
mail.IsBodyHtml = true;
mail.Subject = "Test";
mail.Body = "Some Text";
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
//smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("mymail#gmail.com", "password");
smtp.EnableSsl = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
}
catch (Exception errore)
{
elenco_errori.Visible = true;
elenco_errori.InnerText = errore.Message;
}
}
I solved the question
in Application Start, in global.asax I added this code
public void Application_Start() {
System.Security.Cryptography.RSACryptoServiceProvider.UseMachineKeyStore = true;
System.Security.Cryptography.DSACryptoServiceProvider.UseMachineKeyStore = true;
}