您的位置:首页 > 大数据 > 人工智能

使用System.Net.Mail发送电子邮件

2011-01-17 15:06 573 查看
Send.aspx.cs

 

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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSend_Click(object sender, EventArgs e)
{
//// here just simulating a registration process.
////after a user register in the website and email notification will goes to him containing the regsitreation details,

//// creating the class of the email notifiction
//EmaislNotifications en = new EmaislNotifications("Registration", ConfigurationManager.AppSettings["EmailTemplatesPath"]);

////en.DebugMode = true;
////Literal1.Text = en.SendNotificationEmail("register.txt", txtRecepEmail.Text, txtSenderName.Text, txtSenderEmail.Text, null, txtSubject.Text, ConfigurationManager.AppSettings["CompanyLogoURL"], DateTime.Now.ToString("dddd dd, MMMM, yyyy"), txtSenderName.Text, "ucion", "991001");

////this will send an email
//en.SendNotificationEmail("register.txt", "gaoxu529@126.com", "Demo", "gaoxu529@126.com", null, "Registration", ConfigurationManager.AppSettings["CompanyLogoURL"], DateTime.Now.ToString("dddd dd, MMMM, yyyy"), "ucion", "ucion", "991001");

try
{
// creating email message
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;// email body will allow html elements

// setting the Sender Email ID
msg.From = new MailAddress(ConfigurationManager.AppSettings["SenderEmail"]);

// adding the Recepient Email ID
string[] recepEmailList = txtRecepEmail.Text.Split(',');
foreach (string recepEmail in recepEmailList)
{
msg.To.Add(recepEmail);
}

// add CC email ids if supplied.
//if (!string.IsNullOrEmpty(cc))
//    msg.CC.Add(cc);

//setting email subject and body
msg.Subject = "这是我的测试邮件!";
msg.Body = "这是我的测试邮件!";

//create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
SmtpClient SmtpMail = new SmtpClient();
SmtpMail.Host = "smtp.126.com";
SmtpMail.Port = 25;
SmtpMail.Credentials = new System.Net.NetworkCredential("gaoxu529", "gaoxu-529");

// sending the message.
SmtpMail.Send(msg);
lblMsg.Text = "邮件发送成功!";
}
catch
{
lblMsg.Text = "邮件发送失败!";
}
}
}


 

Send.aspx

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Send.aspx.cs" Inherits="_Default" %>

<mce:style type="text/css"><!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
--></mce:style><style type="text/css" mce_bogus="1">body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}</style>
<mce:script type="text/javascript"><!--
function $(id)
{
return document.getElementById(id);
}
function checkForm()
{
var txtSenderEmail = $("txtRecepEmail").value;
if (txtSenderEmail == "")
{
alert("收件人邮箱不能为空!");
return ;
}

$("btnSend").click();
}
// --></mce:script>
<form action="" method="post" runat=server  name="form1">
<table width="100%" height="100%"  border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="80"><div align="center">
<h1>邮件发送测试</h1>
</div></td>
</tr>
<tr>
<td align="center" valign="top" style="height: 78px"><table width="80%" height="100%"  border="0" cellpadding="0" cellspacing="5">
<tr>
<td height="30" align="right" style="width: 559px"></td>
<td height="30" style="width: 995px">
</td>
</tr>
<tr>
<td align="right"  style="width: 559px; height: 28px;">
</td>
<td align="left"  style="width: 995px; height: 28px;">
<asp:label id="lblMsg" runat="server" forecolor="Red"></asp:label>
</td>
</tr>
<tr>
<td align="right" style="width: 559px; height: 30px;">收件人:</td>
<td style="width: 995px; height: 30px;">
<asp:textbox id="txtRecepEmail" runat="server" width="408px"></asp:textbox>
</td>
</tr>
<tr>
<td height="30" align="right" style="width: 559px"></td>
<td height="30" style="width: 995px">
</td>
</tr>
<tr>
<td align="right" style="width: 559px; height: 7px;"> </td>
<td style="width: 995px; height: 7px;">
 
<asp:button id="btnSend" runat="server" onclick="btnSend_Click" text="发送" width="0" />
<input id="hbtnSend" type="button" value="发送" onclick="checkForm()" /></td>
</tr>
</table></td>
</tr>
<tr>
<td valign=top align=center>
<asp:literal id="Literal1" runat="server"></asp:literal>
</td>
</tr>
</table>
</form>


 

web.config

 

<?xml version="1.0"?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
/Windows/Microsoft.Net/Framework/v2.x/Config 中
-->
<configuration>

<appSettings>
<add key ="EmailTemplatesPath" value ="C:/temp/"/>
<add key ="CompanyLogoURL" value ="http://www.ucion.com/favicon.ico"/>
<add key="SenderEmail" value="gaoxu529@126.com"/>
</appSettings>
<connectionStrings></connectionStrings>
<system.web>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation debug="true"/>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<authentication mode="Windows"/>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
<system.net>
<mailSettings>
<smtp from="gaoxu529@126.com">
<network host="smtp.126.com" password="gaoxu-529" port="25" userName="gaoxu529" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>

</configuration>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息