Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c# - Show Progress bar with smtpClient.send

I am using this function to send mails via 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("[email protected]");
    mail.To.Add("[email protected]");
    mail.Subject = "Backup mail- Dated- " + DateTime.Now + " part - " + 
        backupnumber;
    mail.Body = "Hi self. This mail contains 
 backup number- " + 
        backupnumber + " 
 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("[email protected]", "password");
    SmtpServer.EnableSsl = true;

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

Now I want to show a progress bar (in case there is a large attachment) to show the upload . Is this possible ? I think I know how to use a progress bar, but don't know how to use it with Smtpclient.send() .

any help ?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should use SendAsync and subscribe to SendCompleted, to know, when the sending your mail completed. There is no way to get the progress of the send process, though...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...