When you send an Excel attachment by using the MailMessage class that comes with C #, opening Excel again prompts you to be locked.
Workaround: Attachment is the class that added the attachment, and the class was not released after the message was sent
public string Sendsmtpemail (string strsmtpserver, String strfrom, String Strfrompass, String Strto, string strsubject, str ing strbody, string excelpath)
{
Try
{
This write prevents Excel from being locked when Excel attachments are sent
using (var message = new MailMessage (strfrom, Strto, Strsubject, strbody))
{
using (var attachment = new Attachment (Excelpath))//Add attachment
{
Message. Attachments.Add (attachment);
Message. bodyencoding = System.Text.Encoding.UTF8;
Message. Isbodyhtml = true;
using (var client = new SmtpClient (strsmtpserver))
{
Client. Timeout = 9999999;
Client. useDefaultCredentials = false;
Client. Credentials = new System.Net.NetworkCredential (strfrom, Strfrompass);
Client. Deliverymethod = Smtpdeliverymethod.network;
Client. Send (message);
}
}
}
}
catch (Exception ex)
{
Program.form.text.Invoke (ex. ToString ());
Return ex. ToString ();
}
Return "";
}
C # resolves messages when you send Excel attachments, Excel is locked