asp.net C # send a message implementation (show sender name, recipient name)

Source: Internet
Author: User
Tags static class throw exception mailmessage smtpclient


Instance One Mailhelper

The code is as follows Copy Code

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 {}
}
}
}
}
}

Example Two SmtpClient

The code is as follows Copy Code
Creating SmtpClient Objects
System.Net.Mail.SmtpClient client = new SmtpClient ();
Client. Host = "smtp.163.com";//163 SMTP Server is smtp.163.com
String from = "***@163.com";
string pwd = "password";
String Tomail = "* * @qq. com";

Client. useDefaultCredentials = false;
Client. Credentials = new System.Net.NetworkCredential (from, PWD);

Client. Deliverymethod = Smtpdeliverymethod.network;
System.Text.Encoding Encoding = System.Text.Encoding.UTF8;
String senderdisplayname = configurationmanager.appsettings["Senderdisplayname"];//This configuration is the sender's name to be displayed in the message
String recipientsdisplayname = "Hao Yun";//This configuration is the recipient's name to be displayed in the message

MailAddress mailfrom = new MailAddress (from, Senderdisplayname, encoding)//sender's email address, name, encoding UTF8
MailAddress mailto = new MailAddress (Tomail, Recipientsdisplayname, encoding);//Recipient mailbox address, name, encoding UTF8
Creating MailMessage objects
System.Net.Mail.MailMessage message = new MailMessage (mailfrom, mailto);
Message. Subject = "test Message title";
Text default format is HTML
Message. BODY = "Test email content";
Message. Isbodyhtml = true;
Message. bodyencoding = encoding;
Message. subjectencoding = encoding;
Message. headersencoding = encoding;

Client. Send (message);
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.