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

邮件发送示例代码

2008-10-09 22:04 471 查看
 
<%...@ Import Namespace="System.Web.Mail" %>




<%...@ Page language="c#" AutoEventWireup="true" %>

DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<title>WebForm1title>



<scriptlanguage="C#" runat="server">...





void Page_Load(Object sender, EventArgs e) ...{

this.ddlMailFormat.Items.Add( new ListItem( System.Enum.GetName(typeof(System.Web.Mail.MailFormat), MailFormat.Text), MailFormat.Text.ToString()));

this.ddlMailFormat.Items.Add( new ListItem( System.Enum.GetName(typeof(System.Web.Mail.MailFormat), MailFormat.Html), MailFormat.Html.ToString()));

}





void btnSend_Click(object sender, System.EventArgs e) ...{



try ...{

MailMessage Message = new MailMessage();





if( this.chkRequireLogin.Checked ) ...{

Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" ); //basic authentication

Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", txtUserName.Text ); //set your username here

Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", txtPassword.Text ); //set your password here

}

Message.To = txtTo.Text;

Message.Cc = txtCC.Text;

Message.Bcc = txtBCC.Text;

Message.From = txtFrom.Text;

Message.Subject = txtSubject.Text;

Message.BodyFormat = (MailFormat)System.Enum.Parse(typeof(MailFormat), ddlMailFormat.SelectedValue );

Message.Body = txtMessage.Text;



SmtpMail.SmtpServer = txtServer.Text;

SmtpMail.Send(Message);



lblResult.Text = "Mail Sent";

}



catch( Exception ex ) ...{

lblResult.Text = ex.ToString();

}

}



script>

<metacontent="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

<metacontent="C#" name="CODE_LANGUAGE">

<metacontent="JavaScript" name="vs_defaultClientScript">

<metacontent="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

HEAD>

<bodyMS_POSITIONING="GridLayout">

<formid="Form1" method="post" runat="server">

<asp:Labelid="lblServer" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server"

Width="104px">Server:asp:Label>

<asp:Labelid="lblMessage" style="Z-INDEX: 121; LEFT: 16px; POSITION: absolute; TOP: 280px"

runat="server" Width="104px">Message:asp:Label>

<asp:Labelid="lblMailFormat" style="Z-INDEX: 120; LEFT: 16px; POSITION: absolute; TOP: 248px"

runat="server" Width="104px">Mail Format:asp:Label>

<asp:Labelid="lblSubject" style="Z-INDEX: 119; LEFT: 16px; POSITION: absolute; TOP: 224px"

runat="server" Width="104px">Subject:asp:Label>

<asp:Labelid="lblBCC" style="Z-INDEX: 118; LEFT: 16px; POSITION: absolute; TOP: 200px" runat="server"

Width="104px">BCC:asp:Label>

<asp:Labelid="lblCC" style="Z-INDEX: 117; LEFT: 16px; POSITION: absolute; TOP: 176px" runat="server"

Width="104px">CC:asp:Label>

<asp:TextBoxid="txtSubject" style="Z-INDEX: 114; LEFT: 128px; POSITION: absolute; TOP: 224px"

tabIndex="9" runat="server">Test of CDONTasp:TextBox>

<asp:TextBoxid="txtBCC" style="Z-INDEX: 113; LEFT: 128px; POSITION: absolute; TOP: 200px" tabIndex="8"

runat="server">asp:TextBox>

<asp:TextBoxid="txtCC" style="Z-INDEX: 112; LEFT: 128px; POSITION: absolute; TOP: 176px" tabIndex="7"

runat="server">asp:TextBox>

<asp:TextBoxid="txtTo" style="Z-INDEX: 111; LEFT: 128px; POSITION: absolute; TOP: 152px" tabIndex="6"

runat="server">asp:TextBox>

<asp:TextBoxid="txtFrom" style="Z-INDEX: 110; LEFT: 128px; POSITION: absolute; TOP: 128px" tabIndex="5"

runat="server">asp:TextBox>

<asp:TextBoxid="txtPassword" style="Z-INDEX: 109; LEFT: 152px; POSITION: absolute; TOP: 88px"

tabIndex="4" runat="server" Width="128px" TextMode="Password">asp:TextBox>

<asp:TextBoxid="txtUserName" style="Z-INDEX: 108; LEFT: 152px; POSITION: absolute; TOP: 64px"

tabIndex="3" runat="server" Width="128px">asp:TextBox>

<asp:Labelid="lblTo" style="Z-INDEX: 106; LEFT: 16px; POSITION: absolute; TOP: 152px" runat="server"

Width="104px">To:asp:Label>

<asp:Labelid="lblFrom" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 128px" runat="server"

Width="104px">From:asp:Label>

<asp:Labelid="lblPassword" style="Z-INDEX: 104; LEFT: 40px; POSITION: absolute; TOP: 88px"

runat="server" Width="104px">Password:asp:Label>

<asp:Labelid="lblUserName" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 64px"

runat="server" Width="104px">User Name:asp:Label>

<asp:CheckBoxid="chkRequireLogin" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 40px"

runat="server" Width="232px" Text="Server Requires Login" tabIndex="2">asp:CheckBox>

<asp:TextBoxid="txtServer" style="Z-INDEX: 107; LEFT: 128px; POSITION: absolute; TOP: 16px"

runat="server" tabIndex="1">asp:TextBox>

<asp:DropDownListid="ddlMailFormat" style="Z-INDEX: 115; LEFT: 128px; POSITION: absolute; TOP: 248px"

tabIndex="10" runat="server" Width="152px">asp:DropDownList>

<asp:TextBoxid="txtMessage" style="Z-INDEX: 116; LEFT: 16px; POSITION: absolute; TOP: 304px"

tabIndex="11" runat="server" Width="280px" Height="136px" Rows="20" MaxLength="255">This is a test of CDONT Functionality.asp:TextBox>

<asp:Buttonid="btnSend" style="Z-INDEX: 122; LEFT: 240px; POSITION: absolute; TOP: 456px" tabIndex="12"

runat="server" Text="Send">asp:Button>

<asp:Labelid="lblResult" style="Z-INDEX: 123; LEFT: 24px; POSITION: absolute; TOP: 496px"

runat="server" Width="100%" Height="200px">asp:Label>

form>

body>

HTML>

 

 

 

 

--------------------------

public static void SendEmail(string mailFrom,string mailTo,string mailSubject ,string mailBody,string mailCCTo,string mailBccTo)
{
MailAddress from = new MailAddress(mailFrom);
MailAddress to = new MailAddress(mailTo);
MailMessage message = new MailMessage(from ,to);
message.Subject = mailSubject;
message.Body = mailBody;
MailMessage MyMessage = new MailMessage();
MailAddress bcc = new MailAddress(mailBccTo);
message.Bcc.Add(bcc);
MailAddress CC = new MailAddress(mailCCTo);
message.CC.Add(CC);
SmtpClient client = new SmtpClient("smtp-out.us.dell.com");
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);

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