1. Configure the web. config file first:
<System.net>
<MailSettings>
<Smtp from = "Emailname">
<Network host = "smtp.163.com" userName = "Emailname" password = "Emailpassword"
Port = "25" defaultCredentials = "false"/>
</Smtp>
</MailSettings>
</System.net> 2. Compile the function for sending emails:
/** // <Summary>
/// Email sending method (with attachment)
/// </Summary>
/// <Param name = "mailto"> recipient address. For example, mailto: receiver@163.com % 3C/param>
/// <Param name = "mailsubject"> email title </param>
/// <Param name = "mailbody"> email body </param>
/// <Param name = "mailFrom"> email recipient address. For example, mailto: sender@163.com % 3C/param>
/// <Param name = "list"> attachment path </param>
/// <Returns> </returns>
Public bool MySendMail (string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list)
...{
Try
...{
// Email recipient address
System. Net. Mail. MailAddress from = new System. Net. Mail. MailAddress (mailFrom );
// Such as test@163.com, preliminary test, can not use the test@sina.com, with 163 mail server, you must use 163 mailbox User Name
// Recipient address
System. Net. Mail. MailAddress to = new System. Net. Mail. MailAddress (mailto); // such as test@tom.com
System. Net. Mail. MailMessage mail = new System. Net. Mail. MailMessage (from, );
Mail. Subject = mailsubject;
Mail. Body = mailbody;
// Set the server as follows
System. Net. Mail. SmtpClient mySmth = new System. Net. Mail. SmtpClient ();
// Add attachments as follows
Int count = list. Count;
For (int I = 0; I <count; I ++)
...{
System. Net. Mail. Attachment data = new System. Net. Mail. Attachment (list [I]. ToString ());
Mail. Attachments. Add (data );
}
MySmth. Send (mail );
Mail. Dispose ();
Return true;
}
Catch
...{
Return false;
}
}
3. Finally, call the function:
// Automatically send the email
String mailSubject = "Member registration confirmation letter ";
String mailBody = "body content. ";
String mailFrom = ConfigurationManager. receivetask[ "SendMail"];
ArrayList List = new ArrayList ();
List. Add (Server. MapPath (ConfigurationManager. receivetask[ "SendMailText"]);
If (MySendMail(this.txt Email. Text, mailSubject, mailBody, mailFrom, List ))
...{
ShowAndRedirect (this, "the registration confirmation letter has been sent to your mailbox. Please check it! "," Index. aspx ");
}
Else
...{
ShowAndRedirect (this, "email sending failed! "," Index. aspx ");
Return;
}
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/h1jie/archive/2008/01/21/2057212.aspx