【发布时间】:2018-09-19 09:44:56
【问题描述】:
我有一个基于 datagridview 数据发送电子邮件的程序。发送电子邮件时效果很好,但我希望在每次函数调用后更改文本框的内容。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;
using System.Net.Mail;
using System.Net;
namespace sendEmail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnGetData_Click(object sender, EventArgs e)
{
string connectionString = "connectionString";
string sql = "sqlQuery";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "data");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "data";
}
private void btnSend_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
textBox1.Text += "Processing .... " + row.Cells["data"].Value.ToString();
sendMail("subject", row.Cells["data"].Value.ToString(), "emailString");
)
}
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
}
private void sendMail (string subject, string content, string email)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myEmail@gmail.com");
mail.To.Add("toEmail@gmail.com");
mail.Subject = "Mail for " + content;
mail.Body = content;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myEmail@gmail.com", "myPassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
textBox1.Text += " -> Success\n";
}
catch
{
textBox1.Text += " -> Fail\n";
}
}
}
}
在运行程序时,文本框的内容只有在函数一直调用时才会改变(datagrid循环中的所有行)。
那么,我怎样才能让文本框在每次调用函数时更改内容。
感谢阅读并请帮助!
【问题讨论】: