vs2008 C# 发邮件

以前做过发邮件的,不过是在rails中做的,今天要在vs中,找了一下,下面的可以用:


using System.Net.Mail;




//使用新浪邮箱发送

public static bool sina(MailAddress Messagefrom, string MessageTo, string MessageSubject, string MessageBody)
        {
            MailMessage message = new MailMessage();
            message.From = Messagefrom;
            message.To.Add(MessageTo);              //收件人邮箱地址可以是多个以实现群发
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.IsBodyHtml = true;              //是否为html格式
            message.Priority = MailPriority.High;  //发送邮件的优先等级
            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.sina.com";              //指定发送邮件的服务器地址或IP
            sc.Port = 25;                          //指定发送邮件端口
            //sc.UseDefaultCredentials = true;
            //sc.EnableSsl = true;
            sc.Credentials = new System.Net.NetworkCredential(“[email protected]”, "密码"); //指定登录服务器的用户名和密码
            try
            {
                sc.Send(message);      //发送邮件
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }


//使用Gmail邮箱发送

        public static bool gmail(MailAddress Messagefrom, string MessageTo, string MessageSubject, string MessageBody)
        {
            MailMessage message = new MailMessage();
            message.From = Messagefrom;
            message.To.Add(MessageTo);              //收件人邮箱地址可以是多个以实现群发
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.IsBodyHtml = true;              //是否为html格式
            message.Priority = MailPriority.High;  //发送邮件的优先等级
            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.gmail.com";              //指定发送邮件的服务器地址或IP
            sc.Port = 587;                          //指定发送邮件端口
            sc.UseDefaultCredentials = true;
            sc.EnableSsl = true;
            sc.Credentials = new System.Net.NetworkCredential(“[email protected]”, "密码"); //指定登录服务器的用户名和密码
            try
            {
                sc.Send(message);      //发送邮件
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

//使用QQ邮箱发送
        public static bool qqemail(MailAddress Messagefrom, string MessageTo, string MessageSubject, string MessageBody)
        {
            MailMessage message = new MailMessage();
            message.From = Messagefrom;
            message.To.Add(MessageTo);              //收件人邮箱地址可以是多个以实现群发
            message.Subject = MessageSubject;

            message.Body = MessageBody;
            message.IsBodyHtml = true;              //是否为html格式
            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.qq.com";                //指定发送邮件的服务器地址或IP
            sc.Port = 25;                           //指定发送邮件端口
            sc.Credentials = new System.Net.NetworkCredential(“[email protected]”, "密码"); //指定登录服务器的用户名和密码
            try
            {
                sc.Send(message);                   //发送邮件
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }





//调用方式

protected void Button1_Click(object sender, EventArgs e)
        {
            //发件邮箱
            //发件人名称
            //编码方式
            MailAddress Messagefrom = new MailAddress(“[email protected]”,"白屋梁",System.Text.Encoding.UTF8);  //发件人邮箱地址
          
            string MessageTo = TextBox3.Text;             //收件人邮箱地址
            string MessageSubject = TextBox1.Text;        //邮件主题
            string MessageBody = TextBox2.Text;           //邮件内容
            if (strcl.gmail(Messagefrom, MessageTo, MessageSubject, MessageBody))
            {
                UImsg.show(Page, "发送成功");
            }
            else
            {
                UImsg.show(Page, "发送邮件失败");
            }
        }

你可能感兴趣的:(C++,c,qq,C#,Gmail)