This article mainly introduces the ASP. e-mail sample sharing, the needs of friends can refer to the following
Mailhelper-------Mail Help class
The code is as follows:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mail;///< Summary>///mailhelper Summary description///</summary>public class mailhelper{public mailhelper () {//// TODO: Add constructor logic here//}///<summary>//Mail Send///</summary>//<param name= "Addre Ssee "> Recipient address </param>//<param name=" from "> Sender address </param>///<param Name=" Sendpassword "> Hair Person password </param>//<param name= "Copy" > CC address </param>//<param name= "Secret" > Bcc address </PARAM&G T <param name= "Subject" > Send subject </param>//<param name= "Attachment" > Attachment information </param>//<par Am Name= "Body" > Message content </param> public string Sendeemal (string addressee, string from, String Sendpassword, String Copy, String secret, String Subject, String Attachment, String Body) {MailMessage objmailmessage; MailAttachment objmailattachment; Create mail message objmailmessage = new MailMessage (); Sender Email Objmailmessage.from = from;//Source Email address//Recipient Email objmailmessage.to = addressee; Destination email address/email cc objmailmessage.cc = copy; Mail Misong objmailmessage.bcc = secret; Message subject Objmailmessage.subject = Subject; Send Message title//message Content Objmailmessage.body = body;//The contents of the message//Create an Attachment object if (Attachment! = "") {objmailattachment = new mailattachment (Attachment);//Send mail attachment c:\\test.txt Objmailmessage.attachme Nts. Add (objmailattachment);//attach attachments to the mail message object}//Then use SMTP to send mail, need to use the Microsoft. NET Framework SDK v1.1 and more than its version Basic Permissions ObjMailMessage.Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); User Name String name = From.substring (0, From.indexof (' @ ')); OBJMAILMESSAGE.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendusername", name); Password ObjMailMessage.Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", Sendpassword); If you do not have the above three lines of code, the following error message appears: The server rejected one or more recipient addresses. The server response is: 554:client host rejected:access denied//SMTP address string smtp = "smtp." + from.substring (from . IndexOf (' @ ') + 1); Smtpmail.smtpserver = "smtp." + from.substring (From.indexof (' @ ') + 1); Start sending mail try {smtpmail.send (objmailmessage); Return "Mail sent successfully! "; } catch (System.Net.Mail.SmtpException ex) {return ex. Message; }//Core code end}}
And then down is a demo--of his own.
Front desk
The code is as follows:
<%@ page language= "C #" autoeventwireup= "true" codefile= "Mail.aspx.cs" inherits= "Information_mail" validaterequest = "false"%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Background:
The code is as follows:
protected void Button1_Click (object sender, EventArgs e) { //Instance Mail helper class mailhelper mails = new Mailhelper (); c3/>string FilePath = hiddenfield1.value; String a = mails. Sendeemal (TextBox1.Text, "Mail Account", "Mail Password", TextBox2.Text, Textbox4.text, Textbox5.text, FilePath, textbox3.text); Label1.Text = A;}