namespace WindowsFormsAppEmailClient
{
public partial class Form1 : Form
{
MailMessage MyMsg = new MailMessage();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnSend_Click(object sender, EventArgs e)
{
SmtpClient MyServer = new SmtpClient("smtp.gmail.com", 587);
//defining global MyMsg so we use it in attachment button
// MailMessage MyMsg = new MailMessage();
MyMsg.From = new MailAddress( "asharkashif2@gmail.com");
MyMsg.To.Add(tbTo.Text);
if (tbcc.Text != "")
{
MyMsg.CC.Add(tbcc.Text);
}
MyMsg.Subject = tbSub.Text;
//message = body
MyMsg.Body = tbMsg.Text;
MyServer.EnableSsl = true;
//Credentials used for defining User Name + Password
MyServer.Credentials = new System.Net.NetworkCredential("asharkashif2@gmail.com", "03456016286");
MyServer.Send(MyMsg);
}
private void btnAtt_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
Attachment myFile = new Attachment(openFileDialog1.FileName);
MyMsg.Attachments.Add(myFile);
}
}
}
问题是:我想在电子邮件发送成功时显示成功消息框。请帮忙
答案 0 :(得分:0)
你试过MessageBox.Show ??
答案 1 :(得分:0)
根据您的代码以及您在公共论坛上发布实际凭据的事实,我假设您只需要一个同步解决方案,并希望在(阻止)调用MyServer.Send();
<后显示一个消息框/ p>
将您的发送封装在try/catch
块中:
// The program will attempt to send a message
try
{
MySever.Send(MyMsg);
MessageBox.Show("Message sent successfully");
}
catch (Exception ex)
{
// Whoops there was an error sending the message, better tell the user what happened.
MessageBox.Show(String.Format("Message Failed To send because: {0}", ex.message);
}
如果这不起作用,那么您需要在问题中添加一些上下文,例如您看到的任何错误或异常。
执行此操作的“正确”方法是执行MyServer.SendAsync(MyMsg)
,然后让回调听取SendCompleted
类提供给您的SMTPClient
事件: