NET 2.0 sends EMail

Source: Internet
Author: User
Tags mailmessage smtpclient
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.