When Asp.net sends an email, it inserts an image into the body.

Source: Internet
Author: User
Tags add time mailmessage smtpclient

# Region send email
/// <Summary>
/// Send an email through an attachment
/// </Summary>
/// <Param name = "emailfrom"> sender's email address </param>

/// <Param name = "Files"> image address </param>
/// <Param name = "emailfromname"> sender name </param>
/// <Param name = "zsrlist"> recipient set </param>
/// <Param name = "csrlist"> CC set </param>
/// <Param name = "arrfilepath"> attachment path </param>
/// <Param name = "mailsubject"> topic </param>
/// <Param name = "mailbody"> body content </param>
/// <Returns> </returns>
Public static bool sendemail (string emailfrom, string [] files, string emailfromname, ilist <CSR> zsrlist, ilist <CSR> csrlist, arraylist arrfilepath, string mailsubject, string mailbody)
{
Mailmessage email = new mailmessage ();
Try
{
Mailaddress emailfrom = new mailaddress (emailfrom, emailfromname); // create a sender email address object
Email. From = emailfrom; // specify the sender
Email. CC. Add (emailfrom); // send the sender as a CC
For (INT I = 0; I <zsrlist. Count; I ++) // cyclically Add the master mail address object
{
Mailaddress zsraddr = new mailaddress (zsrlist [I]. Email, zsrlist [I]. Name );
Email. to. Add (zsraddr );
}
For (INT I = 0; I <csrlist. Count; I ++) // cyclically Add the CC recipient email address object
{
Mailaddress csraddr = new mailaddress (csrlist [I]. Email, csrlist [I]. Name );
Email. CC. Add (csraddr );
}}
Catch
{
Return false;
} Email. isbodyhtml = true;
Email. bodyencoding = system. Text. encoding. utf8;
Email. Body + = mailbody;
For (INT I = 0; I <files. length; I ++)
{
System. net. Mail. Attachment attachment = new system. net. Mail. Attachment (files [I]);
Email. attachments. Add (Attachment );
Email. Body + = " ";
} Email. Priority = mailpriority. High;
// Email Subject
Email. Subject = mailsubject;
Email. subjectencoding = encoding. getencoding (936); // email attachment
For (INT I = 0; I <arrfilepath. Count; I ++)
{
String file = arrfilepath [I]. tostring (); // attachment path
Attachment DATA = new attachment (file, system. net. Mime. mediatypenames. application. octet );
// Add time stamp information for the file.
System. net. Mime. contentdisposition disposition = data. contentdisposition;
Disposition. creationdate = system. Io. file. getcreationtime (File );
Disposition. modificationdate = system. Io. file. getlastwritetime (File );
Disposition. readdate = system. Io. file. getlastaccesstime (File );
// Add the file attachment to this e-mail message.
Email. attachments. Add (data );
}
Smtpclient client = new smtpclient ("172.30.1.13 ");
Client. usedefacrecredentials = false;
Client. Credentials = new system. net. networkcredential ("Shoa", "shnetweb.1234 ");
Client. deliverymethod = smtpdeliverymethod. Network; try
{
Client. Send (email );
}
Catch (smtpfailedrecipientsexception ex)
{
For (INT I = 0; I <ex. innerexceptions. length; I ++)
{
Smtpstatuscode status = ex. innerexceptions [I]. statuscode;
If (status = smtpstatuscode. mailboxbusy | status = smtpstatuscode. mailboxunavailable)
{
// Response. Write ("Delivery failed-retrying in 5 seconds .");
System. Threading. thread. Sleep (5000 );
Client. Send (email );
}
}
}
Catch (exception ex)
{
Return false;
}
Finally
{
For (INT I = 0; I <email. attachments. Count; I ++) // release Excel Resources
{
Email. attachments [I]. Dispose ();
}
}
Return true;
}
# Endregion
Related Article

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.