ASP. net2.0 implements mail sending (test successful)

Source: Internet
Author: User
Tags mailmessage smtpclient

1. default. aspx Code As follows: Copy code The Code is as follows: <% @ page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" validaterequest = "false" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



No title page









email receiving:

subject:

content: width = "336px">

</Div>
<Table id = "Table2" runat = "server" border = "0" cellpadding = "0" cellspacing = "0" visible = "false">
<Tr>
<TD align = "center" style = "width: 400px">
<Asp: Label id = "label1" runat = "server" forecolor = "red" text = "Congratulations! "> </ASP: Label> <br/>
<Asp: button id = "button2" runat = "server" text = "return" onclick = "button2_click"/> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

2. The default. aspx. CS code is as follows:Copy codeThe Code is as follows: using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
// Enter the namespace
Using system. net;
Using system. net. mail;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}
Protected void button#click (Object sender, eventargs E)
{
//// Set the sender's mailbox and display name
Mailaddress from = new mailaddress ("zgdx0503@cpp114.com", "Yanbian Vocational Information Center ");
// Set the recipient's mailbox and display name
Mailaddress to = new mailaddress (textbox1.text, "0503 class ");
// Create a mailmessage object
Mailmessage omail = new mailmessage (from, );

Omail. Subject = textbox2.text; // mail title
Omail. Body = textbox3.text; // mail content

Omail. isbodyhtml = true; // specifies the mail format. The HTML format is supported.
Omail. bodyencoding = system. Text. encoding. getencoding ("gb2312"); // The email encoding.
Omail. Priority = mailpriority. High; // set the priority of the email to high.

// Sending email server
Smtpclient client = new smtpclient ();
Client. Host = "mail.cpp114.com"; // specify the email server
Client. Credentials = new networkcredential ("zgdx0503@cpp114.com", "123456"); // specify the server email and password

// Send
Try
{
Client. Send (omail); // send an email
Label1.text = "Congratulations! The email is sent successfully. ";
}
Catch
{
Label1.text = "failed to send the email. Check whether the network and mailbox are available. ";
}

Omail. Dispose (); // release resources

Table1.visible = false;
Table2.visible = true;
}
Protected void button2_click (Object sender, eventargs E)
{
// Return and continue sending
Response. Redirect (request. url. tostring ());
Table1.visible = true;
Table2.visible = false;
}
}

3. Run and enter the test mailbox zgdx0503@tom.com (password: 123456 ). As follows:

4. Open the mailbox to view

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.