C # code to send mail (vs2005)

Source: Internet
Author: User
Tags mail tostring trim mailmessage smtpclient
Send an email

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Net;
Using System.Net.Mail;
public partial class Sendemail:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{

}
protected void Button1_Click (object sender, EventArgs e)
{

String s_from = TextBox1.Text.Trim (). ToString ();
String s_to = TextBox3.Text.Trim (). ToString ();
String Pwd=textbox2.text. Trim (). ToString ();
String S_body =textbox5.text.trim (). ToString ();
S_body = S_body. Replace ("\ r \ n", "<br/>");
S_body = S_body. Replace ("\ n", "<br/>");
S_body = S_body. Replace ("", "");

int i = S_from. IndexOf ("@");
String username = S_from. Substring (0,i);


        mailaddress from = new MailAddress (s_from);
        mailaddress to = new MailAddress (s_to);
        MailMessage mailobj = new MailMessage (from, to);
        mailobj. Subject = TextBox4.Text.Trim (). ToString ();
        mailobj. BODY = S_body;
        mailobj. Isbodyhtml = true;
        mailobj. bodyencoding = System.Text.Encoding.GetEncoding ("GB2312");
        mailobj. Priority = Mailpriority.high;

if (FileUpload1.PostedFile.ContentLength > 0)
{
Mailobj. Attachments.Add (new Attachment (FileUpload1.PostedFile.FileName));
}

SmtpClient smtp = new SmtpClient ("smtp.163.com");
Smtp. Host = "smtp.163.com";
Smtp. useDefaultCredentials = false;
Smtp. Credentials = new NetworkCredential (USERNAME,PWD);
Smtp. Deliverymethod = Smtpdeliverymethod.network;

try {
Smtp. Send (Mailobj);
Response.Write ("ok!");

}
Catch

{
Response.Write ("sorry!");
}


}
}

Because the mailbox is 163, so set the SMTP address is 163, if you use another mailbox, as long as the address of the SMTP to change it.





Related Article

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.