I have a error on progress bar when sending email - c#

I would like to have a progress bar when sending the auto generated email but I'm getting errors. Please help me fix this. Any type of response is greatly appreciated.
I don't know if I'm on the right path, I'm a beginner on C# and just relying guides online so.
This line has the error
new System.Threading.Thread(new System.Threading.ThreadStart(btnSend_Click));
private void btnSend_Click(object sender, EventArgs e)
{
//Cursor.Current = Cursors.WaitCursor;
try
{
MailMessage loginInfo = new MailMessage();
string em = "angelicasarimo15#gmail.com";
loginInfo.To.Add(em.ToString());
loginInfo.From = new MailAddress("vicserna1997#gmail.com");
loginInfo.Subject = "Requesting Supplies";
loginInfo.Body = "We want another supplies for blah blah blah" + System.Environment.NewLine +
"This is a system generated email.";
loginInfo.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("vicserna1997#gmail.com", "*Pass*");
smtp.Send(loginInfo);
MessageBox.Show("Email has been sent!", "Sent", MessageBoxButtons.OK);
progressBar1.Visible = true;
progressBar1.Style = ProgressBarStyle.Marquee;
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(btnSend_Click));
thread.Start();
}
catch
{
MessageBox.Show("Message not sent please check you internet connection", "Not Sent", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
}

Use SendAsync and subscribe to SendCompleted. This will notify you the sending process is completed. I don't believe its possible to get the progress of the sending email.

Related

How can I make my Xamarin app send an email whenever an error occurs?

My Xamarin app's release version is being tested by a non-developer, which sometimes means the tester has trouble communicating to me the details of bugs they encounter.
I'd like to implement some code that will execute whenever a crash or error occurs, and send an email to my address with the error information to help my debugging. I've written an email method (see below) but I'd like to know how I can call it whenever an unhandled exception is thrown rather than wrapping every single possible method in a try-catch.
public static void Email(string htmlString)
{
try
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.From = new MailAddress("email here");
msg.To.Add(new MailAddress("other email here"));
msg.Subject = "Error Report";
msg.IsBodyHtml = true;
msg.Body = htmlString;
smtp.Port = 1234;
smtp.Host = "smtp here";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("email", "password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(msg);
}
catch (Exception)
{
}
}
The basic way to catch errors in Xamarin apps is by using a try/catch block. Try/catch is easy to get.
You could do something like below.
private void Send_Clicked(object sender, EventArgs e)
{
try
{
.......
}
catch(Exception ex)
{
Email(ex.ToString());
}
}
public static void Email(string htmlString)
{
MailMessage msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
msg.From = new MailAddress("email here");
msg.To.Add(new MailAddress("other email here"));
msg.Subject = "Error Report";
msg.IsBodyHtml = true;
msg.Body = htmlString;
smtp.Port = 1234;
smtp.Host = "smtp here";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("email", "password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(msg);
}
Xamarin(Mono) internally "handles" those uncaught exceptions by surrounding literally everything with try-catch and raising the Unhandled event.
Explanation from the thread. Struggling to understand Xamarin exception handling
You could check the link. It provides a way to get the error log from log File.
Exceptions when using Xamarin Android

Error on sending email when using other mail server expect that gmail

I'm trying to send email from Mail address(hassan#creativesolutionzone.com), but its not sending an unhandled Exception:
System.net.mail.smtp exception: Mailbox unavailable. the server response was access denied - Invalid HELO name(see RFC2821 4.1.1.1)
Here is the code..
private void sendMail()
{
login = new NetworkCredential(txtUsername.Text, txtPassword.Text);
client = new SmtpClient(txtSMTP.Text);
client.Port = Convert.ToInt32(txtport.Text);
client.EnableSsl = chkSSL.Checked;
client.Credentials = login;
msg = new MailMessage { From = new MailAddress(txtUsername.Text + txtSMTP.Text.Replace("mail.", "#"), "Creative Solution Zone", Encoding.UTF8) };
msg.To.Add(txtTO.Text);
if (!string.IsNullOrEmpty(txtCC.Text))
msg.To.Add(new MailAddress(txtCC.Text));
msg.Subject = txtSubject.Text;
msg.Body = txtMessege.Text;
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.SendCompleted += new SendCompletedEventHandler(SendCompleteCallBack);
string userState = "Sending...";
client.SendAsync(msg, userState);
}
private static void SendCompleteCallBack(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
MessageBox.Show(string.Format("{0} Send Cancelled ", e.UserState), "Messgae", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (e.Error != null)
MessageBox.Show(string.Format("{0}", e.Error), "Messgae", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Your Message Has Been Successfully Sent", "Messgae", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Can Anyone Help me to resolve this?
Note: When I tried to send mail from my Gmail account, the code is
working perfectly but when i change my mail its not working

How to send value from textBox to mail?

I got this idea of making a mail sender. I tried to look for solutions but none really worked and they were badly explained or if it is even possible to do this?
So i basically have this if else code that check's if it's empty or not and if it's not it would send the value to mail.
using System.Net.Mail; //i think this is what i need?
private void button1_Click(object sender, EventArgs e)
{
if(string.IsNullOrWhiteSpace(textBox1.Text) && string.IsNullOrWhiteSpace(textBox2.Text))
{
MessageBox.Show("You're empty!");
}
else if(Int32.Parse(textBox1.Text) != 0)
{
// send text box to mail
}
else
{
MessageBox.Show("Something went wrong.");
System.Threading.Thread.Sleep(2000);
MessageBox.Show("Closing");
System.Threading.Thread.Sleep(1000);
this.Close();
}
}
Is someone willing to direct me in the correct direction or perhaps help me explain how to do it?
You can put textBox1.Text as an email body, something like this :
mail.From = new MailAddress(emailaddress);
mail.To.Add(recipient);
mail.Subject = "Test Mail";
mail.Body = textBox1.Text; // sets the body to the text box's content
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(emailaddress, password);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
System.Windows.Forms.MessageBox.Show("Mail sent");
Try this :
private void button1_Click(object sender, EventArgs e)
{
sendMailToAdmin(textbox1.Text,textbox2.text);
}
protected void sendMailToAdmin(string uname, string email)
{
MailMessage myMsg = new MailMessage();
myMsg.From = new MailAddress("****#mail.com");
myMsg.To.Add(email);
myMsg.Subject = "New User Email ";
myMsg.Body = "New User Information\n\nUser Name: " + uname + "\nEmail : " + email;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("****#mail.com", "pass***");
smtp.EnableSsl = true;
smtp.Send(myMsg);
}

c# sending email operation timed out

MailMessage msg = new MailMessage("teunenrichard#gmail.com", "ipadcraze#hotmail.com", "Movies this month", "Hello this is a test mail");
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
NetworkCredential xre = new System.Net.NetworkCredential("teunenrichard#gmail.com", "Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = xre;
smtp.EnableSsl = true;
smtp.Send(msg);
This is the code i run in a form . load to do a test email but it will not rund and says operation timed out. ive tried everything please help
MessageBox.Show("mail sent");
Use the "Timeout" property for your Smtp client . I think 0 is max
smtp.Timeout = 0;
For better Understanding of the error message try putting your code in a Try-Catch Block and then see the inner exception of the catch in MessageBox.Show(). It might provide you some more information regarding the error and might help/guide you in the right direction to resolve it. Something like below:-
try
{
//your email sending logic
}
catch(Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
You should send your mails using background thread, so as to make sure that your UI thread does not block and returns immediately. You can do something like this
private async void sendButton_Click(object sender, EventArgs e)
{
var result = await SendMail();
if (result)
{
MessageBox.Show("Mail sent");
}
}
private Task<bool> SendMail()
{
var task = Task.Run<bool>(() =>
{
MailMessage msg = new MailMessage("sendermail#gmail.com", "recievermail#gmail.com", "Movies this month", "Hello this is a test mail");
msg.IsBodyHtml = false;
using(SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.UseDefaultCredentials = false;
NetworkCredential xre = new NetworkCredential("sendermail#gmail.com", "Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = xre;
smtp.EnableSsl = true;
smtp.Send(msg);
return true;
}
});
return task;
}

error in sending mail from separate thread - asp.net/C#

I'm working in an application which sends the mail from server. since smtp.send(msg) will take some time to communicate with the server. i had made the send code block in separate thread. It worked fine before, but after adding the timer control timer1( which was doing some code logic). The mail send was interrupted due to the following error :
Unable to evaluate expression because the code is optimized or a
native frame is on top of the call stack
Threading comes here..
void sendMail()
{
ThreadStart sendCreateMail = delegate() { Send(subject); };
Thread threadSendCreateMail = new Thread(sendCreateMail);
sendCreateMail.IsBackground = true;
sendCreateMail.Start();
timer1.Enabled = true;
}
net.mail code comes here....
protected void Send(string subject)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient("smtp.office365.com");
var credential = (System.Net.NetworkCredential)smtp.Credentials;
string Username = credential.UserName;
string password = credential.Password;
mail.From = new MailAddress(Username);
mail.To.Add(toMail);
mail.Subject = "subject";
mail.Body = "msg body";
mail.IsBodyHtml = true;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(Username, password);
smtp.Send(mail);
}
UPDATE
The sendMail task works in other pages. Here it is a popup so that timer1 block which i already told is doing a popup close function. There it is stopping the execution of thread. that i can understand (like response.end, response.redirect can't guess what exactly, its a third party tool called telerik radwindow!). but how to overcome this.
this is what i use in my application
SmtpClient client = new SmtpClient();
client.Port = your port;
client.Host = your host;
client.EnableSsl = true;
client.Timeout = 15000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(your username, your passowrd);
MailMessage mm = new MailMessage(your username, recepient[0], title, message);
for (int a = 1; a < recepient.Count; a++)
mm.To.Add(recepient[a]);
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.SendCompleted += (s, e) =>
{
client.Dispose();
mm.Dispose();
};
client.SendAsync(mm, null);

Categories

Resources