MailMessage mail = new MailMessage(); mail.To = "[email protected]"; mail.From = "[email protected]"; mail.Subject = "this is a test email."; mail.Body = "Some text goes here"; mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authenticationmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); //set your username heremail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); //set your passwordhere SmtpMail.SmtpServer = "smtp.126.com"; //your real server goes here SmtpMail.Send( mail );
// Build an IList of mail attachments. if (sAttach != "") { char[] delim = new char[] {','}; foreach (string sSubstr in sAttach.Split(delim)) { MailAttachment MyAttachment = new MailAttachment(sSubstr); MyMail.Attachments.Add(MyAttachment); } }
mail.To = "[email protected]";
mail.From = "[email protected]";
mail.Subject = "this is a test email.";
mail.Body = "Some text goes here";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authenticationmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); //set your username heremail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); //set your passwordhere SmtpMail.SmtpServer = "smtp.126.com"; //your real server goes here
SmtpMail.Send( mail );
封装好的代码,可以参考!
http://www.codeproject.com/KB/IP/Pop3MailClient.aspx
Windows Email Client application using .NET (C#)
http://www.codeproject.com/KB/IP/WinEmailClient.aspx
/// Email-Send
/// </summary>
/// <param name="strSmtpServer">如果是163邮箱就写smtp.163.com</param>
/// <param name="strFrom">发件人的帐号</param>
/// <param name="strFromPass">发件人密码</param>
/// <param name="strto">收件人帐号</param>
/// <param name="strSubject">主题</param>
/// <param name="strBody">内容</param>
public static void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
/// 发送电子邮件成功返回True,失败返回False
/// </summary>
/// <returns></returns>
private bool SendMail()
{
MailAddress from = new MailAddress(TxtSendEmail.Text, TxtSendName.Text, System.Text.Encoding.GetEncoding("gb2312")); //收件人地址
MailAddress to = new MailAddress(TxtEmail.Text, TxtName.Text, System.Text.Encoding.GetEncoding("gb2312"));
MailMessage message = new MailMessage(from, to);
//添加附件,判断文件存在就添加
if (FuloadAdjunct.PostedFile.FileName != "")
{
if (System.IO.File.Exists(Path.GetFullPath(FuloadAdjunct.PostedFile.FileName)))
{
Attachment item = new Attachment(Path.GetFullPath(FuloadAdjunct.PostedFile.FileName), MediaTypeNames.Text.Plain);
message.Attachments.Add(item);
}
}
message.Subject = TxtTitle.Text; // 设置邮件的标题
message.Body = TxtContent.Text; //发送邮件的正文
//message.BodyEncoding = System.Text.Encoding.Unicode; //System.Text.Encoding.Default;
//message.BodyEncoding = System.Text.Encoding.UTF8;
message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
//MailAddress other = new MailAddress("[email protected]");
//message.CC.Add(other); //添加抄送人 //创建一个SmtpClient 类的新实例,并初始化实例的SMTP 事务的服务器
SmtpClient client = new SmtpClient(@""+TxtSMTP.Text+"");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = false; //身份认证 client.Credentials = new System.Net.NetworkCredential(TxtSendEmail.Text, TxtPassWord.Text); bool ret = true; //返回值
try
{
client.Send(message);
}
catch (SmtpException ex)
{
Response.Write(ex.Message);
ret = false;
}
catch (Exception ex2)
{
Response.Write(ex2.Message);
ret = false;
}
return ret;
}
这个是.net中自带的 还有个Jmail的
// /**/
// ///建立发邮件类
// jmail.MessageClass oJmailMessage = new jmail.MessageClass(); // /**/
// /// 字符集
// oJmailMessage.Charset = "GB2312"; // /**/
// ///附件的编码格式
// oJmailMessage.Encoding = "BASE64"; // //附件的格式定义
// oJmailMessage.ContentType = "Encoding"; // /**/
// ///是否将信头编码成iso-8859-1字符集
// oJmailMessage.ISOEncodeHeaders = false; // /**/
// /// 优先级
// oJmailMessage.Priority = Convert.ToByte(1); // /**/
// ///发送人邮件地址
// oJmailMessage.From = "[email protected]"; // /**/
// ///发送人姓名
// oJmailMessage.FromName = "张毅"; // oJmailMessage.Logging = true; // /**/
// /// 邮件主题
// oJmailMessage.Subject = "产品管理"; // /**/
// ///身份验证的用户名
// oJmailMessage.MailServerUserName = "[email protected]"; // /**/
// ///用户密码
// oJmailMessage.MailServerPassWord = "123456"; // //添加一个收件人,抄送人和密送人的添加和该方法是一样的,只是分别使用AddRecipientCC和RecipientBCC两个属性
// //要是需要添加多个收件人,则重复下面的语句即可。添加多个抄送和密送人的方法一样
// oJmailMessage.AddRecipient("[email protected]", "[email protected]", ""); // //jmail.AddAttachment(strFileName, true, null);//指定附件文件
// oJmailMessage.AddAttachment("C:\\Documents and Settings\\zhangyi\\桌面\\新建文件夹 (2)\\PdaConfigFile.xls", true, null); // //判断如果附件不为空则添加附件
// //if ("" != FileUExce.PostedFile.FileName)
// //{
// // string attpath = FileUExce.PostedFile.FileName;
// // oJmailMessage.AddAttachment(@attpath, true, attpath.Substring(attpath.LastIndexOf(".") + 1, 3));//添加附件
// // oJmailMessage.AddAttachment(attpath, true, null);
// //} // ///邮件内容
// // oJmailMessage.Body = "请接收";
// // oJmailMessage.Body = FileUExce.PostedFile.FileName;
// oJmailMessage.Body = FuloadAdjunct.PostedFile.FileName; // if (oJmailMessage.Send("218.106.248.88", false))
// {
// Response.Write("发送成功!");
// }
// else
// {
// Response.Write("发送失败,请检查邮件服务器的设置!");
// } // oJmailMessage = null;
//}
//protected void Button2_Click(object sender, EventArgs e)
//{
// #region 以 Jmail 形式发邮件
// /**/
// ///建立发邮件类
// jmail.MessageClass oJmailMessage = new jmail.MessageClass(); // /**/
// /// 字符集
// oJmailMessage.Charset = "GB2312"; // /**/
// ///附件的编码格式
// oJmailMessage.Encoding = "BASE64"; // //附件的格式定义
// oJmailMessage.ContentType = "Encoding"; // /**/
// ///是否将信头编码成iso-8859-1字符集
// oJmailMessage.ISOEncodeHeaders = false; // /**/
// /// 优先级
// oJmailMessage.Priority = Convert.ToByte(1); // /**/
// ///发送人邮件地址
// oJmailMessage.From = "[email protected]"; // /**/
// ///发送人姓名
// oJmailMessage.FromName = "张毅"; // oJmailMessage.Logging = true; // /**/
// /// 邮件主题
// oJmailMessage.Subject = "产品管理"; // /**/
// ///身份验证的用户名
// oJmailMessage.MailServerUserName = "[email protected]"; // /**/
// ///用户密码
// oJmailMessage.MailServerPassWord = "123456"; // //添加一个收件人,抄送人和密送人的添加和该方法是一样的,只是分别使用AddRecipientCC和RecipientBCC两个属性
// //要是需要添加多个收件人,则重复下面的语句即可。添加多个抄送和密送人的方法一样
// oJmailMessage.AddRecipient("[email protected]", "[email protected]", ""); // //jmail.AddAttachment(strFileName, true, null);//指定附件文件
// oJmailMessage.AddAttachment("C:\\Documents and Settings\\zhangyi\\桌面\\新建文件夹 (2)\\PdaConfigFile.xls", true, null); // //判断如果附件不为空则添加附件
// //if ("" != FileUExce.PostedFile.FileName)
// //{
// // string attpath = FileUExce.PostedFile.FileName;
// // oJmailMessage.AddAttachment(@attpath, true, attpath.Substring(attpath.LastIndexOf(".") + 1, 3));//添加附件
// // oJmailMessage.AddAttachment(attpath, true, null);
// //} // ///邮件内容
// // oJmailMessage.Body = "请接收";
// oJmailMessage.Body = FileUExce.PostedFile.FileName; // if (oJmailMessage.Send("218.106.248.88", false))
// {
// Response.Write("发送成功!");
// }
// else
// {
// Response.Write("发送失败,请检查邮件服务器的设置!");
// } // oJmailMessage = null;
// #endregion
// if (SendMail())
// {
// Response.Write("发送成功");
// }
// else
// {
// Response.Write("发送失败");
// }
Jmail 的 你要去找个组件安装下才可以 2 个我都用过了都可以成功发送
命名空间:System.Web.Mail
程序集:System.Web(在 system.web.dll 中)语法
C#
[ObsoleteAttribute("The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202")]
public class MailMessage
JScript
ObsoleteAttribute("The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202")
public class MailMessage
示例语言:C#
<%--
This example shows how to send a mail message from a Web Forms page
using the classes in the System.Web.Mail namespace.
--%><%@ IMPORT namespace="System.Web.Mail" %><html>
<script language=C# runat=server>
void Page_Load()
{
if (!IsPostBack)
{
txtTo.Text="[email protected]";
txtFrom.Text="[email protected]";
txtCc.Text="[email protected]";
txtBcc.Text="[email protected]";
txtSubject.Text="Hello";
txtBody.Text="This is a test message.";
txtAttach.Text=@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg,"
+ @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg";
txtBodyEncoding.Text = Encoding.ASCII.EncodingName;
txtBodyFormat.Text="HTML";
txtPriority.Text="Normal";
txtUrlContentBase.Text="http://www.contoso.com/images";
txtUrlContentLocation.Text="http://www.contoso.com/images";
// Name of relay mail server in your domain.
txtMailServer.Text="smarthost";
}
} void btnSubmit_Click(Object sender, EventArgs e)
{
string sTo, sFrom, sSubject, sBody;
string sAttach, sCc, sBcc, sBodyEncoding;
string sBodyFormat, sMailServer, sPriority;
string sUrlContentBase, sUrlContentLocation;
int iLoop1; sTo = txtTo.Text.Trim();
sFrom = txtFrom.Text.Trim();
sSubject = txtSubject.Text.Trim();
sBody = txtBody.Text.Trim();
sAttach = txtAttach.Text.Trim();
sCc = txtCc.Text.Trim();
sBcc = txtBcc.Text.Trim();
sBodyFormat = txtBodyFormat.Text.Trim();
sBodyEncoding = txtBodyEncoding.Text.Trim();
sPriority = txtPriority.Text.Trim();
sUrlContentBase = txtUrlContentBase.Text.Trim();
sUrlContentLocation = txtUrlContentLocation.Text.Trim();
sMailServer = txtMailServer.Text.Trim(); MailMessage MyMail = new MailMessage();
MyMail.From = sFrom;
MyMail.To = sTo;
MyMail.Subject = sSubject;
MyMail.Body = sBody;
MyMail.Cc = sCc;
MyMail.Bcc = sBcc;
MyMail.UrlContentBase = sUrlContentBase;
MyMail.UrlContentLocation = sUrlContentLocation; if (txtBodyEncoding.Text == Encoding.UTF7.EncodingName)
MyMail.BodyEncoding = Encoding.UTF7;
else if (txtBodyEncoding.Text == Encoding.UTF8.EncodingName)
MyMail.BodyEncoding = Encoding.UTF8;
else
MyMail.BodyEncoding = Encoding.ASCII; switch (sBodyFormat.ToUpper())
{
case "HTML":
MyMail.BodyFormat = MailFormat.Html;
break;
default:
MyMail.BodyFormat = MailFormat.Text;
break;
}
switch (sPriority.ToUpper())
{
case "HIGH":
MyMail.Priority = MailPriority.High;
break;
case "LOW":
MyMail.Priority = MailPriority.Low;
break;
default:
MyMail.Priority = MailPriority.Normal;
break;
}
// Build an IList of mail attachments.
if (sAttach != "")
{
char[] delim = new char[] {','};
foreach (string sSubstr in sAttach.Split(delim))
{
MailAttachment MyAttachment = new MailAttachment(sSubstr);
MyMail.Attachments.Add(MyAttachment);
}
}
SmtpMail.SmtpServer = sMailServer;
SmtpMail.Send(MyMail);
lblMsg1.Text="C# Message sent to " + MyMail.To;
} void btnClear_Click(Object sender, EventArgs e)
{
lblMsg1.Text="";
txtTo.Text="";
txtFrom.Text="";
txtSubject.Text="";
txtBody.Text="";
txtAttach.Text="";
txtBcc.Text="";
txtCc.Text="";
txtBodyEncoding.Text="";
txtBodyFormat.Text="";
txtPriority.Text="";
txtUrlContentBase.Text="";
txtUrlContentLocation.Text="";
txtMailServer.Text="";
btnSubmit.Text="Submit";
}
</script> <p><h4>Send a new mail message:<h4></p>
<Form method="Post" action="MailForm.aspx" runat=server>
<table width="350" bgcolor="#FFFF99">
<tr>
<td Align="Right"><b>To:</b></td>
<td><Asp:Textbox id="txtTo" runat=server/></td>
</tr>
<tr>
<h4><td Align="Right"><b>From:</b></td></h4>
<td><Asp:Textbox id="txtFrom" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>Subject:</b></td>
<td><Asp:Textbox id="txtSubject" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>MessageBody:</b></td>
<td><Asp:Textbox id="txtBody" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>Attachments:</b></td>
<td><Asp:Textbox id="txtAttach" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>CC:</b></td>
<td><Asp:Textbox id="txtBcc" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>BCC:</b></td>
<td><Asp:Textbox id="txtCc" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>BodyEncoding:</b></td>
<td><Asp:Textbox id="txtBodyEncoding" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>BodyFormat:</b></td>
<td><Asp:Textbox id="txtBodyFormat" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>Priority:</b></td>
<td><Asp:Textbox id="txtPriority" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>URL Content Base:</b></td>
<td><Asp:Textbox id="txtUrlContentBase" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>URL Content Location:</b></td>
<td><Asp:Textbox id="txtUrlContentLocation" runat=server/></td>
</tr>
<tr>
<td Align="Right"><b>Mail Server:</b></td>
<td><Asp:Textbox id="txtMailServer" runat=server/></td>
</tr>
</table><br> <asp:button id="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat=server/>
<asp:button id="btnClear" Text="Clear" OnClick="btnClear_Click" runat=server/>
<p><asp:Label id="lblMsg1" runat=server/></p>
</form>
</html>
MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add("收件人地址");
//收件人
//发件人信息
msg.From = new MailAddress("发件人地址", "发件人姓名", System.Text.Encoding.UTF8);
msg.Subject = "这是测试邮件";
//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;
//标题编码
msg.Body = mailContent;
//邮件主体
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true;
//是否HTML
msg.Priority = MailPriority.High;
//优先级
SmtpClient client = new SmtpClient();
//设置GMail邮箱和密码
client.Credentials = new System.Net.NetworkCredential("发件人地址", "邮箱密码");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = 25;
//使用QQ邮箱发送,那么要在QQ邮箱设置中设置-> 账户-> POP3/IMAP/SMTP服务 开启
//平且要保证服务开启 管理工具->服务->simple mail Transfer protocol (SMTP)
//如果是163就改为client.Host = "smtp.163.com";
client.Host = "smtp.qq.com";
//设置QQ邮箱的服务器名称
client.EnableSsl = false;
object userState = msg;
try
{
client.Send(msg);
MessageBox.Show("发送成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "发送邮件出错");
}