C#调用QQ_Mail发送邮件实例代码两例

代码案例一:

private void button1_Click(object sender, EventArgs e)
        {
            string myMaillAdress = "";
            string myMaillPassword = "";
            string myMaillMessage = "";
            string toMaillAdress = "";

            QQEmail qq_mial = new QQEmail();
            bool send_status = qq_mial.IsSendEmail(myMaillAdress, toMaillAdress, myMaillMessage, myMaillPassword);
        }
        class QQEmail
        {
            public bool IsSendEmail(string FromAddress, string ToAddress, string Message, string Pwd)
            {
                MailAddress Address = new MailAddress(FromAddress);
                MailMessage mail = new MailMessage();
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.Subject = "这是" + FromAddress + "发送的一段信息:";
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.Body = Message;
                mail.IsBodyHtml = true;

                mail.Priority = System.Net.Mail.MailPriority.Low;
                mail.To.Add(ToAddress);

                mail.From = Address;

                SmtpClient smtp = new SmtpClient("smtp.qq.com", 25);//smtp支持的服务器是smtp.qq.com,服务器端口是25,587也行

                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(FromAddress, Pwd);//切记这两个数据一定要填对

                try
                {
                    smtp.Send(mail);
                    return true;
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                    return false;
                }
            }
        }

代码案例二:

using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace BLL
{
    public class emailHandle
    {
        private string _serviceType = "SMTP";
        private string _host;

        /// <summary>
        /// 发送者邮箱
        /// </summary>
        public string From { get; set; }

        /// <summary>
        /// 接收者邮箱列表
        /// </summary>
        public List<string> To { get; set; }

        /// <summary>
        /// 抄送者邮箱列表
        /// </summary>
        public string[] Cc { get; set; }

        /// <summary>
        /// 秘抄者邮箱列表
        /// </summary>
        public string[] Bcc { get; set; }

        /// <summary>
        /// 邮件主题
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 邮件内容
        /// </summary>
        public string Body { get; set; }

        /// <summary>
        /// 是否是HTML格式
        /// </summary>
        public bool IsBodyHtml { get; set; }

        /// <summary>
        /// 附件列表
        /// </summary>
        public string[] Attachments { get; set; }

        /// <summary>
        /// 邮箱服务类型(Pop3,SMTP,IMAP,MAIL等),默认为SMTP
        /// </summary>
        public string ServiceType
        {
            get { return _serviceType; }
            set { _serviceType = value; }
        }

        /// <summary>
        /// 邮箱服务器,如果没有定义邮箱服务器,则根据serviceType和Sender组成邮箱服务器
        /// </summary>
        public string Host
        {
            get { return _host; }
            set { _host = value; }
        }

        /// <summary>
        /// 邮箱账号(默认为发送者邮箱的账号)
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 邮箱密码(默认为发送者邮箱的密码),默认格式GB2312
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 邮箱优先级
        /// </summary>
        public MailPriority MailPriority { get; set; }

        /// <summary>
        ///  邮件正文编码格式
        /// </summary>
        public Encoding Encoding { get; set; }

        /// <summary>
        /// 构造参数,发送邮件,使用方法备注:公开方法中调用
        /// </summary>
        public int Send()
        {
            var mailMessage = new MailMessage();

            //读取To  接收者邮箱列表
            try
            {
                if (this.To != null && this.To.Count > 0)
                {
                    foreach (string to in this.To)
                    {
                        if (string.IsNullOrEmpty(to)) continue;
                        mailMessage.To.Add(new MailAddress(to.Trim()));
                    }
                }
                //读取Cc  抄送者邮件地址
                if (this.Cc != null && this.Cc.Length > 0)
                {
                    foreach (var cc in this.Cc)
                    {
                        if (string.IsNullOrEmpty(cc)) continue;
                        mailMessage.CC.Add(new MailAddress(cc.Trim()));
                    }
                }
                //读取Attachments 邮件附件
                if (this.Attachments != null && this.Attachments.Length > 0)
                {
                    foreach (var attachment in this.Attachments)
                    {
                        if (string.IsNullOrEmpty(attachment)) continue;
                        mailMessage.Attachments.Add(new Attachment(attachment));
                    }
                }
                //读取Bcc 秘抄人地址
                if (this.Bcc != null && this.Bcc.Length > 0)
                {
                    foreach (var bcc in this.Bcc)
                    {
                        if (string.IsNullOrEmpty(bcc)) continue;
                        mailMessage.Bcc.Add(new MailAddress(bcc.Trim()));
                    }
                }
                //读取From 发送人地址
                mailMessage.From = new MailAddress(this.From);

                //邮件标题
                Encoding encoding = Encoding.GetEncoding("GB2312");
                mailMessage.Subject = this.Subject;
                //邮件正文是否为HTML格式
                mailMessage.IsBodyHtml = this.IsBodyHtml;
                //邮件正文
                mailMessage.Body = this.Body;
                mailMessage.BodyEncoding = this.Encoding;
                //邮件优先级
                mailMessage.Priority = this.MailPriority;

                //发送邮件代码实现
                var smtpClient = new SmtpClient
                {
                    Host = this.Host,
                    EnableSsl = true,
                    Credentials = new NetworkCredential(this.UserName, this.Password)
                };
//加这段之前用公司邮箱发送报错:根据验证过程,远程证书无效
//加上后解决问题
                ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
                //认证
                smtpClient.Send(mailMessage);
                return 1;
            }
            catch (Exception ex)
            {
                var loger = LogManager.GetLogger(typeof(emailHandle));
                loger.Info(string.Format("发送邮件异常,收信邮箱:{0}", this.To[0]), ex);
                return -1;
            }
        }
    }
}

到此这篇关于C#调用QQ_Mail发送邮件的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#实现QQ邮箱发送邮件

    闲着蛋疼.计划着改善公司的邮件服务.怎料公司网络封闭的太厉害了.我只能在家里利用开放点的网络来测试发送邮件: 利用qq邮箱发送到公司的企业邮箱上: 前提准备,登陆qq邮箱开启stmp服务.不开启的话没法通过代码登陆到你的邮箱: 查询腾讯qq邮箱的smtp主机地址为:smtp.qq.com  端口是587,或者465 using System; using System.Collections.Generic; using System.Linq; using System.Text; using

  • C#实现发送邮件的方法

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 #region 发送邮件部分 private static String fromMail = "1111@126.com"; ///邮箱名称 private static String mailPwd = "111111"; ///密码 private static string toMail = "2222@163.com"; ///邮箱服务器 privat

  • c# 实现发送邮件到指定邮箱

    很多小伙伴对于[程序发送邮件]不明觉厉的同时又羡慕嫉妒恨,其实发送邮件是一个很常用的功能, 我们这里就简单做一个发送邮箱的案例. PS:案例使用qq邮箱,当然,也可以使用其他邮箱,只要替换邮箱关键字即可,下面案例已做标注. 首先,我们需要一个[当前发件授权码],这个码需要登录发件箱里面进行开启,我们先开启一下. 1.登录发件箱的邮箱,进入[设置] 2.点击[账户] 3.下滑找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,点击开启,根据提示操作完成后得到一个授

  • c# 两种发送邮件的方法

    目录 一.两种发送邮件的方法 二.遇到的问题 三.示例 System.Web.Mail System.Net.Mail 一.两种发送邮件的方法 有用到两种方式发邮件,一种是用System.Web.Mail类,另一种是System.Net.Mail类. System.Net.Mail是作为System.Web.Mail的替代存在的. System.Web.Mail使用时会提示已过时,但目前任然可以正常使用. 二.遇到的问题 我在使用System.Net.Mail发邮件的时候遇到一个问题,如果是用的

  • C#实现的自定义邮件发送类完整实例(支持多人多附件)

    本文实例讲述了C#实现的自定义邮件发送类.分享给大家供大家参考,具体如下: using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Mail; using System.Net.Mime; namespace ConsoleApplication1 { /// <summary> /// 发送邮件类 的摘要说明 /// </summary&g

  • C# SendMail发送邮件功能实现

    最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法 1.利用新浪邮箱发送 2.利用公司邮箱发送 3.利用CDO发送,这种方式要引用Interop.ADODB.dll(http://www.nodevice.com/dll/Interop_ADODB_dll/item20357.html)和Interop.CDO.dll()两个文件 具体代码如下: using System; using System.Collections.Generic; using System.Linq; usin

  • c#实现服务器性能监控并发送邮件保存日志

    客户端代码 复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.ServiceProcess;using System.Text;using System.Threading;using System.Management;using System.Configurat

  • c#调用qq邮箱smtp发送邮件修改版代码分享

    复制代码 代码如下: try            {                MailMessage mm = new MailMessage();                MailAddress Fromma = new MailAddress("xxxx@qq.com");                MailAddress Toma = new MailAddress("MMMMMMM@qq.com", null);              

  • C#编程实现发送邮件的方法(可添加附件)

    本文实例讲述了C#编程实现发送邮件的方法.分享给大家供大家参考,具体如下: using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Mail; namespace WindowsFo

  • c# 实现发送邮件的功能

    微软已经为我们准备好了现成的工具类供我们调用: MailMessage //邮件信息类 SmtpClient //邮件发送类 首先需要在项目的类文件中引用以下命名空间: using System.Net; using System.Net.Mail; 然后直接上封装好的代码: /// <summary> /// 发送邮件方法 /// </summary> /// <param name="mailTo">接收人邮件</param> ///

  • 通过C#实现发送自定义的html格式邮件

    要发送HTML格式邮件,需要设置MailMessage对象的IsBodyHtml属性,设置为true. 类MailMessage在命名空间System.Net.Mail下. using System.Net.Mail; 发送HTML格式的邮件在HoverTreeTop项目中已经实现,并发送成功. 需依赖于HoverTreeFrame项目的HoverTreeEmail类. 方法为: 复制代码 代码如下: public static string HoverTreeSendEmail(string

  • C#实现发送邮件的三种方法

    本文实例讲述了C#实现发送邮件的三种方法.分享给大家供大家参考.具体方法分析如下: 一.问题: 最近公司由于一个R&I项目的需要,用户要求在购买产品或出货等一些环节,需要发送邮件提醒或者说每周一让系统自动采集数据发送一封E-mail,因此我也就找来相关资料,写了一个Demo分享给大家,大家共同学习学习. 二.实现代码: 通过.Net FrameWork 2.0下提供的"System.Net.Mail"可以轻松的实现,本文列举了3种途径来发送: 1.通过Localhost: 2.

  • c#利用system.net发送html格式邮件

    复制代码 代码如下: using System;using System.Text;using System.Net;using System.Net.Mail;using System.Net.Mime; namespace LeeStudio.Basic{/// <summary>/// 邮件发送类/// </summary>public class SendEmail{private MailMessage mailMessage = new MailMessage(); /

  • C# 服务器发送邮件失败实例分析

    错误展示: 我在本地是可以发送的但部署到服务器上后就不能发送了.SMTP服务是开了的. 报错: "{"success":false,"message":"错误System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'smtp.163.com' 分析: 邮件

随机推荐