您的位置:首页 > 编程语言 > C#

C#使用gmail发送邮件

2014-05-17 13:17 459 查看
最近在做邮箱验证的功能,找了好多代码,还是不能尽如人意,但是,经过一番查找还是有收获的,所以也就有了下面的代码

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;
                //收件人
                mm.To.Add("674026565@qq.com");
                //邮箱标题
                mm.Subject = "Hello Dear:";
                mm.IsBodyHtml = true;
                //邮件内容
                mm.Body = "你好Mr流星!";
                //内容的编码格式
                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";//你的邮箱地址
                nc.Password = "Password";//你的邮箱密码,这里的密码是xxxxx@qq.com邮箱的密码,特别说明下~
                sc.UseDefaultCredentials = true;
                sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                sc.Credentials = nc;
                sc.EnableSsl = true;
                sc.Port = 587;
                //如果这里报mail from address must be same as authorization user这个错误,是你的QQ邮箱没有开启SMTP,

                sc.Host = "smtp.gmail.com";
                sc.Send(mm);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message + "   " + ex.Source);
            }
        }
       
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: