.net郵件發送程式

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Text;
//添加命名空間
using System.Net.Mail;
using System.Net;

namespace Walter.K.Wang
{
    /// <summary>
    /// 寄送電子郵件類
    /// </summary>
    public class Mail
    {
        /// <summary>
        /// 寄送電子郵件函數
        /// </summary>
        /// <param name="txtHost">電子郵件服務主機名稱</param>
        /// <param name="txtFrom">發送人地誌</param>
        /// <param name="txtPass">發信人密碼</param>
        /// <param name="txtTo">收信人地址</param>
        /// <param name="txtSubject">郵件標題</param>
        /// <param name="txtBody">郵件內容</param>
        /// <param name="isBodyHtml">是否採用HTML編碼</param>
        /// <param name="priority">電子郵件的優先順序別</param>
        /// <param name="encoding">內容採用的編碼方式</param>
        /// <param name="files">附件</param>
        /// <returns>操作結果</returns>
        public static string SendMail(string txtHost, string txtFrom, string txtPass, string txtTo, string txtSubject, string txtBody, bool isBodyHtml, MailPriority priority, System.Text.Encoding encoding,string[] files)
        {
            //電子郵件附件
            Attachment data = null;
            //傳送的電子郵件類
            MailMessage message = new MailMessage(txtFrom, txtTo);
            //設定標題
            message.Subject = txtSubject;
            //設定內容
            message.Body = txtBody;
            //是否採用HTML編碼
            message.IsBodyHtml = isBodyHtml;
            //電子郵件的優先順序別
            message.Priority = priority;
            //內容採用的編碼方式
            message.BodyEncoding = encoding;
            try
            {
                //添加附件
                if (files.Length > 0 && files != null)
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        //執行個體話電子郵件附件,並設定類型
                        data = new Attachment(files[i], System.Net.Mime.MediaTypeNames.Application.Octet );
                        //執行個體郵件內容
                        System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
                        //取得建檔日期
                        disposition.CreationDate = System.IO.File.GetCreationTime(files[i]);
                        //取得附件修改日期
                        disposition.ModificationDate = System.IO.File.GetLastWriteTime(files[i]);
                        //取得讀取日期
                        disposition.ReadDate = System.IO.File.GetLastAccessTime(files[i]);
                        //設定檔案名稱
                        System.IO.FileInfo fi = new System.IO.FileInfo(files[i]);
                        disposition.FileName = fi.Name.ToString();
                        //添加附件
                        message.Attachments.Add(data);
                    }
                }
                //執行個體從送電子郵件類
                SmtpClient client = new SmtpClient();
                //設定電子郵件主機名稱
                client.Host = txtHost;
                //取得寄信人認證
                client.Credentials = new NetworkCredential(txtFrom, txtPass);
                //寄送電子郵件
                client.Send(message);
                return "郵件發送成功";
            }
            catch (Exception Err)
            {
                //返回錯誤資訊
                return Err.Message;
            }
            finally
            {
                //銷毀電子郵件附件
                if (data != null)
                {
                    data.Dispose();
                }
                //銷毀傳送的電子郵件執行個體
                message.Dispose();
            }
        }
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.