Asp.net three email sending codes (stmp, no component email sending)

Source: Internet
Author: User
Tags mailmessage smtpclient

Asp.net three email sending codes (stmp, no component email sending)

Asp tutorial. net three email sending code (stmp, no component email sending)

Public bool sendmails ()
{
Smtpclient _ smtpclient = new smtpclient ();
_ Smtpclient. deliverymethod = smtpdeliverymethod. network; // specify the email sending Method
_ Smtpclient. host = "ip address"; // specify the smtp server
_ Smtpclient. credentials = new system.net. networkcredential (_ straccount, _ strpwd); // user name and password

Mailmessage _ mailmessage = new mailmessage ("molizuqiuba@163.com", "11111111111@qq.com ");

_ Mailmessage. subject = "mail test"; // subject

_ Mailmessage. body = "the email is successfully sent..."; // content
_ Mailmessage. bodyencoding = system. text. encoding. utf8; // body encoding
_ Mailmessage. isbodyhtml = true; // set it to html Format
_ Mailmessage. priority = mailpriority. high; // priority
Try
{
_ Smtpclient. send (_ mailmessage );
Response. write ("<script> alert ('email sent successfully ............. '); window. history (-1) </script> ");
Return true;
}
Catch
{
Response. write ("<script> alert ('send shibai ............. '); window. history (-1) </script> ");
Return false;
}
}

%>

Three classes are required: mailmessage, smtpclient, and networkcredential.

The namespace of mailmessage and smtpclient is:

System.net. mail
The namespace of networkcredential is:

System.net

Mailmessage mail = new mailmessage ("sender's email address", "recipient's email address ");
Mail. subjectencoding = encoding. utf8;
Mail. subject = "mail title ";
Mail. isbodyhtml = true; // whether the content is allowed in html Format
Mail. bodyencoding = encoding. utf8;
Mail. body = "<strong> system.net. mail </strong> ";
Mail. attachments. add (new attachment ("e: \ foo.txt"); // add an attachment

Smtpclient smtp = new smtpclient ("smtp server address ");
Smtp. credentials = new networkcredential ("Login Name", "password"); // smtp Verification
Smtp. send (mail );

Mail. attachments. dispose (); // After the email is sent, the attachment is locked.


<%
// Check the code for sending emails without Components

Using system;
Using system. data;
Using system. configuration;
Using system. web;
Using system. web. security;
Using system. web. ui;
Using system.web.ui.html controls;
Using system. web. ui. webcontrols;
Using system. web. ui. webcontrols. webparts;
Using system. collections. generic;
Using system.net. mail;
Using system. text;

Namespace ec
{
/// <Summary>
/// Send email
/// </Summary>
Public class mailobj
{
Private string _ strhost = string. empty;
Private string _ straccount = string. empty;
Private string _ strpwd = string. empty;
Private string _ strfrom = string. empty;

# Region constructor and destructor
Public mailobj ()
{
_ Strhost = "smtp.163.com"; // address of the stmp Server
_ Straccount = "aa"; // smtp service account
_ Strpwd = "123456"; // smtp service Password
_ Strfrom = "aa@163.com"; // the sender's email address
}

/// <Summary>
/// Send email purchase function
/// </Summary>
/// <Param name = "strhost"> stmp server address: smtp.163.com </param>
/// <Param name = "straccount"> smtp service account: liugongxun </param>
/// <Param name = "strpwd"> smtp service password: www. bKjia. c0m </param>
/// <Param name = "strfrom"> sender email address: liugongxun@163.com </param>
Public mailobj (string strhost, string straccount, string strpwd, string strfrom)
{
_ Strhost = strhost;
_ Straccount = straccount;
_ Strpwd = strpwd;
_ Strfrom = strfrom;
}


~ Mailobj ()
{
Dispose ();
}

Public void dispose ()
{
Gc. suppressfinalize (this );
}
# Endregion

# Region send email
Public bool sendmail (string to, string title, string content)
{
Smtpclient _ smtpclient = new smtpclient ();
_ Smtpclient. deliverymethod = smtpdeliverymethod. network; // specify the email sending Method
_ Smtpclient. host = _ strhost; // specify the smtp server
_ Smtpclient. credentials = new system.net. networkcredential (_ straccount, _ strpwd); // user name and password

Mailmessage _ mailmessage = new mailmessage (_ strfrom, );
_ Mailmessage. subject = title; // subject
_ Mailmessage. body = content; // content
_ Mailmessage. bodyencoding = system. text. encoding. utf8; // body encoding
_ Mailmessage. isbodyhtml = true; // set it to html Format
_ Mailmessage. priority = mailpriority. high; // priority
Try
{
_ Smtpclient. send (_ mailmessage );
Return true;
}
Catch
{
Return false;
}
}
# Endregion
}
}

// Call Method

Mailobj _ mail = new mailobj ();
_ Mail. sendmail ("lxx@qq.com", "test bKjia. c0m", "<B> content </B> ");
_ Mail. dispose ();

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.