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 htmlCode
< 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 =NewMailmessage (emailfrom. Text, emailto. Text );
Mm. Subject = subject. text;
Mm. Body = body. text;
// Html format
Mm. isbodyhtml =True;
// Add an attachment
Mm. attachments. Add (NewAttachment (attachment. postedfile. inputstream, attachment. filename ));
/** // * Other attributes such as CC and priority can be set in attributes of the mailmessage class */
4. instantiate an 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 (EXIs Smtpexception)
{
// Ex. tostring ();
Response. Write ("SMTP sending failed");
}
Else
{
Response. Write (ex. tostring ());
}
}