Asp. Net Send mail

Source: Internet
Author: User
Tags net send smtpclient

Implement the ability to send messages in ASP., including sending nearby, HTML pages, pictures, and multiple sending objects.

In the actual use of the queue, Threading technology, the purpose is to prevent the process of sending mail to hinder the operation of the main site program.

The specific code is as follows:

 Public BOOLSendEmail ( out stringmessage) {            stringSTR3; BOOLFlag =false; Message=string.            Empty;  This. Mailmm.priority =Mailpriority.normal;  This. Mailmm.bodyencoding =Encoding.UTF8;  This. mailmm.isbodyhtml = This. isbodyhtml;



  

About the picture
AlternateView HtmlView=NULL; Try{HtmlView= Alternateview.createalternateviewfromstring (Strmailcontent,NULL,"text/html"); for(inti =0; i < Aboimgsrclist.count; i++) { Try{Linkedresource logo=NewLinkedresource (Aboimgsrclist[i],"Image/jpeg"); Logo. ContentId="imgsrc"+ (i +1);htmlView.LinkedResources.Add (logo); } Catch(Exception EX) {}} MailMM.AlternateViews.Add (HtmlView);
About Attachments
for(inti =0; I < This. Attachmentslist.count; i++) { stringTempStr = This. Attachmentslist[i]. ToString (); Attachment Attach=NewAttachment (TEMPSTR); This. MAILMM.ATTACHMENTS.ADD (attach); }
} Catch(Exception) {} intCount =0; foreach(stringStrinch This. Arrmailto.keys) { Try { This. MAILMM.TO.ADD (NewMailAddress ( This. Arrmailto[str])); if( This. Strmailtitle! ="") { This. Mailmm.subject = This. Strmailtitle; } This. Mailmm.subjectencoding =Encoding.UTF8; if( This. Mailmm.from = =NULL) { This. Mailmm.from =NewMailAddress ( This. Strsender, ( This. Strsenderdesc = ="") ? This. Strsender: This. Strsenderdesc, Encoding.UTF8); } smtpclient Client=NewSmtpClient {Deliverymethod=Smtpdeliverymethod.network, Enablessl=false, Host= This. MailServer, Port=int. Parse ( This. Smtpport), Credentials=NewNetworkCredential ( This. Strsenderuser, This. Strsenderuserpass)}; Client. Send ( This. MAILMM); MailMM.To.Clear (); Flag=true; Count++; } Catch(Smtpexception exception) {flag=false; Message+="("+count+") "+datetime.now.tostring () +" "+ This. Arrmailto[str] +": "+ Exception. Message +"; \ r \ n"; } Catch(InvalidCastException exc) {flag=false; Message+="("+ Count +") "+ DateTime.Now.ToString () +" "+ This. Arrmailto[str] +": "+ exc. Message +"; \ r \ n"; } Catch(Exception ex) {flag=false; Message+="("+ Count +") "+ DateTime.Now.ToString () +" "+ This. Arrmailto[str] +": "+ ex. Message +"; \ r \ n"; } } returnFlag; }

A thread pool is then opened within the Application_Start method in the global file to traverse the queue of the mailbox, and if there are messages to be sent in the queue, the code that sends the message is automatically called for message delivery.

The code is as follows:

1 protected voidApplication_Start (Objectsender, EventArgs e)2         {3             stringBasePath =AppDomain.CurrentDomain.BaseDirectory;4             //step 2:start A thread to scan email queue (send email)5ThreadPool.QueueUserWorkItem ((o) =6             {7                  while(true)8                 {9                     TryTen                     { One                         if(EmailHelper.mailQueueLR.Count >0) A                         { -                             Lock( This) -                             { the                                 if(EmailHelper.mailQueueLR.Count >0) -                                 { -                                     intTempint =EmailHelper.mailQueueLR.Dequeue (); -                                     if(Tempint = =NULL|| Tempint = =0) +                                     { -                                     } +                                     Else A                                     { at                                       //here is a specific code to create a message, according to the actual situation of writing -                                      } -                                         Catch(Exception ex) -                                         { -                                             //This . Addmsg (Exception. Message); -                                             stringstr =Ex. Message; instr = str +"Str"; -  to                                         } +                                     } -                                 } the                             } *                         } $                         ElsePanax Notoginseng                         { -Thread.Sleep ( +); the                         } +                     } A                     Catch(InvalidOperationException ioex) the                     { +                         Continue; -                     } $                     Catch(Exception ex) $                     { -                         Continue; -                     } the  -                 }Wuyi }, BasePath); the}
View Code

Problems encountered in the actual development

1: Appears in ipad or iphone device, the received message only shows the source code situation.

Cause: When sending a message, it actually sends two copies, one source code, one optional content, and the ipad shows only the source code.

Workaround: put all the HTML code in the variable view (alternateviews) and no longer put it inside the body,

2: In the message display, there is a garbled situation.

Cause: The message Content encoding format is not uniform, for example: the body content and the title encoding format is not uniform

workaround : Uniform encoding format, all strings converted to UTF7 encoded format

Asp. Net Send mail

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.