以 C# 撰寫 Gmail 寄信程式

基礎篇

C# 簡介

開發環境

變數與運算

流程控制

陣列

函數

物件

例外處理

函式庫篇

檔案處理

資料結構

正規表達式

Thread

應用篇

視窗程式

媒體影音

網路程式

遊戲程式

手機程式

資料庫

雲端運算

特殊功能

委派

擴展方法

序列化

LinQ

WPF

網路資源

教學影片

投影片

教學文章

軟體下載

考題解答

101習題

using System;
using System.IO;
using System.Data;
using System.Configuration;

using System.Web;

using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Net.Mail;
using System.Net.Mime;

using System.Text;
/// <summary>
/// myMail 的摘要描述
/// </summary>
public class myMail
{
    public myMail()
    {
        //
        // TODO: 在此加入建構函式的程式碼
        //
    }
    public static void SendMail(string sHost, int nPort, string sUserName, string sPassword, string sFromName, string sFromEmail,
     string sToName, string sToEmail, string sHeader, string sMessage, bool fSSL)
    {

        MailMessage em = new MailMessage(new System.Net.Mail.MailAddress(sFromEmail, sFromName, Encoding.UTF8),new System.Net.Mail.MailAddress(sToEmail,sToName, Encoding.UTF8));

        em.SubjectEncoding = System.Text.Encoding.UTF8;
        em.BodyEncoding = Encoding.UTF8;
        //信件主題 
        em.Subject = sHeader;
        //內容 
        em.Body =sMessage;
        em.IsBodyHtml = true;
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        //登入帳號認證  
        client.Credentials = new System.Net.NetworkCredential(sUserName,sPassword);
        //使用587 Port 
        client.Port = nPort;
        client.Host = sHost;
        //啟動SSL 
        client.EnableSsl = fSSL;
        //寄出 
        client.Send(em);

        /*

        if (sToName.Length == 0)
            sToName = sToEmail;
        if (sFromName.Length == 0)
            sFromName = sFromEmail;

        System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
        if (fSSL)
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

        if (sUserName.Length == 0)
        {
            //Ingen auth 
        }
        else
        {
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
        }

        Mail.To = sToEmail;
        Mail.From = sFromEmail;
        Mail.Subject = sHeader;
        Mail.Body = sMessage;
        Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
        System.Web.Mail.SmtpMail.Send(Mail);
        */

    }

    public static void SendMail(string sHost, int nPort, string sUserName, string sPassword, string sFromName, string sFromEmail,
 string sToName, string sToEmail, string sHeader, string sMessage, bool fSSL, string sFile)
    {

        MailMessage em = new MailMessage(new System.Net.Mail.MailAddress(sFromEmail, sFromName, Encoding.UTF8), new System.Net.Mail.MailAddress(sToEmail, sToName, Encoding.UTF8));

        em.SubjectEncoding = System.Text.Encoding.UTF8;
        em.BodyEncoding = Encoding.UTF8;
        //信件主題 
        em.Subject = sHeader;
        //內容 
        em.Body = sMessage;
        em.IsBodyHtml = true;
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        //登入帳號認證  
        client.Credentials = new System.Net.NetworkCredential(sUserName, sPassword);
        //使用587 Port 
        client.Port = nPort;
        client.Host = sHost;
        //啟動SSL 
        client.EnableSsl = fSSL;

        Attachment data = new Attachment(sFile, MediaTypeNames.Application.Octet);
        // Add time stamp information for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(sFile);
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(sFile);
        disposition.ReadDate = System.IO.File.GetLastAccessTime(sFile);
        // Add the file attachment to this e-mail message.
        em.Attachments.Add(data);

        //寄出 
        client.Send(em);

        /*

        if (sToName.Length == 0)
            sToName = sToEmail;
        if (sFromName.Length == 0)
            sFromName = sFromEmail;

        System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

        Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
        if (fSSL)
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

        if (sUserName.Length == 0)
        {
            //Ingen auth 
        }
        else
        {
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
        }

        Mail.To = sToEmail;
        Mail.From = sFromEmail;
        Mail.Subject = sHeader;
        Mail.Body = sMessage;
        Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
        System.Web.Mail.SmtpMail.SmtpServer = sHost;
        System.Web.Mail.MailAttachment file = new System.Web.Mail.MailAttachment(sFile);
        Mail.Attachments.Add(file);

        System.Web.Mail.SmtpMail.Send(Mail);
        */

    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License