撰寫 Gmail 寄信程式
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);
*/
}
}
陳鍾誠 (2010年06月15日),(網頁標題) 以 C# 撰寫 Gmail 寄信程式,(網站標題) 免費電子書:C# 程式設計,2010年06月15日,取自 http://cs0.wikidot.com/gmail ,網頁修改第 1 版。