C # send emails, including console programs, WPF, WebForm, and ASP. NET MVC

Source: Internet
Author: User
Tags mailmessage smtpclient

I have always wanted to master the mail sending function, and I 've been dragging it all over the past two days. I 've finally looked at it and sorted it out. I hope it can help me learn it.

The SMTP server can be used to send emails. One is to use the SMTP function of IIS, and the other is to directly use the SMTP function of the mail supplier, such as Gmail, Sina, and QQ, to use these SMTP servers, you must register an account. Generally, you can directly use your mailbox and password. However, some mailboxes must enable the POP3/SMTP service. For example, QQ mail is disabled by default, you can find it in "Settings"> "account. I used the second method for sorting out today.

Early. the NET version uses System. web. the Mail class provides the function to send emails; version 2.0 introduces the System. net. mail class to replace System. web. mail, but I use System in WebForm. net. mail always triggers an exception, and then uses System. web. mail is successful. You can see the differences in my code; ASP. net mvc 3 provides the WebMail class for sending mails, making it easier.

Console programs, WPF, WebForm, and ASP. net mvc I tested it. The console program and WPF both use System. net. mail, WebForm, and ASP. net mvc can all use System. web. mail, while ASP. net mvc 3 is more convenient to use WebMail directly. Next I will post the Code separately.

Console program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail; // Add reference

namespace SendEmail
    class Program
        static void Main (string [] args)
            Program p = new Program ();
            bool flag = p.SendEmail ();
            if (flag)
                Console.Write ("Success!");
            Console.Read ();

        public bool SendEmail ()
            MailMessage msg = new MailMessage ();
            msg.To.Add ("1234567@qq.com"); // Mail recipient's account
            msg.From = new MailAddress ("1234567@qq.com", "nickname", System.Text.Encoding.UTF8); // The account number and display name and character encoding for sending mail
            msg.Subject = "Subject"; // E-mail subject
            msg.SubjectEncoding = System.Text.Encoding.UTF8; // Email header encoding
            msg.Body = "Content"; // Email content
            msg.BodyEncoding = System.Text.Encoding.UTF8; // Email content encoding
            msg.IsBodyHtml = false; // Whether it is HTML mail
            msg.Priority = MailPriority.High; // Mail priority

            SmtpClient client = new SmtpClient ();
            client.Credentials = new System.Net.NetworkCredential ("1234567@qq.com", "password"); // The registered mailbox and password, as far as the QQ mailbox is concerned, if an independent password is set, the independent password should be used instead
            client.Host = "smtp.qq.com"; // SMTP server corresponding to QQ mailbox
            object userState = msg;
                client.SendAsync (msg, userState);
                return true;
            catch (Exception ex)
                Console.WriteLine (ex.Message);
                return false;


           Button_Click(               flag =                               MessageBox.Show(,   
                        MailMessage message =              message.To.Add(             message.From =  MailAddress(,             message.Subject =              message.SubjectEncoding =             message.Body =              message.BodyEncoding =             message.IsBodyHtml =              message.Priority =             Attachment att =  Attachment();
             SmtpClient smtp =              smtp.Credentials =  System.Net.NetworkCredential(,             smtp.Host =               userState = 
                                                    MessageBox.Show(ex.Message,                      }


           Button1_Click(               flag =                               Response.Write(   
                        MailMessage message =              message.To =              message.From =              message.Subject =              message.Body =              
             SmtpMail.SmtpServer =              

ASP. net mvc:

= = ;
            WebMail.EnableSsl = ;
            WebMail.UserName = = = ,,“Content




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.