C # uses Gmail to send mail

Source: Internet
Author: User
Tags mailmessage smtpclient

Recently in doing a mailbox verification function, find a lot of code, or not satisfactory, but, after a search or have harvested, so there is the following code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Net.Mail;
Using System.Web.Mail;
Using System.Net;
Using System.ComponentModel;
            Namespace MailSender {class Program {static void Main (string[] args) {try
                {MailMessage mm = new MailMessage ();
                MailAddress Fromma = new MailAddress ("zjyOxcj@gmail.com");
                MailAddress Toma = new MailAddress ("674026565@qq.com", null); Mm.
                from = Fromma; Recipient mm.
                To.add ("674026565@qq.com"); Mailbox title mm.
                Subject = "Hello Dear:"; Mm.
                Isbodyhtml = true; Message content mm. BODY = "Hello, Mr Meteor."
                "; The encoding format for the content mm.
                bodyencoding = System.Text.Encoding.UTF8; Mm.
                ReplyTo = Toma; Mm.
                Sender =fromma; Mm.
                Isbodyhtml = false;Mm.
                Deliverynotificationoptions = deliverynotificationoptions.onsuccess; Mm. Cc.
                ADD (Toma);
                SmtpClient sc = new SmtpClient ();
                NetworkCredential NC = new NetworkCredential (); nc. UserName = "zjyOxcj@gmail.com";//Your email address NC. Password = "Password";//Your email password, here the password is xxxxx@qq.com mailbox password, special instructions under ~ SC.
                useDefaultCredentials = true; Sc.
                Deliverymethod = Smtpdeliverymethod.network; Sc.
                Credentials = NC; Sc.
                Enablessl = true; Sc.
                Port = 587; If you report mail from address must is same as authorization user this error, is your QQ mailbox does not open SMTP, SC.
                Host = "smtp.gmail.com"; Sc.
            Send (mm); The catch (Exception ex) {Console.Write (ex). Message + "" + Ex.
            Source); }
        }
       
    }
}


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.