ASP tutorial. NET Three send mail code (STMP, no component mail sent)
public bool Sendmails ()
{
SmtpClient _smtpclient = new SmtpClient ();
_smtpclient.deliverymethod = smtpdeliverymethod.network;//Specify how e-mail is sent
_smtpclient.host = "IP address";//Specify SMTP server
_smtpclient.credentials = new System.Net.NetworkCredential (_straccount, _strpwd);//username and password
MailMessage _mailmessage = new MailMessage ("molizuqiuba@163.com", "11111111111@qq.com");
_mailmessage.subject = "mail test";//Subject
_mailmessage.body = "message sent successfully ...";//Content
_mailmessage.bodyencoding = system.text.encoding.utf8;//Body encoding
_mailmessage.isbodyhtml = true;//set to HTML format
_mailmessage.priority = mailpriority.high;//Priority
Try
{
_smtpclient.send (_mailmessage);
Response.Write (' <script>alert (' Mail sent successfully ... '); Window.history ( -1) </script> ");
return true;
}
Catch
{
Response.Write (' <script>alert (' Mail send Shibai ... '); Window.history ( -1) </script> ");
return false;
}
}
%>
Three classes are required: MailMessage, SmtpClient, NetworkCredential.
The MailMessage, SmtpClient namespaces are:
System.Net.Mail
The NetworkCredential namespace is:
System.Net
MailMessage mail = new MailMessage ("Sender mail Address", "receiving mail address");
mail.subjectencoding = Encoding.UTF8;
Mail.subject = "Mail title";
Mail.isbodyhtml = true; Whether 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 authentication
Smtp.send (mail);
Mail.attachments.dispose (); Send message complete, release lock on attachment
<%
See a no component Send mail code
using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
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>
///mail Send
</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 Construction and destructor function
Public Mailobj ()
{
_strhost = "smtp.163.com"; STMP server Address
_straccount = "AA"; SMTP Service Account
_strpwd = "123456"; SMTP Service password
_strfrom = "aa@163.com"; Mailing address of sending party
}
<summary>
Send mail 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.111cn.net</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 mail
public bool SendMail (string to, string title, string content)
{
SmtpClient _smtpclient = new SmtpClient ();
_smtpclient.deliverymethod = smtpdeliverymethod.network;//Specify how e-mail is sent
_smtpclient.host = _strhost;//Specify SMTP server
_smtpclient.credentials = new System.Net.NetworkCredential (_straccount, _strpwd);//username and password
MailMessage _mailmessage = new MailMessage (_strfrom, to);
_mailmessage.subject = title;//Theme
_mailmessage.body = content;//Content
_mailmessage.bodyencoding = system.text.encoding.utf8;//Body encoding
_mailmessage.isbodyhtml = true;//set 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 111cn.net", "<b> content </b>");
_mail.dispose ();