什么不用多说就是一个方法大家直接看下代码吧
[C#] 纯文本查看 复制代码 /// <summary>
/// 发送邮件
/// </summary>
/// <param name="MailFromAddress">发件人地址</param>
/// <param name="MailFromUserName">用户名</param>
/// <param name="MailFormPwd">密码</param>
/// <param name="MailTitle">邮件标题</param>
/// <param name="MailBody">邮件内容</param>
/// <param name="MailTo">收件人地址</param>
/// <returns>发送成功返回True否则返回False</returns>
public static Boolean SendMail(string MailFromAddress, string MailFormPwd, string MailTitle, string MailBody, string MailTo)
{
try
{
MailMessage objMail = new MailMessage();
//发件人地址
objMail.From = new MailAddress(MailFromAddress);
//邮件标题
objMail.Subject = MailTitle;
//邮件标题编码
objMail.SubjectEncoding = System.Text.Encoding.UTF8;
//邮件内容
objMail.Body = MailBody;
//邮件内容编码
objMail.BodyEncoding = System.Text.Encoding.UTF8;
//收件人地址在这里可以加多个
objMail.To.Add(MailTo);
//用SMTP发送邮件的方式
SmtpClient client = new SmtpClient();
//用户名和密码
client.Credentials = new System.Net.NetworkCredential(MailFromAddress, MailFormPwd);
string StrMail = "";
for (int i = 0; i < MailFromAddress.Length; i++)
{
if (MailFromAddress.Substring(i, 1).ToString().Trim() == "@")
{
StrMail = MailFromAddress.Substring(i + 1, MailFromAddress.Length - i - 1);
}
}
//服务器名
client.Host = "smtp." + StrMail.ToString().Trim();
//发送
client.Send(objMail);
return true;
}
catch (Exception)
{
return false;
}
}
使用的方法也很简单
[C#] 纯文本查看 复制代码 MailMessage objMail = new MailMessage();
//发件人地址
objMail.From = new MailAddress(txtForm.Text.ToString().Trim());
//邮件标题
objMail.Subject = txttitle.Text.ToString().Trim();
//邮件标题编码
objMail.SubjectEncoding = System.Text.Encoding.UTF8;
//邮件内容
objMail.Body = txtBody.Text.ToString().Trim();
//邮件内容编码
objMail.BodyEncoding = System.Text.Encoding.UTF8;
//收件人地址在这里可以加多个
objMail.To.Add(txtTO.Text.ToString().Trim());
//用SMTP发送邮件的方式
SmtpClient client = new SmtpClient();
//用户名和密码
client.Credentials = new System.Net.NetworkCredential(txtForm.Text.ToString().Trim(), txtPwd.Text.ToString().Trim());
//服务器名
client.Host = "SMTP.163.com";
//发送
client.Send(objMail);
里面的MailMessage 大家自己写一下吧就是一个简单的实体类没什么特别之处
在这里呢多给大家提供两个发送邮件的类方法吧,大家可以做为参考
[C#] 纯文本查看 复制代码 using System;
using System.Web.Mail;
using System.Collections;
/**/
/// <summary>
/// 支持批量(发送、抄送、密送)
/// 支持多语言、带多个附件
/// 支持smtp发送邮件服务器验证
/// </summary>
public class DotNetSendMail
{
public DotNetSendMail()
{ }
/**/
/// <summary>
/// 邮件发送结果。如果发送过程出现错误,该值为捕获到的异常提示;否则,该值为“OK”。
/// </summary>
public string SendMailResult
{
get
{
return this.m_SendMailResult;
}
set
{
this.m_SendMailResult = value;
}
}
private string m_SendMailResult;
/**/
/// <summary>
/// 抄送地址,多个以逗号隔开
/// </summary>
public string Cc
{
set
{
this.m_Cc = value;
}
}
private string m_Cc;
/**/
/// <summary>
/// 密送地址,多个以逗号隔开
/// </summary>
public string Bcc
{
set
{
this.m_Bcc = value;
}
}
private string m_Bcc;
/**/
/// <summary>
/// 邮件格式。可以赋值为“1”或“2”,“1”表示Html格式,“2”表示Text格式。
/// </summary>
public int BodyFormat
{
set
{
this.m_BodyFormat = value;
}
}
private int m_BodyFormat = 1;
/**/
/// <summary>
/// 邮件格式。可以赋值为“1”、“2”或“3”。
///“1”表示不紧急邮件,“2”表示普通邮件, “3”表示紧急邮件
/// </summary>
public int MailPriority
{
set
{
this.m_MailPriority = value;
}
}
private int m_MailPriority = 2;
/**/
/// <summary>
/// 附件地址列表,用该属性Add方法加入多个附件,附件地址必须是绝对路径
/// </summary>
public ArrayList AttachFiles
{
get
{
return this.m_AttachFiles;
}
set
{
this.m_AttachFiles = value;
}
}
ArrayList m_AttachFiles = new ArrayList();
/**/
/// <summary>
/// 发送邮件方法,方法中几个参数是发邮件时所必须的
/// </summary>
/// <param name="p_From">发件人</param>
/// <param name="p_To">邮件人,多个收件人逗号隔开</param>
/// <param name="p_Subject">邮件标题</param>
/// <param name="p_Body">正文</param>
/// <param name="p_SmtpServer">发送邮件服务器</param>
/// <param name="p_SmtpUsername">发件服务器登录名</param>
/// <param name="p_SmtpPassword">发件服务器登录密码</param>
/// <returns></returns>
public bool SendMail(string p_From, string p_To, string p_Subject, string p_Body, string p_SmtpServer, string p_SmtpUsername, string p_SmtpPassword)
{
try
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.From = p_From;
mail.To = p_To;
mail.Subject = p_Subject;
mail.Body = p_Body;
SmtpMail.SmtpServer = p_SmtpServer;
//邮件服务器验证,需要输入您在邮件发送服务器上注册的邮箱用户名和密码
if (p_SmtpUsername != "")
{
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", p_SmtpUsername);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", p_SmtpPassword);
}
//以上是发邮件时所必需的,下面是额外的一些功能设置
if (this.m_Cc != "")
{
mail.Cc = this.m_Cc;
}
if (this.m_Bcc != "")
{
mail.Bcc = this.m_Bcc;
}
switch (this.m_BodyFormat)
{
case 1:
mail.BodyFormat = MailFormat.Html;
break;
case 2:
mail.BodyFormat = MailFormat.Text;
break;
default:
break;
}
switch (this.m_MailPriority)
{
case 1:
mail.Priority = System.Web.Mail.MailPriority.Low;
break;
case 2:
mail.Priority = System.Web.Mail.MailPriority.Normal;
break;
case 3:
mail.Priority = System.Web.Mail.MailPriority.High;
break;
default:
break;
}
if (this.m_AttachFiles.Count > 0)
{
foreach (string file in m_AttachFiles)
{
if (file.Trim() != "")
{
mail.Attachments.Add(new MailAttachment(file.Trim()));
}
}
}
SmtpMail.Send(mail);
this.m_SendMailResult = "OK";
return true;
}
catch (Exception ex)
{
this.m_SendMailResult = ex.ToString();
return false;
}
}
}
下面还有一个我个人最喜欢这个
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace BLL.Function
{
public class EmailServices
{
///<summary>
/// 发送邮件
///</summary>
///<param name="sendEmailAddress">发件人邮箱</param>
///<param name="sendEmailPwd">发件人密码</param>
///<param name="msgToEmail">收件人邮箱地址</param>
///<param name="title">邮件标题</param>
///<param name="content">邮件内容</param>
///<returns>0:失败。1:成功!</returns>
public static int SendEmail(string sendEmailAddress, string sendEmailPwd, string[] msgToEmail, string title, string content)
{
//发件者邮箱地址
string fjrtxt = sendEmailAddress;
//发件者邮箱密码
string mmtxt = sendEmailPwd;
////收件人收箱地址
//string sjrtxt = msgToEmail;
//主题
string zttxt = title;
//附件
//string fjtxt = fj.Text;
//内容
string nrtxt = content;
string[] fasong = fjrtxt.Split('@');
string[] fs = fasong[1].Split('.');
//发送
//设置邮件协议
SmtpClient client = new SmtpClient("smtp." + fs[0].ToString().Trim() + ".com");
client.UseDefaultCredentials = false;
//通过网络发送到Smtp服务器
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//通过用户名和密码 认证
client.Credentials = new NetworkCredential(fasong[0].ToString(), mmtxt);
//发件人和收件人的邮箱地址
MailMessage mmsg = new MailMessage();
mmsg.From = new MailAddress(fjrtxt);
for (int i = 0; i < msgToEmail.Length; i++)
{
mmsg.To.Add(new MailAddress(msgToEmail));
}
//邮件主题
mmsg.Subject = zttxt;
//主题编码
mmsg.SubjectEncoding = Encoding.UTF8;
//邮件正文
mmsg.Body = nrtxt;
//正文编码
mmsg.BodyEncoding = Encoding.UTF8;
//设置为HTML格式
mmsg.IsBodyHtml = true;
//优先级
mmsg.Priority = MailPriority.High;
//if (fj.Text.Trim() != "")
//{
////增加附件
// mmsg.Attachments.Add(new Attachment(fj.Text));
//}
try
{
client.Send(mmsg);
return 1;
}
catch
{
return 0;
}
}
}
} |