把代码贴出来,供大家参考
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// SendMail 的摘要说明
/// </summary>
public class SendMail
{
/// <summary>
/// 发送邮件,不能发送附件。
/// </summary>
/// <param name="send">发件人地址</param>
/// <param name="recieve">收件人地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="mailbody">邮件内容允许是html</param>
/// <param name="host">发件主机</param>
/// <param name="pwd">密码</param>
public void SendMailUseGmail(string send, string recieve, string subject, string mailbody, string host, string pwd,Page page)
{
try
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = host;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(send, pwd);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(recieve); message.From = new System.Net.Mail.MailAddress(send, "派遣168网", System.Text.Encoding.UTF8);
message.Subject = subject;
message.Body = mailbody;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
//添加附件
//System.Net.Mail.Attachment data = new Attachment(@"附件地址如:e:/a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);
client.Send(message);
System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "success", "alert('邮件发送成功!');", true);
}
catch (Exception ex)
{
System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "eeror", "alert('邮件发送失败!"+ex.Message+"');", true);
}
}
/// <summary>
/// 发送邮件,不能发送附件。
/// </summary>
/// <param name="send">发件人地址</param>
/// <param name="recieve">收件人地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="mailbody">邮件内容允许是html</param>
/// <param name="host">发件主机</param>
/// <param name="sendname">发件人的称呼</param>
/// <param name="pwd">密码</param>
public bool SendMailUseGmail(string send, string recieve, string subject, string mailbody, string host, string pwd,string sendname, Page page)
{
try
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = host;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(send, pwd);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(recieve); message.From = new System.Net.Mail.MailAddress(send, sendname, System.Text.Encoding.UTF8);
message.Subject = subject;
message.Body = mailbody;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
//添加附件
//System.Net.Mail.Attachment data = new Attachment(@"附件地址如:e:/a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);
client.Send(message);
return true;
//System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "success", "alert('邮件发送成功!');", true);
}
catch (Exception ex)
{
return false;
//System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "eeror", "alert('邮件发送失败!" + ex.Message + "');", true);
}
}
}
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// SendMail 的摘要说明
/// </summary>
public class SendMail
{
/// <summary>
/// 发送邮件,不能发送附件。
/// </summary>
/// <param name="send">发件人地址</param>
/// <param name="recieve">收件人地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="mailbody">邮件内容允许是html</param>
/// <param name="host">发件主机</param>
/// <param name="pwd">密码</param>
public void SendMailUseGmail(string send, string recieve, string subject, string mailbody, string host, string pwd,Page page)
{
try
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = host;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(send, pwd);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(recieve); message.From = new System.Net.Mail.MailAddress(send, "派遣168网", System.Text.Encoding.UTF8);
message.Subject = subject;
message.Body = mailbody;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
//添加附件
//System.Net.Mail.Attachment data = new Attachment(@"附件地址如:e:/a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);
client.Send(message);
System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "success", "alert('邮件发送成功!');", true);
}
catch (Exception ex)
{
System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "eeror", "alert('邮件发送失败!"+ex.Message+"');", true);
}
}
/// <summary>
/// 发送邮件,不能发送附件。
/// </summary>
/// <param name="send">发件人地址</param>
/// <param name="recieve">收件人地址</param>
/// <param name="subject">邮件主题</param>
/// <param name="mailbody">邮件内容允许是html</param>
/// <param name="host">发件主机</param>
/// <param name="sendname">发件人的称呼</param>
/// <param name="pwd">密码</param>
public bool SendMailUseGmail(string send, string recieve, string subject, string mailbody, string host, string pwd,string sendname, Page page)
{
try
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = host;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(send, pwd);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(recieve); message.From = new System.Net.Mail.MailAddress(send, sendname, System.Text.Encoding.UTF8);
message.Subject = subject;
message.Body = mailbody;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
//添加附件
//System.Net.Mail.Attachment data = new Attachment(@"附件地址如:e:/a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);
client.Send(message);
return true;
//System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "success", "alert('邮件发送成功!');", true);
}
catch (Exception ex)
{
return false;
//System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), "eeror", "alert('邮件发送失败!" + ex.Message + "');", true);
}
}
}