Asp.net mail sending class (with attachment)

Source: Internet
Author: User
Tags ticket mailmessage smtpclient
Asp tutorial. net mail sending class (with attachment)
Using system;
Using system. collections. generic;
Using system. text;
Using system.net;
Using system.net. mail;
Using system.net. mime;
Namespace windowsformsapplication1
{
Class sendmail
    {
Private mailmessage;
Private smtpclient;
Private string password; // The sender's password.
/// <Summary>
/// The instance of the class after review
/// </Summary>
/// <Param name = "to"> recipient address </param>
/// <Param name = "from"> sender address </param>
/// <Param name = "body"> email body </param>
/// <Param name = "title"> subject of the email </param>
/// <Param name = "password"> Sender password </param>
Public sendmail (string to, string from, string body, string title, string password)
        {
Mailmessage = new mailmessage ();
Mailmessage. to. add ();
Mailmessage. from = new system.net. mail. mailaddress (from );
Mailmessage. subject = title;
Mailmessage. body = body;
Mailmessage. isbodyhtml = true;
Mailmessage. bodyencoding = system. text. encoding. utf8;
Mailmessage. priority = system.net. mail. mailpriority. normal;
This. password = password;
        }
/// <Summary>
/// Add an attachment
/// </Summary>
Public void attachments (string path)
        { 
String [] path = path. split (',');
Attachment data;
Contentdisposition disposition;
For (int I = 0; I <path. length; I ++)
            { 
Data = new attachment (path [I], mediatypenames. application. octet); // instantiate the attachment
Disposition = data. contentdisposition;
Disposition. creationdate = system. io. file. getcreationtime (path [I]); // Obtain the attachment creation date
Disposition. modificationdate = system. io. file. getlastwritetime (path [I]); // Obtain the attachment modification date
Disposition. readdate = system. io. file. getlastaccesstime (path [I]); // Obtain the read date of the attachment
Mailmessage. attachments. add (data); // add it to the attachment.
            } 
        }
/// <Summary>
/// Send mail asynchronously
/// </Summary>
/// <Param name = "completedmethod"> </param>
Public void sendasync (sendcompletedeventhandler completedmethod)
        {
If (mailmessage! = Null)
            {
Smtpclient = new smtpclient ();
Smtpclient. credentials = new system.net. networkcredential (mailmessage. from. address, password); // Set the sender identity ticket
Smtpclient. deliverymethod = system.net. mail. smtpdeliverymethod. network;
Smtpclient. host = "smtp." + mailmessage. from. host;
Smtpclient. sendcompleted + = new sendcompletedeventhandler (completedmethod); // register the event when the asynchronous mail sending is completed
Smtpclient. sendasync (mailmessage, mailmessage. body );
            }
        }
/// <Summary>
/// Send an email
/// </Summary>
Public void send ()
        {
If (mailmessage! = Null)
            {
Smtpclient = new smtpclient ();
Smtpclient. credentials = new system.net. networkcredential (mailmessage. from. address, password); // Set the sender identity ticket
Smtpclient. deliverymethod = system.net. mail. smtpdeliverymethod. network;
Smtpclient. host = "smtp." + mailmessage. from. host;
Smtpclient. send (mailmessage );
            }
        } 
    }
}
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.