Protected void button#click (object sender, EventArgs e)
...{
System. Net. Mail. MailMessage message = new System. Net. Mail. MailMessage ();
// Recipient
Message. To. Add (txtMailAddress. Text );
// CC
Foreach (string mailAddress in txtCopyAddress. Text. Split (','))
...{
If (! String. IsNullOrEmpty (mailAddress ))
...{
Message. CC. Add (mailAddress );
}
}
// Subject
Message. Subject = txtSubject. Text;
Message. From = new System. Net. Mail. MailAddress (fromAddress );
Message. Body = txtMailContents. Text;
// Send the attachment
If (fulAttachment. HasFile)
...{
System. Net. Mail. Attachment attachment = new System. Net. Mail. Attachment (fulAttachment. PostedFile. FileName );
Message. Attachments. Add (attachment );
}
System. Net. Mail. SmtpClient smtp = new System. Net. Mail. SmtpClient (txtSMTPAddress. Text );
Smtp. Credentials = new System. Net. NetworkCredential (txtUserName. Text, txtPassword. Text );
// Smtp. usedefacrecredentials = true;
Try
...{
Smtp. Send (message );
LblReturnMessage. Text = "Mail Send Success! ";
}
Catch (System. Net. Mail. SmtpException ex)
...{
LblReturnMessage. Text = "Send Error:" + ex. Message;
}
}
If "smtp.163.com" is selected for the 163 SMTP server, the sender must be the 163 email user address of the login user.
System. Net. Mail. SmtpClient smtp = new System. Net. Mail. SmtpClient ("smtp.163.com ");
Smtp. Credentials = new System. Net. NetworkCredential ("163 emailaddress", "emailpassword ");
System. Net. Mail. MailMessage message = new System. Net. Mail. MailMessage ();
Message. From = new System. Net. Mail. MailAddress ("163emailaddress@163.com ");
Add web. config
<System.net>
<MailSettings>
<Smtp from = "163emailaddress@163.com">
<Network host = "smtp.163.com" password = "*****" port = "25" userName = "username" defaultCredentials = "false"/>
</Smtp>
</MailSettings>
</System.net>
The following is the VB. NET code.
Dim message As New MailMessage ()
'Recipient
Message. To. Add ("one@21cn.com ")
'Topic
Message. Subject = "use. NET 2.0 to send an email"
Message. From = New MailAddress ("163emailaddress@163.com ")
Message. Body = "very simple"
Dim m_smtpClient As New SmtpClient ("smtp.163.com ")
M_smtpClient.Send (message)