C # send emails and attachments

Source: Internet
Author: User
Tags mailmessage smtpclient

C # the function of sending emails is found on the internet and socket is also used. The reason why I tried it is that the SMTP server is faulty. Here I use mailmessage and stmp.sohu.com of Sohu. The source code is as follows:

 

Protected void button#click (Object sender, eventargs E)
{

String from = ****** @ sohu.com;
String Fromer = "sender ";
String to = "***** @ 126.com ";
String toer = "recipient ";
String subject = "mail title ";
String file = "attachment address ";
String body = "sent content ";
String smtphost = "smtp.sohu.com ";
String smtpuser = "******* @ sohu.com ";
String smtppass = "*******";
Sendmail (from, Fromer, to, toer, subject, body, file, smtphost, smtpuser, smtppass );
}

 

 

 

 

/// <Summary>
/// C # email sending Function
/// </Summary>
/// <Param name = "from"> sender's email address </param>
/// <Param name = "Fromer"> sender </param>
/// <Param name = "to"> recipient's email </param>
/// <Param name = "toer"> recipient </param>
/// <Param name = "subject"> topic </param>
/// <Param name = "body"> content </param>
/// <Param name = "file"> attachment </param>
/// <Param name = "smtphost"> SMTP server </param>
/// <Param name = "smtpuser"> email </param>
/// <Param name = "smtppass"> password </param>

/// <Returns> </returns>
Public bool Sendmail (string sfrom, string sfromer, string sto, string stoer, string ssubject, string sbody, string sfile, string ssmtphost, string ssmtpuser, string ssmtppass)
{
//// Set the from and to addresses
Mailaddress from = new mailaddress (sfrom, sfromer );
Mailaddress to = new mailaddress (STO, stoer );

//// Create a mailmessage object
Mailmessage omail = new mailmessage (from, );

/// Add an attachment
If (sfile! = "")
{
Omail. attachments. Add (new attachment (sfile ));
}

 

//// Mail title
Omail. Subject = ssubject;

//// Email content
Omail. Body = sbody;

//// Email format
Omail. isbodyhtml = false;

//// Email Encoding
Omail. bodyencoding = system. Text. encoding. getencoding ("gb2312 ");

//// Set the mail priority to high
Omail. Priority = mailpriority. High;

//// Send an email
Smtpclient client = new smtpclient ();
/// Client. usedefacrecredentials = false;
Client. Host = ssmtphost;
Client. Credentials = new networkcredential (ssmtpuser, ssmtppass );
Client. deliverymethod = smtpdeliverymethod. Network;
Try
{
Client. Send (omail );
Return true;
}
Catch (exception ERR)
{
Response. Write (ERR. Message. tostring ());
Return false;
}
Finally
{
//// Release resources
Omail. Dispose ();
}

}

 

 

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.