public static void CreateTimeoutTestMessage(string server) { string to = "[email protected]"; string from = "[email protected]"; string subject = "Using the new SMTP client."; string body = @"Using this new feature, you can send an e-mail message from an application very easily."; MailMessage message = new MailMessage(from, to, subject, body); SmtpClient client = new SmtpClient(server); Console.WriteLine("Changing time out from {0} to 100.", client.Timeout); client.Timeout = 100; // Credentials are necessary if the server requires the client // to authenticate before it will send e-mail on the client's behalf. client.Credentials = CredentialCache.DefaultNetworkCredentials; client.Send(message); }
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(); }
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
{
string to = "[email protected]";
string from = "[email protected]";
string subject = "Using the new SMTP client.";
string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient(server);
Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
client.Timeout = 100;
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
}
或者上网找一些发邮件的组件,比如JMail,是Com组件,功能很强大,使用方便,而且可以用密码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="TABLE1" runat="server" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 393px">
收信:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
主题:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
内容:<asp:TextBox ID="TextBox3" runat="server" Height="154px" TextMode="MultiLine"
Width="336px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="发送" OnClick="Button1_Click" /></td>
</tr>
</table>
</div>
<table id="Table2" runat="server" border="0" cellpadding="0" cellspacing="0" visible="false">
<tr>
<td align="center" style="width: 400px">
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="恭喜您,发表成功!"></asp:Label><br />
<asp:Button ID="Button2" runat="server" Text="返回" OnClick="Button2_Click" /></td>
</tr>
</table>
</form>
</body>
</html>
.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class sendMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
////设置发件人信箱,及显示名字
MailAddress from = new MailAddress("[email protected]", "J.L.C");
//设置收件人信箱,及显示名字
MailAddress to = new MailAddress(TextBox1.Text, "JLC");
//创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to); oMail.Subject = TextBox2.Text; //邮件标题
oMail.Body = TextBox3.Text; //邮件内容 oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
oMail.Priority = MailPriority.High;//设置邮件的优先级为高 //发送邮件服务器
SmtpClient client = new SmtpClient();
client.Host = "smtp.126.com"; //指定邮件服务器
client.Credentials = new NetworkCredential("[email protected]", "jlc3509589934");//指定服务器邮件,及密码 //发送
try
{
client.Send(oMail); //发送邮件
Label1.Text = "恭喜你!邮件发送成功。";
}
catch
{
Label1.Text = "邮件发送失败,检查网络及信箱是否可用。";
} oMail.Dispose(); //释放资源 TABLE1.Visible = false;
Table2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
//返回,继续发送
Response.Redirect(Request.Url.ToString());
TABLE1.Visible = true;
Table2.Visible = false;
}
}
jmail.MessageClass oJmailMessage = new jmail.MessageClass();//建立发邮件类 oJmailMessage.Charset = "GB2312";// 字符集
oJmailMessage.Encoding = "BASE64";//附件的编码格式
oJmailMessage.ContentType = "text/html";
oJmailMessage.ISOEncodeHeaders = false;//是否将信头编码成iso-8859-1字符集
oJmailMessage.Priority = Convert.ToByte(1);// 优先级
oJmailMessage.From = TxtEmail.Text.Trim();//发送人邮件地址
oJmailMessage.FromName = TxtName.Text.Trim();//发送人姓名
oJmailMessage.Subject = txtSubject.Text.Trim();// 邮件主题
oJmailMessage.MailServerUserName = TxtSmtpUser.Text.Trim();//身份验证的用户名
oJmailMessage.MailServerPassWord = TxtSmtpPwd.Text.Trim();//用户密码 //添加一个收件人,抄送人和密送人的添加和该方法是一样的,只是分别使用AddRecipientCC和RecipientBCC两个属性
//要是需要添加多个收件人,则重复下面的语句即可。添加多个抄送和密送人的方法一样
oJmailMessage.AddRecipient(txtReciver.Text.Trim(),"","");
if("" != upFile.PostedFile.FileName)
{
string attpath = upFile.PostedFile.FileName;
oJmailMessage.AddAttachment(@attpath,true,attpath.Substring(attpath.LastIndexOf(".")+1,3));//添加附件
}
oJmailMessage.Body = txtContent.Text.Trim();//邮件内容 if(oJmailMessage.Send(TxtSmtServer.Text.Trim(),false))
{
Response.Write("发送成功!");
}
else
{
Response.Write("发送失败,请检查邮件服务器的设置!");
} oJmailMessage = null;
smtp.Host = host; //邮件服务器 比如smtp.163.com
smtp.Port = port; //一般是25
System.Net.NetworkCredential userinfo = new System.Net.NetworkCredential(userName, password);//userName 是发件人邮箱用户名或电子邮件地址,password:发件人邮箱密码
smtp.Credentials = userinfo;//声明邮件内容
//发送邮件
MailMessage message = new MailMessage();
message.From = new MailAddress("发件人地址", "显示的名称", "编码");
message.To.Add("收件人地址");
message.Subject = subject;//邮件标题
message.SubjectEncoding = subjectEncoding; //邮件编码
message.Body = body;//邮件内容
message.BodyEncoding = bodyEncoding; //内容编码
message.IsBodyHtml = isBodyHtml; //是否是HTML格式
message.Priority = MailPriority.Normal; //邮件级别
message.Sender = new MailAddress("实际发件人");//可不设置
message.ReplyTo = new MailAddress("回复地址", "收件人回复时显示的名称", "编码"); smtp.Send(message);
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();
}
发邮件尤其是文本的应该比较简单,如果是html的要怎么做呢?