Use the. Net program to send the email code

Source: Internet
Author: User
Tags mailmessage smtpclient

On the homepage, the SMTP service must be enabled for the sender's mailbox.

/// <Summary> /// send the email /// </Summary>
/// <Param name = "from"> sender's email address </param>
/// <Param name = "to"> recipient's email address </param>
/// <Param name = "subject"> subject </param>
/// <Param name = "isbodyhtml"> whether it is HTML </param>
/// <Param name = "body"> body </param>
/// <Param name = "smtphost"> SMTP server address </param>
////// <Param name = "username"> email user name </param>
////// <Param name = "password"> email password </param>
/// <Returns> Successful </returns>

Public static bool send (string from, string to, string subject, bool isbodyhtml, string body, string smtphost, string username, string password)
{
String [] Ts = to. Split (',');
Bool issuccess = true;
Foreach (string t in ts)
{
Try
{
Mailmessage Mm = new mailmessage ();
Mm. From = new mailaddress (from );
Mm. to. Add (New mailaddress (T. Trim ()));
Mm. Subject = subject;
Mm. isbodyhtml = isbodyhtml;
Mm. Body = body;
Smtpclient SC = new smtpclient ();
SC. Host = smtphost;
SC. usedefacrecredentials = true; // winform is not affected. Asp.net, false indicates that the strict identity information is not sent.
// Smtpclient. enablessl = true; // if the server does not support SSL, a security connection error is reported.
SC. Credentials = new system. net. networkcredential (username, password );
SC. deliverymethod = smtpdeliverymethod. Network;
SC. Send (mm );
}
Catch
{
Issuccess = false;
}
}
Return issuccess;
}

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.