C# 透過 gmail 寄信

作品

書籍

課程

程式集

小說集

論文集

散文集

影片集

編輯雜誌

程式人

電子書

JavaScript

計算語言學

微積分

Blender 動畫

C# 語言

系統程式

高等 C 語言

Java

Android

Verilog

Wikidot

R 統計軟體

機率統計

計算機數學

組合語言

人工智慧

開放原始碼

網路資源運用

計算機結構

相關訊息

常用工具

友站連結

在家教育

RSS

最新修改

網頁列表

簡體版

English

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);
        */

    }

}

Facebook

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