asp.net邮件发送功能代码
发布时间: 2013年1月11日大家在自己的项目可能会经常用到给用户发送邮件什么,像一些新年祝福、最新技术信息推送等,也是比较实用的功能,话不多说,源代码贴出来同时自己也作为备忘,直接复制过去用就行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
namespace BLL
{
public class send_mail
{
/// <summary>
/// 邮件发送
/// </summary>
/// <param name="toMail">收件方邮件</param>
/// <param name="ccMail">抄送人</param>
/// <param name="bccMail">暗送人</param>
/// <param name="subject">主题</param>
/// <param name="body">邮件体</param>
/// <param name="format">发送格式</param>
/// <param name="serverFileName">附件</param>
/// <returns>是否发送成功</returns>
public bool Send_Mail(string toMail, string ccMail, string bccMail, string subject, string body, SendMark format, string serverFileName)
{
string fromMail = "moppop@126.com";//发送方邮件
string fromPassword = "************";//发送方密码
string host = "smtp.126.com";//smtp服务器
string fromName = "青岛火凤凰网络科技";//发件人
try
{
MailMessage myMail = new MailMessage();
myMail.From = new MailAddress(fromMail, fromName);
myMail.To.Add(new MailAddress(toMail));
if (!string.IsNullOrEmpty(ccMail))
{
myMail.CC.Add(new MailAddress(ccMail));
}
if (!string.IsNullOrEmpty(bccMail))
{
myMail.Bcc.Add(new MailAddress(bccMail));
}
myMail.Subject = subject;
myMail.Body = body;
if (format == SendMark.Html)
{
myMail.IsBodyHtml = true;
}
else
{
myMail.IsBodyHtml = false;
}
//附件
if (!string.IsNullOrEmpty(serverFileName))
{
myMail.Attachments.Add(new Attachment(serverFileName));
}
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(fromMail, fromPassword);
client.Host = host;
client.Send(myMail);
return true;
}
catch
{
return false;
}
}
}
public enum SendMark
{
Html = 1,
Body = 0
}
}
具体页面代码中调用格式如下:
string username ="miaomiao";
string email = “miaomiao@126.com”;
send_mail mail = new send_mail();
string content = @"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>注册激活</title>
</head>
<body>
<table cellspacing='0' border='0' style='font-size:14px; font-family:宋体,arial,verdana,sans-serif;'>
<tr>
<td colspan='2' style='height:30px; font-weight:bold;'>尊敬的用户:</td>
</tr>
<tr>
<td style='width:40px; height:30px;'></td>
<td style=' font-weight:bold;'>您好![NAME]</td>
</tr>
<tr>
<td style='height:30px;'></td>
<td>您申请了火凤凰网用户名,请点击下面的按钮进行操作</td>
</tr>
<tr>
<td style='height:30px;'></td>
<td style='line-height:18px; font-size:12px; padding-top:20px;'>客服中心<br/>[TIME]</td>
</tr>
</table>
</body>
</html>
";
content = content.Replace("[NAME]", username);
content = content.Replace("[TIME]", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
bool b = mail.Send_Mail(email, "", "", "注册激活", content, SendMark.Html, "");
if (b)
{
Response.Write("成功");
}
else {
Response.Write("失败");
}
- 相关文章:
SHTML文件(include)包含出现空行的处理办法 (2013-1-11 8:25:2)
Application,Session,Cookie,ViewState和Cache等对象保存信息的区别 (2011-10-7 12:10:31)
asp.net开源项目学习网站大全(收藏) (2011-3-13 23:1:22)
VS2008常用快捷键 (2011-3-13 21:10:20)
C#将多个连续的空格替换成一个空格 (2011-3-11 23:12:26)
自己写的net程序实现sql数据库导入excel (2011-1-12 0:2:46)
针对 Java 开发人员的 C# 编程语言 (2011-1-11 23:57:19)
.NET 的商业价值 (2011-1-11 23:42:3)
C# 语言小常识 (2011-1-11 23:30:43)
关于"未能加载文件或程序集"解决方法 (2010-8-2 23:13:41)