asp.net using System.Net.Mail mail to send an implementation

Source: Internet
Author: User
Tags mailmessage smtpclient

I'll give you an example of a mass mailing with C #

1,. Net Messaging functionality needs to be added. Net.mail reference using System.Net.Mail;

2, can be based on their own needs to design a simple front page:

Define a method for getting message send parameters:

The code is as follows Copy Code

public void Email ()
    {
        mailaddress Messagefrom = new MailAddress (txtsender.text); //Sender's mailbox address
        string Messageto = txtreciever.text; //Recipient mailbox Address
        String messagesubject = txttitle.text;       //Mail subject
        String messagebody = txtareacontent.innertext;   //message content
         if (Send (Messagefrom, Messageto, MessageSubject, MessageBody))
        {
            Response.Write ("<script type= ' text/ JavaScript ' >alert (' send success! '); History.go ( -1) </script> ");//Sending success prompts you to return to the current page;

}
Else
{
Response.Write (' <script type= ' text/javascript ' >alert (' Send failed! '); History.go ( -1) </script> ");
}

}


Send Mail method:

The code is as follows Copy Code

public static bool Send (MailAddress messagefrom, String messageto, String messagesubject, String messagebody)
{

MailMessage message = new MailMessage ();//object to create a messaging message
Message. from = Messagefrom;
Message.              To.add (Messageto); The recipient's mailbox address can be multiple to achieve mass
Message. Subject = MessageSubject;
Message. BODY = MessageBody;
Message.              Isbodyhtml = false; is HTML format
Message.  Priority = Mailpriority.high; Priority of sending messages
SmtpClient sc = new SmtpClient (); Simple Mail Transfer Protocol (easy message Transfer Protocol)
Sc.              Host = "smtp.qq.com"; Specifies that the address of the server where the message is sent or that the IP use other mailboxes should be changed ru:smtp. 126.com
Sc.                          Port = 25; Specify Send mail port
Sc. useDefaultCredentials = true;
Sc. Enablessl = false;
Sc. Credentials = new System.Net.NetworkCredential ("Send Mailbox", "Mailbox Password"); Specify the user name and password for the login server
Try
{
Sc.      Send (message); Send mail
}
catch (Exception e)
{
return false;
}

return true;
}

Let's introduce another implementation that implements sending mailboxes.

The code is as follows Copy Code

Using System.Net;
Using System.Net.Mail;

SmtpClient smtp = new SmtpClient (); Instantiate a SmtpClient
Smtp. Deliverymethod = Smtpdeliverymethod.network; Set SMTP Outbound to Network
Smtp. Enablessl = FALSE;//SMTP Whether SSL encryption is enabled on the server

Smtp. Host = "smtp.163.com"; Specify SMTP server address
Smtp.             Port = 25; Specifies the port of the SMTP server, the default is 25, and if the default port is used, you can omit

If your SMTP server does not need authentication, use the following method, however, there is no need for authentication at present.
Smtp. useDefaultCredentials = true;
If authentication is required, use the following method
Smtp. Credentials = new NetworkCredential ("Mailbox Account @163.com", "Mailbox Password");

MailMessage mm = new MailMessage (); Instantiate a message Class

Mm. Priority = Mailpriority.high; Message priority, divided into low, normal, high, usually with normal

Mm. from = new MailAddress ("Mailbox Account @163.com", "Really interesting", encoding.getencoding (936));
The source of the message that the receiving party sees;
The first parameter is the sender's e-mail address.
The second parameter is the name displayed by the sender
The third parameter is the encoding used by the second parameter, and if the designation is incorrect, the opponent will receive a garbled display
936 is the codepage value of Simplified Chinese

Note: The above mail source, must and you log in the mailbox of the same account, or will fail to authenticate

Mm. ReplyTo = new MailAddress ("test_box@gmail.com", "My Receive Mailbox", Encoding.GetEncoding (936));
ReplyTo represents the default receiving address when replying to a message, that is: you send a letter with a mailbox, but use another to receive
The meaning of the latter two parameters, with the meaning of the

Mm. Cc. ADD ("a@163.com,b@163.com,c@163.com");
CC, support mass, multiple e-mail addresses separated by a half-width comma

Of course, you can also use the full address as follows:
Mm. Cc. ADD (New MailAddress ("A@163.com", "cc Person A", encoding.getencoding (936)));
Mm. Cc. ADD (New MailAddress ("B@163.com", "cc B", encoding.getencoding (936)));
Mm. Cc. ADD (New MailAddress ("C@163.com", "CC C", encoding.getencoding (936)));

Mm. Bcc.add ("d@163.com,e@163.com");
The secret sender of the message, support mass, multiple e-mail addresses separated by a half-width comma

Of course, you can also use the full address as follows:
Mm. Cc. ADD (New MailAddress ("d@163.com", "Dense transmitter D", encoding.getencoding (936));
Mm. Cc. ADD (New MailAddress ("e@163.com", "dense send E", encoding.getencoding (936)));

Mm. Sender = new MailAddress ("xxx@xxx.com", "Mail Sender", encoding.getencoding (936));
Can be set arbitrarily, this information is included in the message header, but does not verify the validity and is not displayed to the recipient
To tell you the truth, I don't know what the real effect is, we can ignore it and not write it.

Mm. To.add ("g@163.com,h@163.com");
The recipient of the message, support mass, separated by a half-width comma between multiple addresses

Of course, it can be added with full address

Mm. To.add (New MailAddress ("g@163.com", "Receiver G", encoding.getencoding (936));
Mm. To.add (New MailAddress ("h@163.com", "Receiver H", encoding.getencoding (936));

Mm. Subject = "This is the mail title"; Message headers
Mm. subjectencoding = encoding.getencoding (936);
Here is very important, if your mail title contains Chinese, here must be specified, or the other side received very likely garbled.
936 is the pagecode of Simplified Chinese, if it is English title, this sentence can be ignored

Mm. Isbodyhtml = true; Whether the message body is HTML format

Mm. bodyencoding = encoding.getencoding (936);
The message body code, set incorrectly, the receiver will receive garbled

Mm. BODY = "<font color=" Red "> Email test, hehe </font>";
Message body

Mm. Attachments.Add (New Attachment (@ "D:a.doc", SYSTEM.NET.MIME.MEDIATYPENAMES.APPLICATION.RTF));
Add an attachment, the second parameter, which represents the file type of the attachment, you can do without specifying
You can add multiple attachments
Mm. Attachments.Add (The new Attachment (@ "D:b.doc"));

Smtp. Send (mm); Send the message, and if you don't return an exception, you are done.

The

Self-asp.net refers to a using System.Net.Mail namespace, and it is very easy for us to implement sending mailboxes in. Net.

Related Article

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.