使用Gmail
private void 寄送email(string 標題,string 寄件者, string[] 收信者,string 內文)
{
int i;
try
{
string men = "";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(寄件者);
for (i = 0; i < 收信者.Length; i++)
{
mail.To.Add(收信者[i]);
men += 收信者[i];
}
mail.Subject = 標題;
mail.Body = 內文;
//附件刪掉
//System.Net.Mail.Attachment attachment;
//attachment = new System.Net.Mail.Attachment("you attachment file");
//mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("你的email","密碼");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
//MessageBox.Show("mail Send");
}
catch (Exception ex)
{
Debug.Print("發送Email發生錯誤");
}
}
======================================================================
http://csharp.net-informations.com/communications/csharp-cdo-email.htm
======================================================================
加入參考 Microsoft CDO for Windows 2000 Library
using System.Net.Mail;
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("you attachment file");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");//id:自己的email和密碼
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
=========================================================
http://www.comersus.com.tw/Forum/forum_posts.asp?TID=166
=========================================================
CDO 郵件元件使用方法
Microsoft從Windows Server 2000 及 Windows XP開始內建郵件元件已改為CDO
在之前的系統內建郵件元件則是 CDONTS,各位要注意,以下是CDO的用法
屬性 說明
Subject 郵件的主旨
From 寄件人的電子郵件信箱
To 收件人的電子郵件,可用分號;或逗號,斷開成多位收件人
CC 副本收件人的電子郵件,可用分號;或逗號,斷開成多位收件人
BCC 密送副本收件人的電子郵件,可用分號;或逗號,斷開成多位收件人
TextBody 郵件的本文-純文字模式
HTMLBody 郵件的本文-HTML模式
方法 說明
Send 送出郵件
AddAttachment "c:\mydocuments\test.txt" 附件
CreateMHTMLBody "http://www.w3schools.com/asp/" 將一個網頁用HTML格式送出
CreateMHTMLBody "file://c:/mydocuments/test.htm" 將本機硬碟中的一個網頁用HTML格式送出
範例:
送出一封純文字信件:
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.Send
set myMail=nothing
%>
送出一封純文字信件並給副本收件人及密送副本收件人
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.Bcc="someoneelse@somedomain.com"
myMail.Cc="someoneelse2@somedomain.com"
myMail.TextBody="This is a message."
myMail.Send
set myMail=nothing
%>
送出一封HTML信件
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.HTMLBody = "<h1>This is a message.</h1>"
myMail.Send
set myMail=nothing
%>
將一個網頁用HTML格式送出
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.CreateMHTMLBody "http://www.w3schools.com/asp/"
myMail.Send
set myMail=nothing
%>
將本機硬碟中的一個網頁用HTML格式送出
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.CreateMHTMLBody "file://c:/mydocuments/test.htm"
myMail.Send
set myMail=nothing
%>
送出一封純文字信件並含附件
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.AddAttachment "c:\mydocuments\test.txt"
myMail.Send
set myMail=nothing
%>
Sending a text e-mail using a remote server:
用遠端SMTP主機送出一封純文字信件
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") =2
'遠端SMTP主機名稱或IP位址
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver" )="smtp.server.com"
'遠端SMTP主機埠號 Server port
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverp ort") =25
myMail.Configuration.Fields.Update
myMail.Send
set myMail=nothing
%>
留言列表