asp.net summarizes several ways to send emails

  • 2020-11-03 22:06:07
  • OfStack

MailMessage
Provides properties and methods to create a mail message object. It is usually possible to build mailers by first building the MailMessage object and then setting its properties.

Commonly used attributes:
From -- The address to send the email
To -- Address to receive mail
Subject -- The subject line of the email
Priority -- Priority of message (valid values are High,Low,Normal)
Attachments -- Returns 1 collection representing attachments
Bcc -- BCC address
Cc -- CC address
Body - Gets or sets the content of an E-mail message
BodyFormat -- Gets or sets the enumerated value of MailFormat, which specifies the format of the message body message (Html, Text)
Bodyencoding -- Specifies the encoding of messages (mainly Base64,UUencode)
UrlContentBase: URL encoding in HTML format messages
UrlContentLocation: Priority of email messages (High, Medium,Low)

SmtpMail
The SMTP protocol, which is responsible for sending mail, provides properties and methods for sending mail messages using the federated data object of the windows 2000 CDOSYS messaging component.
The SmtpMail class USES the Send method, whose purpose is to send mail, and has two overloaded methods.

1. SmtpMail.Send (" Send email address "," Receive email address "," Subject of email message "," Content of Email Message ") This method is simple and not suitable for sending emails with attachments.

This method is complex, flexible, suitable for sending attachments, and can set various attribute values for the MailMessage object. If we are using ES63en.NET to write a mail sending program, then how do we get SMTP in the first place? There are two methods: the first method calls SMTP of the current well-known mail service provider, such as SMTP of free E-mail of sina, sohu and netease; the second method calls SMTP of free E-mail of sina, sohu and netease. The second method is to install 1 SMTP virtual server, which is installed at the time of installing IIS.

MailAttachment
An object class associated with mail attachments that provides properties and methods to create a mail attachment object.
The constructor
Create 1 attachment object
MailAttachment objMailAttachment = new MailAttachment("d:\test. txt" ); // Send email attachments
Call form


MailMessage objMailMessage= new MailMessage();
objMailMessage.Attachments.Add( objMailAttachment );// Attach attachments to the mail message object 

Enclosed mail delivery class


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
using System.Text;
public class SendMail
...{
    public SendMail()
    ...{
    }
    private string _host;
    /**//// <summary>
    ///  Server address 
    /// </summary>
    public string Host
    ...{
        get ...{ return _host; }
        set ...{ _host = value; }
    }
    private int _port;
    /**//// <summary>
    ///  Server port 
    /// </summary>
    public int Port
    ...{
        get ...{ return _port; }
        set ...{ _port = value; }
    }
    private string _smtpUsername;
    /**//// <summary>
    ///  User name of sender's email 
    /// </summary>
    public string SmtpUsername
    ...{
        get ...{ return _smtpUsername; }
        set ...{ _smtpUsername = value; }
    }
    private string _sendemail;
    /**//// <summary>
    ///  Email account of sender (only receive encrypted string, decrypt when sending email) 
    /// </summary>
    public string SendEmail
    ...{
        get
        ...{
            return _sendemail;
        }
        set ...{ _sendemail = value; }
    }
    private string _replyToEmail;
    /**//// <summary>
    ///  Reply to person's email account 
    /// </summary>
    public string ReplyToEmail
    ...{
        get ...{ return _replyToEmail; }
        set ...{ _replyToEmail = value; }
    }
    private string _replyUserName;
    /**//// <summary>
    ///  The user name of the respondent 
    /// </summary>
    public string ReplyUserName
    ...{
        get ...{ return _replyUserName; }
        set ...{ _replyUserName = value; }
    }
    private string _getemail;
    /**//// <summary>
    ///  Recipient email account 
    /// </summary>
    public string GetEmail
    ...{
        get ...{ return _getemail; }
        set ...{ _getemail = value; }
    }
    private string _smtpPassword;
    /**//// <summary>
    ///  Sender email password (only receive encrypted string, decrypt when sending email) 
    /// </summary>
    public string SmtpPassword
    ...{
        get ...{ return _smtpPassword; }
        set ...{ _smtpPassword = value; }
    }
    private string _content;
    /**//// <summary>
    ///  Email content 
    /// </summary>
    public string Content
    ...{
        get ...{ return _content; }
        set ...{ _content = value; }
    }
    private string _title;
    /**//// <summary>
    ///  Email title 
    /// </summary>
    public string Title
    ...{
        get ...{ return _title; }
        set ...{ _title = value; }
    }
    private string[] _cc = null;
    /**//// <summary>
    ///  Sent to your email 
    /// </summary>
    public string[] cc
    ...{
        get ...{ return _cc; }
        set ...{ _cc = value; }
    }
    private string[] _bcc = null;
    /**//// <summary>
    ///  Close to send email 
    /// </summary>
    public string[] bcc
    ...{
        get ...{ return _bcc; }
        set ...{ _bcc = value; }
    }
    /**//// <summary>
    /// Send E-mail 
    /// </summary>
    /// <returns> Return success or not </returns>
    public bool Send()
    ...{
        try
        ...{
            MailMessage objMailMessage;
            objMailMessage = new MailMessage(SendEmail, _getemail, _title, _content);
            if (!string.IsNullOrEmpty(_replyToEmail) && !string.IsNullOrEmpty(_replyUserName))
            ...{
                MailAddress reply = new MailAddress(_replyToEmail, _replyUserName);
                objMailMessage.ReplyToList.Add(reply);
            }
            objMailMessage.BodyEncoding = Encoding.GetEncoding(936);
            objMailMessage.IsBodyHtml = true;
            if (cc != null && cc.Length > 0)
            ...{
                foreach (string ccAddress in cc)
                ...{
                    objMailMessage.CC.Add(new MailAddress(ccAddress));
                }
            }
            if (bcc != null && bcc.Length > 0)
            ...{
                foreach (string bccAddress in bcc)
                ...{
                    objMailMessage.Bcc.Add(new MailAddress(bccAddress));
                }
            }
            SmtpClient client = new SmtpClient(this._host, this._port);
            if (!String.IsNullOrEmpty(this.SmtpUsername) && !String.IsNullOrEmpty(this.SmtpPassword))
            ...{
                client.Credentials = new NetworkCredential(this.SmtpUsername, this.SmtpPassword);
            }
            client.EnableSsl = false;
            client.Send(objMailMessage);
            objMailMessage.Dispose();
            return true;
        }
        catch
        ...{
            return false;
        }
    }
}

Call methods and steps:
1. Create 1 object of SendMail class and send the necessary parameters to this object in turn.
2. Call Send().

Email attachment, you can also extend the SendMail class as described above. I won't give you an example here.

ASP. NET USES SMTP on the native SMTP virtual server to send messages
Let's start with the SMTP configuration under 1.
(1) Right click "SMTP Virtual Server" and select "Properties" - > On the General TAB, set the IP address (P). I set 192.168.1.100.
(2) Select the "Access" TAB, click "Relay", select "Only the following list" (default is selected), click "Add", and add 192.168.1.100 to "Single computer".
If (2) is not completed, a common error occurs when the server rejects one or more recipient addresses. The server responded with 550 5.7.1 Unable to relay for scucj@126.com (friendly tip 1: The email address in the error is different) and then started the core code, which is similar to method (1). The main difference from (1) is: 1.SMTP, 2. objMailMessage. From, this method can be filled in casually, but (1) do not fill in casually, so using ES124en. NET(C#) to send the following core code:


         // Core code start  
         using System.Web.Mail; 
         MailMessage objMailMessage; 
         MailAttachment objMailAttachment; 
         //  create 1 10 attachment objects  
         objMailAttachment = new MailAttachment( "d:\test.txt" );// Send an attachment to the message  
         //  Create mail messages  
         objMailMessage = new MailMessage(); 
         objMailMessage.From = "mysina@sina.com";// Source email address  
         objMailMessage.To = "scucj@126.com";// Destination email address, which means send it to me  
         objMailMessage.Subject = " Email sent subject: Hello ";// The subject of the message being sent  
         objMailMessage.Body = " Message sent labeled content: Test 1 Under whether send successfully! ";// The content of the message sent  
         objMailMessage.Attachments.Add( objMailAttachment );// Attach attachments to the mail message object  
         //SMTP address  
         SmtpMail.SmtpServer = "192.168.1.100"; 
         // Start sending mail  
         SmtpMail.Send( objMailMessage );  

So that's where these two approaches come in. The easiest way to use the above method is to add a server button to the page and place the excepted statement into the button click event. Of course, don't forget to put the quoted statements at the top.


Related articles: