C # resolves messages when you send Excel attachments, Excel is locked

Source: Internet
Author: User
Tags mailmessage

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.