当前位置: 移动技术网 > IT编程>开发语言>c# > C#发送邮箱实现代码

C#发送邮箱实现代码

2019年07月18日  | 移动技术网IT编程  | 我要评论

之前自己从来没有做过发送邮箱的功能,前段时间项目需要,在找了很多帖子之后,终于实现了。

之后有整理了一下,写了一个类。直接给类传递信息,就可以发送了。

这里还需要说明的是,发送邮箱需要开通pop3/smtp服务,否则qq邮箱,网易邮箱等会报错。接收的邮箱就不用开通啦,开通方法百度一下就知道啦。

public static class emailhelper
  {
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="subject">邮件主题</param>
    /// <param name="msg">邮件内容</param>
    /// <param name="filepath">附件地址,如果不添加附件传null或""</param>
    /// <param name="senderemail">发送人邮箱地址</param>
    /// <param name="senderpwd">发送人邮箱密码</param>
    /// <param name="recipientemail">接收人邮箱</param>
    public static void sendmail(string subject, string msg, string filepath, string senderemail, string senderpwd, params string[] recipientemail)
    {
      if (!checkisnotemptyornull(subject, msg, senderemail, senderpwd) || recipientemail == null || recipientemail.length == 0)
      {
        throw new exception("输入信息无效");
      }
      try
      {
        string[] sendfromuser = senderemail.split('@');

        //构造一个email的message对象
        mailmessage message = new mailmessage();

        //确定smtp服务器地址。实例化一个smtp客户端
        system.net.mail.smtpclient client = new system.net.mail.smtpclient("smtp." + sendfromuser[1]);

        //构造发件人地址对象
        message.from = new mailaddress(senderemail, sendfromuser[0], encoding.utf8);

        //构造收件人地址对象
        foreach (string username in recipientemail)
        {
          message.to.add(new mailaddress(username, username.split('@')[0], encoding.utf8));
        }

        if (!string.isnullorempty(filepath))
        {
          attachment attach = new attachment(filepath);
          //得到文件的信息
          contentdisposition disposition = attach.contentdisposition;
          disposition.creationdate = system.io.file.getcreationtime(filepath);
          disposition.modificationdate = system.io.file.getlastwritetime(filepath);
          disposition.readdate = system.io.file.getlastaccesstime(filepath);
          //向邮件添加附件
          message.attachments.add(attach);
        }

        //添加邮件主题和内容
        message.subject = subject;
        message.subjectencoding = encoding.utf8;
        message.body = msg;
        message.bodyencoding = encoding.utf8;

        //设置邮件的信息
        client.deliverymethod = smtpdeliverymethod.network;
        message.bodyencoding = system.text.encoding.utf8;
        message.isbodyhtml = false;

        //如果服务器支持安全连接,则将安全连接设为true。
        //gmail,qq支持,163不支持
        switch (sendfromuser[1])
        {
          case "gmail.com":
          case "qq.com":
            client.enablessl = true;
            break;
          default:
            client.enablessl = false;
            break;
        }

        //设置用户名和密码。
        client.usedefaultcredentials = false;
        //用户登陆信息
        networkcredential mycredentials = new networkcredential(senderemail, senderpwd);
        client.credentials = mycredentials;
        //发送邮件
        client.send(message);
      }
      catch (exception ex)
      {
        throw (ex);
      }
    }

    /// <summary>
    /// 验证所有传入字符串不能为空或null
    /// </summary>
    /// <param name="ps">参数列表</param>
    /// <returns>都不为空或null返回true,否则返回false</returns>
    public static bool checkisnotemptyornull(params string[] ps)
    {
      if (ps != null)
      {
        foreach (string item in ps)
        {
          if (string.isnullorempty(item)) return false;
        }
        return true;
      }
      return false;
    }
  }

直接调用方法,传递需要发送的信息,就可以发送邮箱了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网