C # class to send email

Source: Internet
Author: User
Tags tostring mailmessage

Using System;


using System.Collections.Generic;


using System.Text;


using System.Web.Mail;


namespace Baselib


{


public class SendMail


  {


public void Sendtxtmail (string from, String, string to, string subject, mailpriority priority, string body, String smtpserver, System.Collections.ArrayList files)


    {


mailmessage msg = new MailMessage ();


Msg. from = from;


Msg. to = to;


Msg. Subject = Subject;


Msg. Priority = Priority;


Msg. BodyFormat = Mailformat.text;


Msg. BODY = body;


Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); Basic Authentication


Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendusername", from. Substring (0, from. IndexOf ("@")); Set your username here


Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", pass); Set your password here


for (int i = 0; i < files. Count; i++)


      {


if (System.IO.File.Exists (files[i). ToString ())


        {


Msg. Attachments.Add (New MailAttachment (files[i). ToString ()));


        }


      }


smtpmail.smtpserver = SmtpServer;


Try


      {


smtpmail.send (msg);


      }


catch (Exception ex)


      {


      }


    }


public void Sendhtmlmail (string from, String, string to, string subject, mailpriority priority, string body, string s Mtpserver, System.Collections.ArrayList files)


    {


mailmessage msg = new MailMessage ();


Msg. from = from;


Msg. to = to;


Msg. Subject = Subject;


Msg. Priority = Priority;


Msg. BodyFormat = mailformat.html;


Msg. BODY = body;


Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); Basic Authentication


Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendusername", from. Substring (0, from. IndexOf ("@")); Set your username here


Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", pass); Set your password here


for (int i = 0; i < files. Count; i++)


      {


if (System.IO.File.Exists (files[i). ToString ())


        {


Msg. Attachments.Add (New MailAttachment (files[i). ToString ()));


        }


      }


smtpmail.smtpserver = SmtpServer;


smtpmail.send (msg);


    }


  }


}

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.