C#. NET send a Message Instance code _ Practical Tips

Source: Internet
Author: User
Tags static class throw exception mailmessage net send smtpclient
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>
Encapsulating Message 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>
Set Send Message Parameters
</summary>
<param name= "host" &GT;SMTP server address or name </param>
<param name= "Port" &GT;SMTP service port is typically 25</param>
<param name= "Isusercredentials" > Need certification </param>
<param name= "Account" > Need to authenticate users </param>
<param name= "Password" > user's password when required for authentication </param>
public static void Setparameters (string host, int port, bool isusercredentials, string account, string password)
{
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 exception
</summary>
<param name= "receivers" > Recipient </param>
<param name= "title" > title/Subject </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.