I am making a bulk mail sender which will take mail addresses from DB. All is okay, test mails are sending but the success message is showing only after all the mails are sent. I want a message box or a progress box to count the mails that are passing. Help me to add a message pomp to count the mails have been sending. Here the Code I am using for sending mails, please write me the code for the message box to show the count -
private void btnSendEmail_Click(object sender, EventArgs e){
string subject = txtSubject.Text;
string message = txtMessage.Text;
if (!txtFile.Text.Equals(String.Empty))
{
if (System.IO.Directory.GetFiles(txtFile.Text).Length > 0)
{
foreach (string file in System.IO.Directory.GetFiles(txtFile.Text))
{
}
}
else
{
}
}
var con = "Data Source=Ashiq-pc;Initial Catalog=OfferMails;Integrated Security=True;MultipleActiveResultSets=True";
List<EmailModel> emailList = new List<EmailModel>();
using (SqlConnection myConnection = new SqlConnection(con))
{
string oString = "Select * from tbl_MailAdd where Flag=#Flag";
SqlCommand oCmd = new SqlCommand(oString, myConnection);
oCmd.Parameters.AddWithValue("#Flag", true);
myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
EmailModel emailModel = new EmailModel();
emailModel.ID = Convert.ToInt16(oReader["ID"]);
emailModel.EmailAdd = oReader["EmailAdd"].ToString();
emailModel.Flag = Convert.ToBoolean(oReader["Flag"]);
emailList.Add(emailModel);
}
myConnection.Close();
}
}
//return matchingPerson;
foreach (EmailModel email in emailList)
{
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("my mail", "my pass");
MailMessage msg = new MailMessage();
msg.To.Add(email.EmailAdd);
msg.From = new MailAddress("my from name");
msg.Subject = subject;
msg.Body = message;
if (!txtFile.Text.Equals(String.Empty))
{
if (System.IO.Directory.GetFiles(txtFile.Text).Length > 0)
{
foreach (string file in System.IO.Directory.GetFiles(txtFile.Text))
{
//Add file in ListBox.
listAttch.Items.Add(file);
//System.Windows.Forms.MessageBox.Show("Files found: " + file, "Message");
Attachment data = new Attachment(file);
msg.Attachments.Add(data);
}
}
else
{
//listBox1.Items.Add(String.Format(“No files Found at location : {0}”, textBox1.Text));
}
}
//Attachment data = new Attachment(textBox_Attachment.Text);
//msg.Attachments.Add(data);
client.Send(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//for (int i = 0; i < emailList.Count; i++)
//{
// MessageBox.Show("i++");
//}
MessageBox.Show("Successfully Sent Message.");
}
First, let me just say that asking for someone to write this code for you is a bit frowned upon here. But I know what you're asking so I'm willing to help out a bit.
Firstly, a message box by itself isn't going to work for this, because a message box ends up stopping the thread where it is until a the DialogResult event is handled. So, for that being said, you will probably want to build another form (I am assuming that you're doing this with Windows Forms here.) to do the progress for you. If you want it to be a counter, then you could do it using a label that is changing its text as each mail is sent.
So, with that being said, however, if you're doing this with a Windows Form, the issue you will run into is the form will never update until after your
foreach loop finishes. This is because of the way that C# handles thread usage.
For what you want to accomplish, you'll need to utilize the BackgroundWorker. This allows you to run cumbersome operations asynchronously.
Here is an example that you could use to get your system working:
//used to be a counter for your progress
int i_counter = 0;
//create a background worker instance
public BackgroundWorker bg_worker = new BackgroundWorker();
public Form1()
{
InitializeComponent();
//set this to true if you want to have an event where you can cancel the background task
bg_worker.WorkerSupportsCancellation = true;
//this is needed to actually show your progress, allows the background worker to report it is working
bg_worker.WorkerReportsProgress = true;
//assigns the "DoWork" and "ProgressChanged" Handlers to the background worker.
bg_worker.DoWork += new DoWorkEventHandler(bg_worker_DoWork);
bg_worker.ProgressChanged += new ProgressChangedEventHandler(bg_worker_ProgressChanged);
}
//Mail method
public void MailerMethod()
{
//all of the things you want to happen for your mailing methods
foreach(//your loop stuff in here)
{
//THIS NEEDS TO HAPPEN TO CAUSE THE COUNTER TO UPDATE
bg_worker.ReportProgress(i_counter);
}
}
//the stuff that you want done in the background
//fires when "RunAsync" is called by BackgroundWorker object.
private void bg_worker_DoWork(object sender, DoWorkEventArgs e)
{
//IN HERE IS WHERE YOU WANT YOUR EMAIL STUFF TO HAPPEN
bg_worker = sender as BackgroundWorker;
MailerMethod();//or just all of your mailing code, it looks nicer like this though
}
//fires when worker reports the progress has changed
//caused by "ReportProgress" method
private void bg_worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lb_counter.Text = Convert.ToString(i_counter);
}
//this is what will happen when the worker is done.
//you can have it do a alot of things, such as write a report, show a window, etc.
private void bg_worker_RunWorkComplete(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("DONE!", "DING DING!");
Application.Exit();
}
//button click event to start this shindig
private void bt_start_Click(object sender, EventArgs e)
{
//makes sure the background worker isn't already trying to run
if (!bg_worker.IsBusy)
{
//calls the DoWork event
bg_worker.RunWorkerAsync();
bt_start.Visible = false;
}
}
May I direct you here for the official MDSN documentation.
And here for a basic tutorial on how to set up a BackgroundWorker.
Hope this helps, let me know how it goes.
Use a Progress bar with its Position property set to zero, Max property set to the number of emails you are sending and the Step property set to 1, unless you want to step every 10 or so emails then you would use % 10 in your loop to increase the progress. In your loop simply increase the position of the ProgresBar or call Step().
Related
i am new in C# so please be patient with me .
i want to make progress bar work with any functions i make in my program
i have class to check if INTERNET available and the connection of database status
and i have "progressBar1" , style is "Marquee"
i just want to indicate that there is a process work "Function" in the program and i don't need to have step or timer to increment it
just make the progress work until the function finish its code and the functions will work in button event (when i push button)
this is my code
class checkInternet
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
public bool checkInternetAvailable()
{
int Desc;
bool result = false;
if (InternetGetConnectedState(out Desc, 0) == true)
{
try
{
dbConnection StartConn = new dbConnection();
SqlConnection MyConnetion = StartConn.GetConnection();
MyConnetion.Open();
if (MyConnetion.State == ConnectionState.Open)
{
result = true;
}
MyConnetion.Close();
}
catch (Exception)
{
result = false;
MessageBox.Show("The database connection does not available, May be because of this reasons: \n\n1- there is a new version of the program avalible. \n2- database has some maintenance. \n\n Please check later :)", "Conection status");
}
}
else
{
result = false;
MessageBox.Show("No internet connection avalible , Please check later :) \nThanks.", "Conection status");
}
return result;
}
}
and this is what i have in my button event
private void button1_Click(object sender, EventArgs e)
{
checkInternet check = new checkInternet();
progressBar1.Value = 0;
do
{
progressBar1.PerformStep();
} while (check.checkInternetAvailable());
}
how can i implement that ?
thanks
As I understand you want user to see progressbar while your check connection task executes in background. checkInternetAvailable would be your background operation and I wouldn't suggest showing messages directly form it. Instead return a custom struct :
public struct ConnectionCheckResult
{
public bool Success;
public string Message;
}
And this will be your button click event handler :
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.Visible = true;
//add code here to be executed on UI thread before connection check
Task.Run(new Action(() =>
{
//Task.Run this code on the thread pool instead of your UI thread. So your code is checking connection while progress bar is still rendering
ConnectionCheckResult res = new checkInternet().checkInternetAvailable();
this.Invoke(new Action(() =>
{
//this.Invoke executes following delegate on UI thread. All UI changes - like progressBar1.Visible = false; need to be made in UI thread.
//add code here to be executed on the UI thread after connection check.
progressBar1.Visible = false;
if (!string.IsNullOrEmpty(res.Message))
MessageBox.Show(res.Message);
}));
}));
//add code to be executed on UI thread at the same time as connection check
}
I know multi-threading is difficult to wrap your head around at first, here's good tutorial with code samples.
Also when your progressbar style is Marquee you don't need to call PerformStep. It will just roll by itself.
EDIT: You should also modify checkInternetAvailable() like so :
public ConnectionCheckResult checkInternetAvailable()
{
int Desc;
ConnectionCheckResult result = new ConnectionCheckResult();
if (InternetGetConnectedState(out Desc, 0) == true)
{
try
{
dbConnection StartConn = new dbConnection();
SqlConnection MyConnetion = StartConn.GetConnection();
MyConnetion.Open();
if (MyConnetion.State == ConnectionState.Open)
{
result.Success = true;
}
MyConnetion.Close();
}
catch (Exception)
{
result.Success = false;
result.Message = "The database connection does not available, May be because of this reasons: \n\n1- there is a new version of the program available. \n2- database has some maintenance. \n\n Please check later :)";
}
}
else
{
result.Success = false;
result.Message = "No internet connection available , Please check later :) \nThanks.";
}
return result;
}
I want to synchronize my local and web database so i have written a stored procedure using linked server. My stored procedure executes fine and data synchronization is successful but the procedure takes around 7-10 minutes to get executed. The exact timing cannot be determined. So whenever the procedure runs on my windows application then the page seems as if it has become unresponsive though the process is still going on.
So i am having a "Data Sync" button on my page on click of which i want the progress bar to display the progress of the stored procedure. For the time being I am taking the average of last few execution timings to define the time duration for which the stored procedure runs. Now the problem is that when i click on the data sync button then the progress bar doesn't work. Kindly help me with this issue.
My code is as follows:-
namespace RMS
{
public partial class DataSync : Form
{
connection con = new connection();
SqlCommand cmd = new SqlCommand();
static int rowCount;
static int syncTime;
static int timeSlice;
public DataSync()
{
InitializeComponent();
}
private void btnDataSync_Click(object sender, EventArgs e)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
try
{
con.GetConnectLive();
con.GetConnect();
if (con.CnLive.State == ConnectionState.Open)
{
MessageBox.Show("Connection to Live Server Successful!!!...Data Synchronisation may take several minutes so do not cancel the operation while in execution mode");
btnDataSync.Enabled = false;
btnDataSync.Text = "Please Wait...";
string Str = "RMS_LocalToLive";
cmd = new SqlCommand(Str, con.Cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 1200;
rowCount = cmd.ExecuteNonQuery();
if (rowCount > -1)
{
MessageBox.Show("Total no. of rows synchronised = " + rowCount);
btnDataSync.Text = "Success";
}
else
{
MessageBox.Show("Data Synchronisation couldn't be completed because of connection problem... Please try again!!!");
}
}
else
{
MessageBox.Show("Unable to connect to Live Server...Please check your internet connection and try again!!!");
}
con.GetDisConnect();
con.GetDisConnectLive();
}
catch (Exception ex)
{
MessageBox.Show("Please check your internet connection and try again!!!");
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
con.GetConnect();
string Str = "RMS_DataSyncTime";
cmd = new SqlCommand(Str, con.Cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 1200;
syncTime = Convert.ToInt32(cmd.ExecuteScalar().ToString());
timeSlice = syncTime / 100;
con.GetDisConnect();
}
catch (Exception ex)
{
MessageBox.Show("Unable to retrieve last Data Synchronisation Timing");
}
for (int i = 1; i <= synctime; i=i+timeslice)
{
Thread.Sleep(timeslice);
// Report progress.
backgroundWorker1.ReportProgress(i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Change the value of the ProgressBar to the BackgroundWorker progress.
progressBar1.Value = e.ProgressPercentage;
// Set the text.
this.Text = e.ProgressPercentage.ToString() + "% Completed";
}
private void DataSync_Load(object sender, EventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgse)
{
}
}
}
The main issue here is that, while you are executing your progress bar updates in the BackgroundWorker's thread, the ReportProgress() updates never make it to the UI thread, because you've blocked that thread with the main SQL operation.
Instead of doing that, you should do something more like this:
private void btnDataSync_Click(object sender, EventArgs e)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
btnDataSync.Enabled = false;
btnDataSync.Text = "Please Wait...";
bool success = false;
try
{
// Execute the query asynchronously
success = await Task.Run(() => ExecuteLocalToLive());
}
catch (Exception ex)
{
MessageBox.Show("Please check your internet connection and try again!!!");
}
btnDataSync.Enabled = true;
btnDataSync.Text = success ? "Success" : "Failure";
}
private bool ExecuteLocalToLive()
{
bool success = false;
con.GetConnectLive();
con.GetConnect();
if (con.CnLive.State == ConnectionState.Open)
{
MessageBox.Show("Connection to Live Server Successful!!!...Data Synchronisation may take several minutes so do not cancel the operation while in execution mode");
string Str = "RMS_LocalToLive";
cmd = new SqlCommand(Str, con.Cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 1200;
rowCount = cmd.ExecuteNonQuery();
if (rowCount > -1)
{
MessageBox.Show("Total no. of rows synchronised = " + rowCount);
success = true;
}
else
{
MessageBox.Show("Data Synchronisation couldn't be completed because of connection problem... Please try again!!!");
}
}
else
{
MessageBox.Show("Unable to connect to Live Server...Please check your internet connection and try again!!!");
}
con.GetDisConnect();
con.GetDisConnectLive();
return success;
}
I have rearranged the code that handles the button state and text, so that it's still executed in the UI thread where it belongs, even though the method itself is not. You also never appeared to set the button back to the enabled state; it's not clear to me whether that was intentional or not, so I went ahead and added a line to do that.
Finally, I will strongly recommend you figure out a better way to report status to the user than the calls to MessageBox.Show() you have now. The biggest issue is that you don't even start doing any work until after the user dismisses the initial message, which immediately puts your progress bar out of sync with the actual work. But it's also better to keep all your UI in the UI thread, and to keep UI separate from non-UI logic (i.e. the SQL operation).
I've seen some great answers here and was wondering if someone could help me out.
Here's my code:
namespace expandGUIWPF
{
public static string getSHA256b64(string filepath)
{
byte[] bytes = SHA256.Create().ComputeHash(File.ReadAllBytes(filepath));
return Convert.ToBase64String(bytes);
}
private void btnRun_Click(object sender, RoutedEventArgs e)
{
{
string folder = txtFolder.Text;
string filelist = folder + "\\FileList.txt";
string[] test = Directory.GetFiles(folder, "*", System.IO.SearchOption.AllDirectories);
File.WriteAllLines(filelist, test);
int length = File.ReadLines(filelist).Count();
pBar1.Minimum = 1;
pBar1.Maximum = length;
File.WriteAllLines(filelist, test);
using (StreamReader sr = new StreamReader(filelist))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string oldfile = line;
string newfile = oldfile + ".expanded";
string oldhash = "";
string newhash = "";
try
{
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\test\test.exe", oldfile + " " + newfile);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
Thread.Sleep(1000);
if (File.Exists(oldfile))
{
oldhash = getSHA256b64(oldfile);
}
if (File.Exists(newfile))
{
newhash = getSHA256b64(newfile);
File.Delete(oldfile);
File.Move(newfile, oldfile);
}
pBar1.Value = pBar1.Value + 1;
txtLog.AppendText(oldfile + "\r\n Old: " + oldhash + "\r\n New: " + newhash + "\r\n");
if (!(oldhash == newhash))
{
txtLog.AppendText("Successfully expanded file \r\n");
}
else
{
txtLog.AppendText("Unable to expand file \r\n");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
}
}
}
The problem is that my progressbar isn't updating. I know a little C# but I'm a beginner to WPF and can't get my head around setting up a background worker to update my UI. Would someone be able to give me a few pointers please? Currently the app works fine, but the progressbar jumps to 100% finished and all of the text suddenly appears.
Thanks in advance!
Tom
First you'll want your background worker to handle the processes in its DoWork event. Within that event you can call the ProgressChanged event to update the progress bar. Below is an example:
private void btnRun_Click(object sender, RoutedEventArgs e)
{
if(workerThread.IsBusy == false) // Make sure someone doesn't click run multiple times by mistake
{
pBar1.Value = 0;
workerThread.RunWorkerAsync();
}
}
private void workerThread_DoWork(object sender, DoWorkEventArgs e)
{
// Definitions and so forth
pBar1.Minimum = 0;
pBar1.Maximum = length;
int status = 0;
using (StreamReader sr = new StreamReader(filelist))
{
string line;
while ((line = sr.ReadLine()) != null)
{
// Try/Catch work here
status++;
workerThread.ReportProgress(status);
}
}
private void workerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pBar1.Value = e.ProgressPercentage;
}
Just understand that the thread that's running the Form is the same one that will be used to update the form's controls. So if you have 'stuff' to do - like encrypting / decrypting lines from a file - you need to perform those items on another thread with a callback, otherwise the form display wont update until it's done with your stuff to do. You can raise events from inside a worker thread -- and catch them using an event handler on the main (form) thread to update the progress bar.
It seems that your UI thread is being blocked, in windows forms programming you have one message pump and while you main thread (UI) is doing something else it has to wait before it can process messages. You can fix this problem by setting up a background worker to send updates
For more information on UI thread and the message pump see this
http://www.codeproject.com/Articles/10311/What-s-up-with-BeginInvoke
For infomation on the backgroundworker thread see this
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Small Example code
// This event handler is where the time-consuming work is done.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
}
#Title
How do I determine the time it took to send the message?
Right now, when I click "send", my program simply is silent. I would like the program to show something like a progress bar that will load while the program is sending the message.
Is that possible?
As of the moment, this is what I have:
try
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand(getEmail, connect.connection);
cmd.Parameters.AddWithValue("#section", sectionSelect.SelectedValue);
adapter.SelectCommand = cmd;
System.Data.DataTable mailingList = new System.Data.DataTable();
adapter.Fill(mailingList);
foreach (DataRow row in mailingList.Rows)
{
string rows = string.Format("{0}", row.ItemArray[0]);
message.To.Add(rows);
}
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential(email, password.Password);
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Send(message);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
MessageBox.Show("Your message has been sent.");
}
Right now, when I click "send", my program simply is silent.
SmtpClient.Send(MailMessage) is not an asynchronous call, and will consequently block the calling thread until the operation is completed.
Alternatively, you can send the message with SmtpClient.SendAsync(MailMessage, Object) which will make the call asynchronously, allowing you to display any sort of wait dialog you want until the send completes.
You can then set up a callback function that reports the operation as complete to the user. There is a good example of this in the MSDN article I linked above.
You could launch this process on the UI thread and update a progress bar half way through you method above (or at several points), but this is not the ideal way of doing this. You would ideally want (depending on how long each message takes or indeed how many messages you were sending) launch your working process on a separate thread.
Note: this may be overkill if this is not a sufficiently long running process.
An easy way into using threading is to use a BackgroundWorker. So assuming this is a sufficiently long running procedure or something you may extend in the future to sending multiple messages in one batch you could launch this on a background thread as follows:
using System.Threading;
// Threading.
private BackgroundWorker bgWorker;
AutoResetEvent areProgressChanged = new AutoResetEvent(false);
private void SendYourMessage()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
try
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand(getEmail, connect.connection);
cmd.Parameters.AddWithValue("#section", sectionSelect.SelectedValue);
adapter.SelectCommand = cmd;
// Show your progress.
(bgWorker as BackgroundWorker).ReportProgress(progressBarValue, "Half Way through...");
DataTable mailingList = new DataTable();
adapter.Fill(mailingList);
foreach (DataRow row in mailingList.Rows)
{
string rows = string.Format("{0}", row.ItemArray[0]);
message.To.Add(rows);
}
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential(email, password.Password);
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Send(message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
stopwatch.Stop();
TimeSpan timeTaken = stopwatch.Elapsed;
MessageBox.Show(String.Format("Your message has been sent. That took {0}s", timeTaken.Seconds));
}
}
private void SendMyMessage_Click(object sender, EventArgs e)
{
// Start job on new thread.
bgWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
bgWorker.RunWorkerAsync();
}
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker thisWorker = sender as BackgroundWorker;
SendYourMessage();
}
void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Change progress bar (and form label).
this.progressBar.Value = e.ProgressPercentage;
this.label.Text = e.UserState;
// Tell the worker that the UIThread has been updated.
this.areProgressChanged.Set();
return;
}
// Once the work is complete do something.
void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Handle.
if (e.Cancelled || bgWorker.CancellationPending)
MessageBox.Show("Message cancelled at users request!");
else if (e.Error != null)
MessageBox.Show(String.Format("Error: {0}.", e.Error.ToString()));
return;
}
// To cancel the job.
private void cancelAsyncButton_Click(System.Object sender, System.EventArgs e)
{
if (bgWorker.WorkerSupportsCancellation)
bgWorker.CancelAsync();
}
This code may need tweaking. Essentially you set up some event to launch your process (here the SomeEvent_Click event) and this is the entry point to the BackgroundgroundWorker, the rest of the events I use here you can read about on MSDN.
Good luck.
I'm having some wierd behaviour in my WinForm application coded in c#.
in my:
private void buttonSave_Click(object sender, EventArgs e)
Im calling my function:
functions.sendStatusEmail();
The weird thing is that, when I press the Save button then email send is not triggered. But if I close my application the mail is handle and sent.
Have I missed something or do one need to trigger som application event manually to have run the sendout.
( I tried using client.SendAsync(mail,null); then it triggerd at click but the mail was empty)
Thanks in advance
--
Edit: code expamples
private void buttonSave_Click(object sender, EventArgs e)
{
// checks if a ticket is active
if (workingTicketId > 0)
{
// update ticket information
functions.updateTicketInfo(workingTicketId, comboBoxPriority.SelectedIndex,
comboBoxStatus.SelectedIndex, textBoxComment.Text);
// gives feedback
labelFeedback.Text = "Updated";
// updates the active ticket list
populateActiveTicketList();
// marks working ticket row in list
dataGridActiveTicketList.Rows[workingGridIndex].Selected = true;
// checks for change of ticket status
if (comboBoxStatus.SelectedIndex != workingTicketStatus)
{
// checks if contact person exists
if (labelContactPersonValue.Text.ToString() != "")
{
// sends email to contact person
functions.sendStatusEmail(labelContactPersonValue.Text, comboBoxStatus.SelectedIndex, workingTicketId, textBoxDescription.Text);
}
// updates working ticket status
workingTicketStatus = comboBoxStatus.SelectedIndex;
}
}
}
and the send email function:
// sends a status email to contact person
// returns noting
public void sendStatusEmail(string email, int newStatus, int ticketId, string ticketText)
{
// defines variables
string emailSubject;
string emailBody;
// some exkluded mailcontent handling
// sends mail
MailMessage mail = new MailMessage("myFromEmail#hidden.com",email,emailSubject,emailBody);
SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["MailSMTP"]);
mail.IsBodyHtml = true;
client.Send(mail);
// dispose
mail.Dispose();
}
Cannot understand why it would not work. I used below function and it sends the email successfully:
public static bool SendEmail (string smtpServer, string fromAddress, string fromDisplayName,
string toAddress, string subject, string contents, bool important) {
MailAddress from = new MailAddress (fromAddress, fromDisplayName);
MailPriority priority = important ? MailPriority.High : MailPriority.Normal;
MailMessage m = new MailMessage {
From = from,
Subject = subject,
Body = contents,
Priority = priority,
IsBodyHtml = false
};
MailAddress to = new MailAddress (toAddress);
m.To.Add (to);
SmtpClient c = new SmtpClient (smtpServer) { UseDefaultCredentials = false };
c.Send (m);
return true;
}
No offence but are you sure that it is closing the application which results in email being sent. Most of the times there is a delay between an email is sent and it is received because of the traffic on SMTP server. Try pressing that button and then wait for some time (3-4 minutes) and try refreshing your inbox during that time.
BTW, the SendAsync didnt work because you called mail.dispose() after the Async call.
The right way to do it in async would be,
private void button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage("from#domain.com", "to#domain.com", "subj", "body");
SmtpClient client = new SmtpClient("SMTPHOST");
mail.IsBodyHtml = true;
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
client.SendAsync(mail,mail);//send the mail object itself as argument to callback
}
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
//Mail sending is cancelled
}
if (e.Error != null)
{
//There is an error,e.Error will contain the exception
}
else
{
//Do any other success processing
}
((MailMessage)e.UserState).Dispose();//Dispose
}