asp. net send mail samples to share

  • 2020-11-30 08:16:13
  • OfStack

mailhelper --mail helper class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mail;
/// <summary>
///mailhelper  Summary description of 
/// </summary>
public class mailhelper
{
    public mailhelper()
    {
        //
        //TODO:  Add the constructor logic here 
        //
    }
    /// <summary>
    ///  Mail sending 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>
    /// <param name="Subject"> Send theme </param>
    /// <param name="Attachment"> The attachment information </param>
    /// <param name="Body"> Email 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 messages 
        objMailMessage = new MailMessage();
        // The sender EMAIL
        objMailMessage.From = From;// Source email address 
        // The recipient EMAIL
        objMailMessage.To = Addressee; // Destination email Address 
        // Email sent to 
        objMailMessage.Cc = Copy;
        // mail misong
        objMailMessage.Bcc = secret;

        // Email subject 
        objMailMessage.Subject = Subject; // The subject of the message being sent 
        // Email content 
        objMailMessage.Body = Body;// The content of the message sent 
        //  create 1 10 attachment objects 
        if (Attachment != "")
        {
            objMailAttachment = new MailAttachment(Attachment);// Send an attachment to the message  c:\\test.txt
            objMailMessage.Attachments.Add(objMailAttachment);// Attach attachments to the mail message object 
        }
        // Then use SMTP To send an email that needs to be used Microsoft .NET Framework SDK v1.1 And the version above it 
        // Basic permissions 
        objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
        // The 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 none of the above 3 Line of code, the following error message appears: The server refused 1 Or more than one recipient address. The server responded as : 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 " The email has been sent successfully! ";
        }
        catch (System.Net.Mail.SmtpException ex)
        {
            return ex.Message;
        }
        // End of core code 
    }
}

And then down comes an demo I made myself

The front desk


<%@ 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() {
            var file_value = document.getElementById("File1").value;
            document.getElementById("HiddenField1").value = file_value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         To: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
         Cc: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
         Close to send: <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
         Topic: <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
         Content: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
         Attachment: <input id="File1" type="file" />
        <%--<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>--%>
        <br />
        <asp:Button ID="Button1" runat="server" Text=" send " OnClientClick="gei()" OnClick="Button1_Click" /><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    <asp:HiddenField ID="HiddenField1" runat="server" />
    </form>
</body>
</html>


Background:

protected void Button1_Click(object sender, EventArgs e)
    {        // Instance mail helper class 
        mailhelper mails = new mailhelper();
        string filePath = HiddenField1.Value;
        string a = mails.SendeEmal(TextBox1.Text, " Email account ", " Email password ", TextBox2.Text, TextBox4.Text, TextBox5.Text, filePath, TextBox3.Text);
        Label1.Text = a;
}


Related articles: