欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#發(fā)送郵箱實現(xiàn)代碼

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

之前自己從來沒有做過發(fā)送郵箱的功能,前段時間項目需要,在找了很多帖子之后,終于實現(xiàn)了。

之后有整理了一下,寫了一個類。直接給類傳遞信息,就可以發(fā)送了。

這里還需要說明的是,發(fā)送郵箱需要開通POP3/SMTP服務(wù),否則QQ郵箱,網(wǎng)易郵箱等會報錯。接收的郵箱就不用開通啦,開通方法百度一下就知道啦。

public static class EmailHelper
  {
    /// <summary>
    /// 發(fā)送郵件
    /// </summary>
    /// <param name="subject">郵件主題</param>
    /// <param name="msg">郵件內(nèi)容</param>
    /// <param name="filePath">附件地址,如果不添加附件傳null或""</param>
    /// <param name="senderEmail">發(fā)送人郵箱地址</param>
    /// <param name="senderPwd">發(fā)送人郵箱密碼</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('@');

        //構(gòu)造一個Email的Message對象
        MailMessage message = new MailMessage();

        //確定smtp服務(wù)器地址。實例化一個Smtp客戶端
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp." + sendFromUser[1]);

        //構(gòu)造發(fā)件人地址對象
        message.From = new MailAddress(senderEmail, sendFromUser[0], Encoding.UTF8);

        //構(gòu)造收件人地址對象
        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);
        }

        //添加郵件主題和內(nèi)容
        message.Subject = subject;
        message.SubjectEncoding = Encoding.UTF8;
        message.Body = msg;
        message.BodyEncoding = Encoding.UTF8;

        //設(shè)置郵件的信息
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.IsBodyHtml = false;

        //如果服務(wù)器支持安全連接,則將安全連接設(shè)為true。
        //gmail,qq支持,163不支持
        switch (sendFromUser[1])
        {
          case "gmail.com":
          case "qq.com":
            client.EnableSsl = true;
            break;
          default:
            client.EnableSsl = false;
            break;
        }

        //設(shè)置用戶名和密碼。
        client.UseDefaultCredentials = false;
        //用戶登陸信息
        NetworkCredential myCredentials = new NetworkCredential(senderEmail, senderPwd);
        client.Credentials = myCredentials;
        //發(fā)送郵件
        client.Send(message);
      }
      catch (Exception ex)
      {
        throw (ex);
      }
    }

    /// <summary>
    /// 驗證所有傳入字符串不能為空或null
    /// </summary>
    /// <param name="ps">參數(shù)列表</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;
    }
  }

直接調(diào)用方法,傳遞需要發(fā)送的信息,就可以發(fā)送郵箱了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:如何使用C#程序給PDF文件添加編輯域

欄    目:C#教程

下一篇:C#中DataTable導(dǎo)出為HTML格式的方法

本文標(biāo)題:C#發(fā)送郵箱實現(xiàn)代碼

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6034.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有