C# EmailHelper.cs 郵件發送模組代碼

來源:互聯網
上載者:User

  一個C#發郵件模組,EmailHelper.cs 代碼,是從一個C#工具包中拷貝出來的,在ASP.NET或C#項目中添加發送郵件功能時,這個類挺方便,使用時指定郵件標題、郵件內文、收件者、是否為HTML格式等參數即可,代碼如下:

  01using System;

  02using System.Collections.Generic;

  03using System.Linq;

  04using System.Text;

  05namespace CLB.Utility.Tools

  06{

  07 public class EmailHelper

  08 {

  09 ///<summary>

  10 /// 發送郵件

  11 ///<summary>

  12 ///<param name="subject"> 郵件標題</param>

  13 /// <param name="body">郵件內文</param>

  14 /// <param name="to">收件者</param>

  15 /// <param name="Ishtml">是否為html格式</param>

  16 public static bool sendmail(string subject, string body, string to, bool Ishtml)

  17 {

  18 using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage())

  19 {

  20 msg.To.Add(to);

  21 msg.From = new System.Net.Mail.MailAddress("郵箱地址", "標題", System.Text.Encoding.UTF8);

  22 msg.Subject = subject;//郵件標題

  23 msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件標題編碼

  24 msg.Body = body;//郵件內容

  25 msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼

  26 msg.IsBodyHtml = Ishtml;//是否是HTML郵件

  27 msg.Priority = System.Net.Mail.MailPriority.High;//郵件優先順序

  28 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

  29 client.Credentials = new System.Net.NetworkCredential("郵箱地址", "密碼");

  30 client.Host = "網域名稱";

  31 object userState = msg;

  32 try

  33 {

  34 client.Send(msg);

  35 return true;

  36 }

  37 catch

  38 {

  39 return false;

  40 }

  41 }

  42 }

  43 }

  44}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.