C# 发送邮件的三种组件

csharp

浏览数:202

2019-1-8

AD:资源代下载服务

片段 1片段 2片段 3片段 4


相关基础参数信息

//发件服务器账号密码(启用了授权码密码则为授权码)
//中间如果切换过授权码,最好修改密码为邮箱实际密码,发送邮件后会提示使用授权码,重新生成授权码,更换为新的授权码即可
//全局
string serverName = "test@qq.com";
string serverPwd = "testpwd";

//发送成功提示
private void Smtp_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
    MessageBox.Show("邮件已发送成功");
}

//点击发送按钮
private void btnSend_Click(object sender, EventArgs e)
{
    if (txtServerName.Text.Trim() == "")
    {
        MessageBox.Show("请输入发送人邮箱地址");
        return;
    }

    if (txtReceiveName.Text.Trim() == "")
    {
        MessageBox.Show("请输入收件人邮箱地址");
        return;
    }

    if (txtMailContent.Text == "")
    {
        MessageBox.Show("请输入邮件内容");
        return;
    }

    //处理收件人
    List<string> receivelist = new List<string>();
    string[] templist = txtReceiveName.Text.Split('|');
    foreach (string name in templist)
    {
        if (name.Trim() != "")
        {
            receivelist.Add(name);
        }
    }

    //处理附件
    List<string> filelist = new List<string>();
    foreach (string filepath in listboxFile.Items)
    {
        if (filepath.Trim() != "")
        {
            filelist.Add(filepath);
        }
    }

    //是否加密发送
    if (!chkIsEncrypt.Checked)
    {
        SendMailBySmtp(receivelist, filelist);
    }
    else
    {
        /*
        System.Net.Mail支持Explicit SSL
        SSL加密分为两种:Explicit SSL(显示加密)和Implicit SSL(隐式加密)
        大部分邮箱支持Explicit SSL,而QQ邮箱支持Implicit SSL
        所以才有了这三种邮件发送方式
        */

        //常规SSL加密发送
        SendMailByExplicitSSLSmtp(receivelist, filelist);
        //System.Web.Mail.MailMessage参数方式设置加密发送
        SendMailByImplicitSSLSmtp_Web(receivelist, filelist);
        //CDO.Message方式加密发送
        //SendMailByImplicitSSLSmtp_CDO(receivelist, filelist);
    }
}


System.Net.Mail组件发送

void SendMailByExplicitSSLSmtp(List<string> receivelist, List<string> filelist)
{
    //发件服务器身份凭证
    NetworkCredential network = new NetworkCredential();
    network.UserName = serverName;
    network.Password = serverPwd;

    //发件人邮箱地址(发件人地址必须与发件服务器账号地址一致)
    MailAddress sendermailaddress = new MailAddress(serverName, "发件人", Encoding.UTF8);
    //发信人邮箱地址(发信人地址不必与发件服务器地址一致,那么发件人为邮件的代发人(需要注意,不能为收件人地址,容易被识别为垃圾邮件))
    MailAddress frommailaddress = new MailAddress(txtServerName.Text.Trim() + "@" + comboxMailType.SelectedItem.ToString(), "发信人", Encoding.UTF8);

    //邮件传输协议
    SmtpClient smtp = new SmtpClient();
    //smtp方式发送
    smtp.Credentials = network;
    smtp.EnableSsl = true;
    smtp.Host = "smtp.qq.com";
    smtp.Port = 587; //465
    //发送成功后执行操作
    smtp.SendCompleted += Smtp_SendCompleted;

    //邮件信息
    MailMessage mailmsg = new MailMessage();
    mailmsg.Priority = MailPriority.Normal;
    mailmsg.Subject = txtMailHeader.Text.Trim();
    mailmsg.Body = txtMailContent.Text;

    //发件人地址必须与发件服务器地址一致
    mailmsg.Sender = sendermailaddress;
    //发信人地址可以不同,然后邮件的代发人为发件人(需要注意,不能为收件人地址,容易被识别为垃圾邮件)
    mailmsg.From = frommailaddress;
    //邮件接收人
    foreach (string receivename in receivelist)
    {
        mailmsg.To.Add(receivename);
    }
    //邮件抄送()
    //mailmsg.CC.Add("");
    //密件抄送
    //mailmsg.Bcc.Add("")

    //附件集合
    foreach (string filepath in filelist)
    {
        Attachment att = new Attachment(filepath);
        mailmsg.Attachments.Add(att);
    }

    smtp.SendMailAsync(mailmsg);
}


System.Web.Mail组件发送

void SendMailByImplicitSSLSmtp_Web(List<string> receivelist, List<string> filelist)
{
    System.Web.Mail.MailMessage mailmsg = new System.Web.Mail.MailMessage();
    mailmsg.Priority = System.Web.Mail.MailPriority.Normal;
    mailmsg.Subject = txtMailHeader.Text.Trim();
    mailmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
    mailmsg.Body = txtMailContent.Text;

    mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证  
    mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", serverName); //邮箱登录账号,这里跟前面的发送账号一样就行  
    mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", serverPwd); //这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码  
    mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口(587)  
    mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密  

    //发件人邮箱地址(发件人地址必须与发件服务器账号地址一致)
    mailmsg.From = serverName;
    //抄送副本邮件地址
    //mailmsg.Cc = "";
    //匿名副本邮件地址
    //mailmsg.Bcc = "";

    //附件集合
    foreach (string filepath in filelist)
    {
        Attachment att = new Attachment(filepath);
        mailmsg.Attachments.Add(att);
    }

    System.Web.Mail.SmtpMail.SmtpServer = "smtp.qq.com";

    /*
    经测试System.Web.Mail.MailMessage方式发送的邮件接收人不能是发件人地址,否则会提示“与服务器的传输连接失败”
    */
    //邮件接收人
    try
    {
        foreach (string receivename in receivelist)
        {
            mailmsg.To = receivename;
            System.Web.Mail.SmtpMail.Send(mailmsg);
        }
        MessageBox.Show("邮件已发送成功");
    }
    catch (Exception ex)
    {
        MessageBox.Show("邮件发送失败");
    }
}


CDO.Message组件发送

void SendMailByImplicitSSLSmtp_CDO(List<string> receivelist, List<string> filelist)
{
    //引入的COM组件
    //Microsoft ActiveX Data Objects 6.1 Library(版本可能不同)
    //Microsoft CDO for Windows 2000 Library

    CDO.Message oMsg = new CDO.Message();
    CDO.IConfiguration iConfg;
    ADODB.Fields oFields;
    iConfg = oMsg.Configuration;
    oFields = iConfg.Fields;
    Configuration conf = new ConfigurationClass();
    conf.Fields[CdoConfiguration.cdoSendUsingMethod].Value = CdoSendUsing.cdoSendUsingPort;
    conf.Fields[CdoConfiguration.cdoSMTPAuthenticate].Value = CdoProtocolsAuthentication.cdoBasic;
    conf.Fields[CdoConfiguration.cdoSMTPUseSSL].Value = true;
    conf.Fields[CdoConfiguration.cdoSMTPServer].Value = "smtp.qq.com";//必填,而且要真实可用   
    conf.Fields[CdoConfiguration.cdoSMTPServerPort].Value = 465;//465特有
    conf.Fields[CdoConfiguration.cdoSendEmailAddress].Value = "<" + serverName + ">";
    conf.Fields[CdoConfiguration.cdoSendUserName].Value = serverName;//真实的邮件地址   
    conf.Fields[CdoConfiguration.cdoSendPassword].Value = serverPwd;   //为邮箱密码,必须真实       conf.Fields.Update();

    oMsg.Configuration = conf;
    oMsg.HTMLBody = txtMailContent.Text;
    //主题
    oMsg.Subject = txtMailHeader.Text.Trim();

    oMsg.From = serverName;
    //邮件抄送()
    //mailmsg.CC.Add("");
    //密件抄送
    //mailmsg.Bcc.Add("")
    //附件集合
    foreach (string filepath in filelist)
    {
        oMsg.AddAttachment(filepath);
    }

    try
    {
        //邮件接收人
        foreach (string receivename in receivelist)
        {
            oMsg.To = receivename;
            oMsg.Send();
        }
        MessageBox.Show("邮件已发送成功");
    }
    catch (Exception ex)
    {
        MessageBox.Show("邮件发送失败");
    }}