C#. NET instance code for sending mail

Source: Internet
Author: User
Tags bool static class mailmessage net send smtpclient
This article introduces C #. NET send the instance code of the mail, need friend can refer to, want to be helpful to you copy code code as follows:




using System;


using System.Collections.Generic;


using System.Text;


using System.Net.Mail;


using System.Net;


namespace Myquery.utils


{


///<summary>


///Encapsulation Mail handling


///by Jia Shiyi 2011-6-3


///</summary>


public static class Mailhelper


    {


private static string smtphost = null;


private static int smptport = 25;


private static bool Smtpisusercredentials = FALSE;


private static string smtpcredentialaccount = null;


private static string smtpcredentialpassword = null;


///<summary>


///settings Send message Parameters


///</summary>


///<param name= "host" >SMTP server address or name </param>


///<param name= "port" >SMTP service port is generally 25</param>


///<param name= "isusercredentials" > Need certification </param>


///<param name= "account" > Need to authenticate users </param>


<param name= "Password" > user's password </param>
when required for authentication

public static void Setparameters (string host, int port, bool isusercredentials, string account, String PASSW ORD)


        {


SMTPHost = host;


smptport = port;


smtpisusercredentials = isusercredentials;


smtpcredentialaccount = account;


Smtpcredentialpassword = password;


        }


<summary>


///set Send message parameter fetch configuration


///</summary>


private static void Setparameters ()


        {


if (String.IsNullOrEmpty (smtphost))


            {


SMTPHost = Webhelper.getappconfig ("SMTPHost");


Smptport = Datahelper.getintvalue (Webhelper.getappconfig ("Smptport"), 25);


Smtpisusercredentials = constants.true_id. Equals (Webhelper.getappconfig ("smtpisusercredentials"));


Smtpcredentialaccount = Webhelper.getappconfig ("Smtpcredentialaccount");


Smtpcredentialpassword = Webhelper.getappconfig ("Smtpcredentialpassword");


            }


        }


///<summary>


///Send mail send mail error does not throw an exception


///</summary>


<param name= "receivers" > Recipient </param>


///<param name= "title" > Title/Theme </param>


///<param name= "Content" > Letter Contents </param>


///<param name= "sender" > Sender empty Access system configuration </param>


public static void SendMail (string receivers, string title, string content, string sender)


        {


if (! String.IsNullOrEmpty (receivers))


            {


//Initialization Parameters


Setparameters ();


if (! String.IsNullOrEmpty (smtphost))


                {


Try


                    {


smtpclient smtp = new SmtpClient (SMTPHost, Smptport);


if (smtpisusercredentials)


                        {


SMTP. useDefaultCredentials = true;


SMTP. Credentials = new NetworkCredential (Smtpcredentialaccount, Smtpcredentialpassword);


                        }


Smtp. Deliverymethod = Smtpdeliverymethod.network;


if (String.IsNullOrEmpty (sender))


                        {


sender = Smtpcredentialaccount;


                        }


foreach (String receiver in datahelper.getstrings (receivers))


                        {


mailmessage msg = new MailMessage (sender, receiver, title, content);


Msg. bodyencoding = Encoding.UTF8;


Msg. subjectencoding = Encoding.UTF8;


Msg. Isbodyhtml = true;


SMTP. Send (msg);


Msg. Dispose ();


                        }


}


Catch {}


                }


            }


        }


    }


}

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.