Asp.net sends email

Source: Internet
Author: User
Tags mailmessage
Email. aspx:

Email. aspx. CS:

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Web. mail;
Using system. IO;

Namespace Test
{
///

/// Email abstract description.
///

Public class email: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Button button1;
Protected system. Web. UI. webcontrols. textbox tbsubject;
Protected system. Web. UI. webcontrols. textbox tbbody;
Protected system. Web. UI. htmlcontrols. htmlinputfile upfile;
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist1;
Protected system. Web. UI. webcontrols. Button button2;
Protected system. Web. UI. webcontrols. Button button3;
Protected system. Web. UI. webcontrols. textbox TBTO;

Private void page_load (Object sender, system. eventargs E)
{
If (! This. ispostback)
{
}
}

# Generated by region web Form DesignerCode
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

///

/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///

Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. button2.click + = new system. eventhandler (this. button2_click );
This. button3.click + = new system. eventhandler (this. button3_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void button#click (Object sender, system. eventargs E)
{
Mailmessage mymail = new mailmessage ();
Mymail. Subject = This. tbsubject. Text. Trim ();
Mymail. From = "smallfools@sina.com ";
Mymail. To = This. TBTO. Text. Trim ();
Mymail. Body = This. tbbody. text;
Mymail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1 );
Mymail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "smallfools ");
Mymail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword ","****");

If (this. dropdownlist1.items. Count> 0)
{
For (INT I = 0; I <this. dropdownlist1.items. Count; I ++)
{
Mailattachment myattachment = new system. Web. Mail. mailattachment (server. mappath (this. dropdownlist1.items [I]. Value ));
Mymail. attachments. Add (myattachment );
}

}

Smtpmail. smtpserver = "smtp.sina.com.cn ";
Smtpmail. Send (mymail );

If (this. dropdownlist1.items. Count> 0)
{
For (INT I = 0; I <this. dropdownlist1.items. Count; I ++)
{
Fileinfo myfile = new fileinfo (server. mappath (this. dropdownlist1.items [I]. Value ));
Myfile. Delete ();
}
}

Page. registerclientscriptblock ("OK ","");
}

Private void button2_click (Object sender, system. eventargs E)
{
If (this. upfile. postedfile. contentlength! = 0)
{
String clientfilename = This. upfile. postedfile. filename;
String [] strtemp = clientfilename. Split ('.');
String upfileexp = strtemp [strtemp. Length-1]. tostring ();
String serverfilename = datetime. Now. tostring ("yyyymmddhhmmss") + "." + upfileexp;
This. upfile. postedfile. saveas (server. mappath (serverfilename ));
This. dropdownlist1.items. Add (New listitem (clientfilename, serverfilename ));

If (this. dropdownlist1.items. Count> 0)
{
This. button3.enabled = true;
}
Else
{
This. button3.enabled = false;
}
}
}

Private void button3_click (Object sender, system. eventargs E)
{
Fileinfo myfile = new fileinfo (server. mappath (this. dropdownlist1.selectedvalue ));
Myfile. Delete ();

This. dropdownlist1.items. Remove (this. dropdownlist1.items [This. dropdownlist1.selectedindex]);

If (this. dropdownlist1.items. Count> 0)
{
This. button3.enabled = true;
}
Else
{
This. button3.enabled = false;
}
}
}
}

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.