[Download source code]
Send an email in asp.net 2.0
Author: webabcd
1. Add the following configuration information (host-smtp service address, port-port number, userName-user name, and password-password) to <configuration> in web. config. Please modify it yourself ).
<System.net>
<MailSettings>
<Smtp>
<Network host = "smtpserver" port = "25" userName = "uid" password = "pwd"/>
</Smtp>
</MailSettings>
</System.net>
2. aspx page HTML code <table border = "0">
<Tr>
<Td>
Sender
</Td>
<Td>
<Asp: TextBox runat = "server" ID = "emailfrom"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td>
Recipient
</Td>
<Td>
<Asp: TextBox runat = "server" ID = "emailto"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td>
Topic
</Td>
<Td>
<Asp: TextBox runat = "server" ID = "subject"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td>
Attachment
</Td>
<Td>
<Asp: FileUpload ID = "attachment" runat = "server"/>
</Td>
</Tr>
<Tr>
<Td>
Content
</Td>
<Td>
<Asp: TextBox runat = "server" ID = "body" TextMode = "MultiLine" Columns = "50" Rows = "10"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td colspan = "2" align = "center">
<Asp: Button runat = "server" ID = "btnSend" Text = "send" OnClick = "btnSend_Click"/>
</Td>
</Tr>
</Table>
3. instantiate a MailMessage and set its attributes MailMessage mm = new MailMessage (emailfrom. Text, emailto. Text );
Mm. Subject = subject. Text;
Mm. Body = body. Text;
// HTML format
Mm. IsBodyHtml = true;
// Add an attachment
Mm. Attachments. Add (new Attachment (attachment. PostedFile. InputStream, attachment. FileName ));
/** // * Other attributes such as CC and priority can be set in attributes of the MailMessage class */
4. instantiate a SmtpClient and call its Send method. The parameter is the MailMessage object SmtpClient SC = new SmtpClient ();
// Set smtp in programming mode (without web. config)
// SC. Host = "";
// SC. Port = 25;
// SC. Credentials = new NetworkCredential ("username", "password ");
Try
{
SC. Send (mm );
Response. Write ("OK ");
}
Catch (Exception ex)
{
// Smtp-related errors
If (ex is SmtpException)
{
// Ex. ToString ();
Response. Write ("smtp sending failed ");
}
Else
{
Response. Write (ex. ToString ());
}
}
OK
[Download source code]