您的位置:首页 > 编程语言 > ASP

ASP.NET C# 发送邮件

2007-01-27 09:29 363 查看
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.Web.SessionState;
using System.Web.Mail;
using System.Drawing;
using System.ComponentModel;

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

}

protected void Button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mailobj = new MailMessage();
if (emailfrom.Text != "")
{
mailobj.From = emailfrom.Text;
}
if (emailto.Text != "")
{
mailobj.To = emailto.Text;
}
if (emailcc.Text != "")
{
mailobj.Cc = emailcc.Text;
}
if (emailbcc.Text != "")
{
mailobj.Bcc = emailbcc.Text;
}
mailobj.BodyFormat = MailFormat .Text;

mailobj.Priority = MailPriority.Normal;
mailobj.Subject = emailsubject.Text;
mailobj.Body = emailbody.Text;
string strFileName;
strFileName = emailfile.PostedFile.FileName;
if (strFileName != "")
{
mailobj.Attachments.Add(new MailAttachment(strFileName));

}
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(mailobj);
Response.Write("<script>alert('邮件发送成功!')</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('邮件发送失败!')</script>");
}
}
}

mail.aspx

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

<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 693px; height: 338px">
<caption>
发送电子邮件</caption>
<tr>
<td style="width: 213px">
请输入邮件发送地址:</td>
<td style="width: 100px">
<asp:TextBox ID="emailfrom" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 213px">
请输入邮件目的地址:</td>
<td style="width: 100px">
<asp:TextBox ID="emailto" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 213px">
请输入邮件抄送地址:</td>
<td style="width: 100px">
<asp:TextBox ID="emailcc" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 213px">
请输入邮件密送地址:</td>
<td style="width: 100px">
<asp:TextBox ID="emailbcc" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 213px">
请输入邮件主题:</td>
<td style="width: 100px">
<asp:TextBox ID="emailsubject" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
请输入邮件内容:</td>
</tr>
<tr>
<td style="width: 213px">
</td>
<td style="width: 100px">
<asp:TextBox ID="emailbody" runat="server" Height="163px" Width="504px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 213px">
请输入附件:</td>
<td style="width: 100px">
<input id="emailfile" type="file" runat="server"/></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="发送" />
<asp:Button ID="Button2" runat="server" Text="继续发送邮件" /></td>
</tr>
</table>

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