asp.net send mail sample sharing

Source: Internet
Author: User

  This article mainly introduces the asp.net send the mail sample sharing, the need friend can refer to the following

Mailhelper  -------Mail help class     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 Mailhel Per ()     {       //       //todo: Add constructor logic in here        //   /     ///<summary>    ///Mail send operation    ///</summary >    ///<param name= "Addressee" > Recipient address </param>    ///<param name= "from" > Sender address </param>    ///<param name= "Sendpassword" > Sender password </param>    ///<param Name= "Copy" > CC address </param>    ///<param name= "Secret" > Bcc address </param>    ///& Lt;param name= "Subject" > Send theme </param>    ///<param name= "Attachment" > Attachment information </param>    ///<param name= "Body" > Mail content </param>     public string sendeemal (string addressee, string Fro M, 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        //Mail cc         objmailmessage.cc = Copy;        //Mail Misong         OBJMAILMESSAGE.BCC = secret;            //Mail subject         objmailmessage.subject = Subject; Send the title of the message          //mail content         objmailmessage.body = body;//Email content          //Create an Attachment object         if (attachment!= "")         {  &N Bsp         objmailattachment = new MailAttachment (attachment)//email attachments c:test.txt       & nbsp     OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attach attachment to mail message object        }   & nbsp      //Then use SMTP to send mail, you need to use the Microsoft. NET Framework SDK v1.1 and its version        /basic Permissions &nbsp ;       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);     &NBSp  //password         OBJMAILMESSAGE.FIELDS.ADD ("http://schemas.microsoft.com/cdo/configuration/ Sendpassword ", Sendpassword);        //If there are no above three lines of code, the following error prompts: The server rejected one or more recipient addresses. Server response: 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         {    &N Bsp       Smtpmail.send (objmailmessage);             return "Mail sent successfully! ";        }         catch (System.Net.Mail.SmtpException ex)       &N Bsp {            return ex. message;        }         Core code end    }}       then come down and do it yourself. A demo--  front  , 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 "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head runat=" Server ">     <title></ title>     <script src= ". /style/jquery/jquery.js "type=" Text/javascript "></script>     <script src=". /style/jquery/jquery.validate.js "type=" Text/javascript "></script>     <script language=" JavaScript "type=" Text/javascript ">           function GEi () {        &NB Sp   var file_value = document.getElementById ("File1"). Value;             document.getElementById ("HiddeNField1 "). Value = File_value;        }     </script> </head> <body>     <form id= "Form1" R unat= "Server" >     <div>         sent to: <asp:textbox id= "TextBox1" runat= "Server" & Gt;</asp:textbox><br/>         cc: <asp:textbox id= "TextBox2" runat= "Server" > </asp:textbox><br/>         MIDI: <asp:textbox id= "TextBox4" runat= "Server" ></ Asp:textbox><br/>         theme: <asp:textbox id= "TEXTBOX5" runat= "Server" ></asp: Textbox><br/>         content: <asp:textbox id= "TextBox3" runat= "Server" ></asp: Textbox><br/>         accessories: <input id= "File1" type= "file"/>         <%--<asp:textbox id= "TextBox6" runat= "server" ></asp:TextBox>--%>         <BR />       &nbsp <asp:button id= "Button1" runat= "Server" text= "send" onclientclick= "GEi ()" onclick= "Button1_Click"/><br nbsp       <asp:label id= "Label1" runat= "Server" text= "" ></asp:Label>     </div> & nbsp   <asp:hiddenfield id= "HiddenField1" runat= "server"/>     </form> </body> </html >       Background: Code as follows: protected void Button1_Click (object sender, EventArgs e)     {       //Instance Mail help class         mailhelper mails = new Mailhelper ();           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; }  

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.