使用smtpClient.send显示进度条

时间:2011-02-23 13:20:07

标签: c# email smtp

我正在使用此功能通过Gmail发送邮件。

private bool uploadToGmail(string username, string password , string file , 
    string backupnumber)
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("jain@gmail.com");
    mail.To.Add("jain@gmail.com");
    mail.Subject = "Backup mail- Dated- " + DateTime.Now + " part - " + 
        backupnumber;
    mail.Body = "Hi self. This mail contains \n backup number- " + 
        backupnumber + " \n Dated- " + DateTime.Now ;

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment(file);
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = 
        new System.Net.NetworkCredential("jain@gmail.com", "password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Timeout = 999999999;
    SmtpServer.Send(mail);
    //  MessageBox.Show("mail Sent");
    return true;
}

现在我想显示一个进度条(如果有大的附件)来显示上传。这可能吗 ?我想我知道如何使用进度条,但不知道如何使用它与Smtpclient.send()。

任何帮助?

由于

1 个答案:

答案 0 :(得分:3)

您应该使用SendAsync并订阅SendCompleted,以便知道发送邮件的时间。虽然没有办法获得发送过程的进展......