两个asp.net发送邮件类
代码
//第一个
using System;
using System.Text;
using System.Net.Mail;
namespace Cars.Tootls.Tools
{
public class Email
{
private Email()
{
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="Subject">主题</param>
/// <param name="Body">发送内容</param>
/// <param name="To">目标邮件地址,多个邮件用,逗号分隔</param>
/// <returns></returns>
public static bool Send(string Subject, string Body,string To)
{
string SysEmail = "****@grefam.com";
string SiteName = "******";
string smtpSite = "smtp.gmail.com";
int Port = 25;
bool SSL = true;
string MailUser = "*****@grefam.com";
string MailPwd = "*****";
//NormalConfigInfo configInfo = NormalConfig.Load();
MailMessage message = new MailMessage();
message.From = new MailAddress(SysEmail,SiteName);
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
message.SubjectEncoding = Encoding.UTF8;
SmtpClient smtp = new SmtpClient();
smtp.Host = smtpSite;
smtp.Port = Port;
smtp.EnableSsl = SSL;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(MailUser, MailPwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
bool reBool = true;
message.Subject = Subject;
message.Body = Body;
string[] tos = Utils.SplitString(To, ",");
for (int i = 0; i < tos.Length; i++)
{
message.To.Add(tos[i]);
}
try
{
smtp.Send(message);
}
catch
{
reBool = false;
}
return reBool;
}
}
}
using System;
using System.Text;
using System.Net.Mail;
namespace Cars.Tootls.Tools
{
public class Email
{
private Email()
{
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="Subject">主题</param>
/// <param name="Body">发送内容</param>
/// <param name="To">目标邮件地址,多个邮件用,逗号分隔</param>
/// <returns></returns>
public static bool Send(string Subject, string Body,string To)
{
string SysEmail = "****@grefam.com";
string SiteName = "******";
string smtpSite = "smtp.gmail.com";
int Port = 25;
bool SSL = true;
string MailUser = "*****@grefam.com";
string MailPwd = "*****";
//NormalConfigInfo configInfo = NormalConfig.Load();
MailMessage message = new MailMessage();
message.From = new MailAddress(SysEmail,SiteName);
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
message.SubjectEncoding = Encoding.UTF8;
SmtpClient smtp = new SmtpClient();
smtp.Host = smtpSite;
smtp.Port = Port;
smtp.EnableSsl = SSL;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(MailUser, MailPwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
bool reBool = true;
message.Subject = Subject;
message.Body = Body;
string[] tos = Utils.SplitString(To, ",");
for (int i = 0; i < tos.Length; i++)
{
message.To.Add(tos[i]);
}
try
{
smtp.Send(message);
}
catch
{
reBool = false;
}
return reBool;
}
}
}
代码
//第二个
源代码如下:
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.Mail;
public partial class mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MailAddress MessageFrom = new MailAddress("jian0487@163.com"); //发件人邮箱地址
string MessageTo = "441232476@qq.com"; //收件人邮箱地址
string MessageSubject = "邮件主题"; //邮件主题
string MessageBody = "这里是邮件内容。"; //邮件内容
if (Send(MessageFrom, MessageTo, MessageSubject, MessageBody))
{
Response.Write("发送邮件成功");
}
else
{
Response.Write("发送邮件失败");
}
}
/// <summary>
/// 发送电子邮件
/// </summary>
/// <param name="MessageFrom">发件人邮箱地址</param>
/// <param name="MessageTo">收件人邮箱地址</param>
/// <param name="MessageSubject">邮件主题</param>
/// <param name="MessageBody">邮件内容</param>
/// <returns></returns>
public bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
{
MailMessage message = new MailMessage();
// if (FileUpload1.PostedFile.FileName != "")
// {
// Attachment att = new Attachment("d://test.txt");//发送附件的内容
// message.Attachments.Add(att);
// }
message.From = MessageFrom;
message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发
message.Subject = MessageSubject;
message.Body = MessageBody;
//message.Attachments.Add(objMailAttachment);
message.IsBodyHtml = false; //是否为html格式
message.Priority = MailPriority.High; //发送邮件的优先等级
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.163.com"; //指定发送邮件的服务器地址或IP
sc.Port = 25; //指定发送邮件端口
sc.Credentials = new System.Net.NetworkCredential("jian0487@163.com", "***"); //指定登录服务器的用户名和密码
try
{
sc.Send(message); //发送邮件
}
catch
{
return false;
}
return true;
}
}
//第二个
源代码如下:
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.Mail;
public partial class mail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MailAddress MessageFrom = new MailAddress("jian0487@163.com"); //发件人邮箱地址
string MessageTo = "441232476@qq.com"; //收件人邮箱地址
string MessageSubject = "邮件主题"; //邮件主题
string MessageBody = "这里是邮件内容。"; //邮件内容
if (Send(MessageFrom, MessageTo, MessageSubject, MessageBody))
{
Response.Write("发送邮件成功");
}
else
{
Response.Write("发送邮件失败");
}
}
/// <summary>
/// 发送电子邮件
/// </summary>
/// <param name="MessageFrom">发件人邮箱地址</param>
/// <param name="MessageTo">收件人邮箱地址</param>
/// <param name="MessageSubject">邮件主题</param>
/// <param name="MessageBody">邮件内容</param>
/// <returns></returns>
public bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
{
MailMessage message = new MailMessage();
// if (FileUpload1.PostedFile.FileName != "")
// {
// Attachment att = new Attachment("d://test.txt");//发送附件的内容
// message.Attachments.Add(att);
// }
message.From = MessageFrom;
message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发
message.Subject = MessageSubject;
message.Body = MessageBody;
//message.Attachments.Add(objMailAttachment);
message.IsBodyHtml = false; //是否为html格式
message.Priority = MailPriority.High; //发送邮件的优先等级
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.163.com"; //指定发送邮件的服务器地址或IP
sc.Port = 25; //指定发送邮件端口
sc.Credentials = new System.Net.NetworkCredential("jian0487@163.com", "***"); //指定登录服务器的用户名和密码
try
{
sc.Send(message); //发送邮件
}
catch
{
return false;
}
return true;
}
}