//要引用System.Web.Mail命名空间。引用该命名空间需要添加System.Web.Dll MailMessage mail = new MailMessage(); mail.BodyFormat=MailFormat.Html; mail.From ="some@server"; mail.To="some@server";//多个收件人之间用分号分割 mail.Bcc="some@server";//密送 mail.Cc=="some@server";//抄送 mail.Subject = "this is mail subject"; mail.Body ="this is mail body"; mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "username"); mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password);
using System.Net.Mail; using System.Net.Mime; using System.Net; public static void CreateMessageWithAttachment(string server) { // Specify the file to be attached and sent. // This example assumes that a file named Data.xls exists in the // current working directory. string file = @"D:\asdf.txt"; // Create a message and set up the recipients. MailMessage message = new MailMessage( "[email protected]", "[email protected]", "test", "no du"); // Create the file attachment for this e-mail message. Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); // Add time stamp information for the file. ContentDisposition disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); // Add the file attachment to this e-mail message. message.Attachments.Add(data); //Send the message. SmtpClient client = new SmtpClient(server); // Add credentials if the SMTP server requires them. //client.Credentials = CredentialCache.DefaultNetworkCredentials; client.UseDefaultCredentials = true; client.Credentials = new System.Net.NetworkCredential("username", "password"); client.Send(message); // Display the values in the ContentDisposition for the attachment. ContentDisposition cd = data.ContentDisposition; Console.WriteLine("Content disposition"); Console.WriteLine(cd.ToString()); Console.WriteLine("File {0}", cd.FileName); Console.WriteLine("Size {0}", cd.Size); Console.WriteLine("Creation {0}", cd.CreationDate); Console.WriteLine("Modification {0}", cd.ModificationDate); Console.WriteLine("Read {0}", cd.ReadDate); Console.WriteLine("Inline {0}", cd.Inline); Console.WriteLine("Parameters: {0}", cd.Parameters.Count); foreach (DictionaryEntry d in cd.Parameters) { Console.WriteLine("{0} = {1}", d.Key, d.Value); } data.Dispose(); } protected void Timer1_Tick(object sender, EventArgs e) { if (DateTime.Now.Second % 20 == 0) { CreateMessageWithAttachment("smtp.163.com"); } }
给你写个最简单最详细的using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; namespace smtp1 { class Program { static void Main(string[] args) { try { TcpClient smtpclient = new TcpClient(); smtpclient.Connect(IPAddress.Parse("127.0.0.1"), 25);//连接到服务端 NetworkStream ns = smtpclient.GetStream(); string result = Receive(ns); Console.Write(result); send(ns, "helo\r\n");//首先必须跟服务器发送此标记 result = Receive(ns); Console.Write(result); //身份验证 send(ns, "Auth Login\r\n"); result = Receive(ns);//服务器返回一条让你输入用户名的指令,此返回为base64 Console.Write(ConverToString(result.Substring(result.IndexOf(' ')+1))+"\r\n"); if (result.StartsWith("334")) { //输入用户名 send(ns, ConvertToBase64("gg") + "\r\n");//(因为用到了esmtp,它有个身份验证,是以base64来表示的)要想以base64输出,必须先转换成byte[],在从byte[]转换回base64 result = Receive(ns);///服务器返回一条让你输入密码的指令,此返回为base64 Console.Write(ConverToString(result.Substring(result.LastIndexOf(' ') + 1))+"\r\n"); //Console.Write(result); //输入密码 send(ns, ConvertToBase64("gg") + "\r\n"); result = Receive(ns); Console.Write(result); if (result.StartsWith("235")) { Console.WriteLine("从现在开始可以正常收发邮件了!"); send(ns, "Mail From:<[email protected]>\r\n"); result = Receive(ns); Console.Write(result); if (result.StartsWith("250")) { send(ns, "RCPT To:<[email protected]>\r\n"); result = Receive(ns); Console.Write(result); ///////这里可以发送给多个人,服务器属于双项的,你发出去服务器也会给你返回一条,必须去接收 send(ns, "RCPT To:<[email protected]>\r\n"); result = Receive(ns); Console.Write(result); if (result.StartsWith("250")) { send(ns, "DATA\r\n"); result = Receive(ns); Console.Write(result); if (result.StartsWith("354")) { string content = null; //content+ = "From:<[email protected]>\r\n"; //content += "To:<[email protected]>\r\n"; //content += "Date:" + DateTime.Now.ToString()+"\r\n"; //content += "Subject:我是主题\r\n"; //content += "Cc:[email protected]\r\n"; //content += "\r\n"; //content += "我是邮件的正文内容\r\n"; content += ".\r\n"; send(ns, content); result = Receive(ns); Console.Write(result); if (result.StartsWith("250")) { Console.Write("发送成功!"); } } } } } } } catch { Console.WriteLine("连接失败!"); } } #region 接收 static string Receive(NetworkStream ns) { byte[] data=new byte[1024]; int size = ns.Read(data,0,1024); return Encoding.UTF8.GetString(data,0,size); } #endregion #region 发送 static void send(NetworkStream ns,string msg) { byte[] data = Encoding.UTF8.GetBytes(msg); ns.Write(data,0,data.Length); ns.Flush(); } #endregion static string ConvertToBase64(string str) { byte[] data = Encoding.UTF8.GetBytes(str); return Convert.ToBase64String(data); } static string ConverToString(string str) { byte[] data = Convert.FromBase64String(str); return Encoding.UTF8.GetString(data); } } }
mail.BodyFormat=MailFormat.Html;
mail.From ="some@server";
mail.To="some@server";//多个收件人之间用分号分割
mail.Bcc="some@server";//密送
mail.Cc=="some@server";//抄送
mail.Subject = "this is mail subject";
mail.Body ="this is mail body";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "username");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password);
System.Web.Mail.MailAttachment attachment=new MailAttachment("c:\\1.txt");
mail.Attachments.Add(attachment);
SmtpMail.SmtpServer = "10.2.140.100";
SmtpMail.Send(mail);
使用很简单,查查MSDN就行了
http://topic.csdn.net/u/20080301/17/18568a4c-0c34-4bad-b0c5-08bc16a297ef.html
using System.Net.Mime;
using System.Net; public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = @"D:\asdf.txt";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"[email protected]",
"[email protected]",
"test",
"no du"); // Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
//client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("username", "password");
client.Send(message);
// Display the values in the ContentDisposition for the attachment.
ContentDisposition cd = data.ContentDisposition;
Console.WriteLine("Content disposition");
Console.WriteLine(cd.ToString());
Console.WriteLine("File {0}", cd.FileName);
Console.WriteLine("Size {0}", cd.Size);
Console.WriteLine("Creation {0}", cd.CreationDate);
Console.WriteLine("Modification {0}", cd.ModificationDate);
Console.WriteLine("Read {0}", cd.ReadDate);
Console.WriteLine("Inline {0}", cd.Inline);
Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
foreach (DictionaryEntry d in cd.Parameters)
{
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
data.Dispose();
} protected void Timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Second % 20 == 0)
{
CreateMessageWithAttachment("smtp.163.com");
}
}
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace smtp1
{
class Program
{
static void Main(string[] args)
{
try
{
TcpClient smtpclient = new TcpClient();
smtpclient.Connect(IPAddress.Parse("127.0.0.1"), 25);//连接到服务端
NetworkStream ns = smtpclient.GetStream();
string result = Receive(ns);
Console.Write(result);
send(ns, "helo\r\n");//首先必须跟服务器发送此标记
result = Receive(ns);
Console.Write(result);
//身份验证
send(ns, "Auth Login\r\n");
result = Receive(ns);//服务器返回一条让你输入用户名的指令,此返回为base64
Console.Write(ConverToString(result.Substring(result.IndexOf(' ')+1))+"\r\n");
if (result.StartsWith("334"))
{
//输入用户名
send(ns, ConvertToBase64("gg") + "\r\n");//(因为用到了esmtp,它有个身份验证,是以base64来表示的)要想以base64输出,必须先转换成byte[],在从byte[]转换回base64
result = Receive(ns);///服务器返回一条让你输入密码的指令,此返回为base64
Console.Write(ConverToString(result.Substring(result.LastIndexOf(' ') + 1))+"\r\n");
//Console.Write(result);
//输入密码
send(ns, ConvertToBase64("gg") + "\r\n");
result = Receive(ns);
Console.Write(result);
if (result.StartsWith("235"))
{
Console.WriteLine("从现在开始可以正常收发邮件了!");
send(ns, "Mail From:<[email protected]>\r\n");
result = Receive(ns);
Console.Write(result);
if (result.StartsWith("250"))
{
send(ns, "RCPT To:<[email protected]>\r\n");
result = Receive(ns);
Console.Write(result);
///////这里可以发送给多个人,服务器属于双项的,你发出去服务器也会给你返回一条,必须去接收
send(ns, "RCPT To:<[email protected]>\r\n");
result = Receive(ns);
Console.Write(result);
if (result.StartsWith("250"))
{
send(ns, "DATA\r\n");
result = Receive(ns);
Console.Write(result);
if (result.StartsWith("354"))
{
string content = null;
//content+ = "From:<[email protected]>\r\n";
//content += "To:<[email protected]>\r\n";
//content += "Date:" + DateTime.Now.ToString()+"\r\n";
//content += "Subject:我是主题\r\n";
//content += "Cc:[email protected]\r\n";
//content += "\r\n";
//content += "我是邮件的正文内容\r\n";
content += ".\r\n";
send(ns, content);
result = Receive(ns);
Console.Write(result);
if (result.StartsWith("250"))
{
Console.Write("发送成功!");
}
}
}
}
}
}
}
catch
{
Console.WriteLine("连接失败!");
}
}
#region 接收
static string Receive(NetworkStream ns)
{
byte[] data=new byte[1024];
int size = ns.Read(data,0,1024);
return Encoding.UTF8.GetString(data,0,size);
}
#endregion
#region 发送
static void send(NetworkStream ns,string msg)
{
byte[] data = Encoding.UTF8.GetBytes(msg);
ns.Write(data,0,data.Length);
ns.Flush();
}
#endregion
static string ConvertToBase64(string str)
{
byte[] data = Encoding.UTF8.GetBytes(str);
return Convert.ToBase64String(data);
}
static string ConverToString(string str)
{
byte[] data = Convert.FromBase64String(str);
return Encoding.UTF8.GetString(data);
}
}
}