Use. NET (C #) to send e-mail learning manuals (with success stories)

Source: Internet
Author: User
Tags mailmessage smtpclient

Use. NET (C #) to send e-mail learning manuals (with success stories)
1. Learn three ways to send mail
2. The example describes the use of the client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis
3. How to set the SMTP server for native IIS
1. Learn three ways to send mail
First: Client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
//Pass the remote SMTP service to send the mail, the network here indicates you want to use the long-range SMTP server.  
Second: client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;  
//via the native SMTP server to send the email, The Pickupdirectoryfromiis here indicates that your mail will be sent through the SMTP server of the native IIS. So if you use this item, be sure to set the address of the server you want to go to on the SMTP service. This is detailed below.  
Third: client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;  
// Indicates that the e-mail message is copied to the directory specified by System.Net.Mail.SmtpDeliveryMethod.PickupDirectorylocation. So that there are other programs to perform the sending of the message.  

2. The example describes using the client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis delivers the message. The code for
(1) mail.aspx is as follows (paste directly):  

  1. <%@ page language="C #" autoeventwireup="true" codefile="Mail.aspx.cs" inherits="Mail"%> /c2>
  2. <! DOCTYPE HTML public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">
  3. "http://www.w3.org/1999/xhtml" >
  4. "Server" >
  5. <title>mail to Users</title>
  6. <body>
  7. <form id="Form1" runat= "Server" >
  8. <div>
  9. <asp:label id="Label1" runat="server" text="Label" ></asp:Label>
  10. </div>
  11. </form>
  12. </body>


(2) The Mail.aspx.cs code is as follows:
Note: General companies are agents of the Internet. So if you use this item. You can only send messages to the intranet.
However, this does not mean that the item cannot send an external network message. But the reason for the agency blockade.

  1. Using System;
  2. Using System.Data;
  3. Using System.Configuration;
  4. Using System.Collections;
  5. Using System.Web;
  6. Using System.Web.Security;
  7. Using System.Web.UI;
  8. Using System.Web.UI.WebControls;
  9. Using System.Web.UI.WebControls.WebParts;
  10. Using System.Web.UI.HtmlControls;
  11. Using System.Net;
  12. Using System.Net.Mail;
  13. Public partial class Mail:System.Web.UI.Page
  14. {
  15. protected void Page_Load (object sender, EventArgs e)
  16. {
  17. //sendmail (sender, recipient, subject, content, host, sender nickname, password, attachment)
  18. SendMail ("[email protected]", "[email protected]", " keynote", "Mail content test", "exhj.yyhj.com.cn", "Sun Festival",  " yyhj", "" ");
  19. }
  20. public void SendMail (string send, string recieve, String subject, String mailbody, String host, String uname, Stri ng pwd, String strfilename)
  21. {
  22. //Generate a client object that uses SMTP to send mail
  23. System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient ();
  24. //Generate a host IP
  25. //client. Port = 25; 587, 465, 995
  26. Client. host = host;
  27. //indicates not to authenticate with the default credentials of the currently logged-on user
  28. Client.  useDefaultCredentials =true;
  29. //include user name and password
  30. if (uname! = "")
  31. {
  32. Client.  Credentials = New System.Net.NetworkCredential (uname, PWD);
  33. }
  34. //Specify how to send e-mail.
  35. Client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;
  36. //Send the message through the native SMTP server,
  37. //In fact, you can set the "host, sender nickname, password", because your IIS server is already set up.  and intra-company email is not required to verify.
  38. System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage ();
  39. Message. To.add (recieve);
  40. Message.  From = new System.Net.Mail.MailAddress (send, uname, System.Text.Encoding.UTF8);
  41. Message. Subject = Subject;
  42. Message. Body = Mailbody;
  43. //define the message body and encode the subject
  44. Message.  bodyencoding = System.Text.Encoding.GetEncoding ("UTF-8");
  45. Message.  subjectencoding = System.Text.Encoding.GetEncoding ("UTF-8");
  46. //Gets or sets a value that indicates whether the e-mail body is HTML.
  47. Message.  isbodyhtml = false;
  48. //Specify message priority
  49. Message. priority = System.Net.Mail.MailPriority.High;
  50. //Add Attachments
  51. //system.net.mail.attachment data = new Attachment (@ "E:\9527\tubu\PA260445.  JPG ", System.Net.Mime.MediaTypeNames.Application.Octet);
  52. if (strFileName! = "" "" && strfilename! = null)
  53. {
  54. Attachment data = new Attachment (strFileName);
  55. Message. Attachments.Add (data);
  56. }
  57. Try
  58. {
  59. //Send
  60. Client. Send (message);
  61. Label1.Text = "sent successfully!"  ";
  62. }
  63. catch (System.Net.Mail.SmtpException ex)
  64. {
  65. Label1.Text ="Send failed:" + ex.  Message;
  66. }
  67. }
  68. }


2. Describes using the client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.Network delivers the message. &NBSP;
Use this item. Your computer must first be directly linked to the extranet. &NBSP;
then direct mail.aspx.cs in Client.deliverymethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis; change to client. Deliverymethod = System.Net.Mail.SmtpDeliveryMethod.Network; &NBSP;
Then set the &NBSP;
//SendMail (sender, recipient, subject, content, host, sender nickname, password, attachment) &NBSP;
SendMail ("[email protected]", "[email protected]", "keynote", "12.37 Mail Content", "smtp.163.com", "Loeley", "81859505", "" "); &NBSP;
Turn from: http://hi.baidu.com/lslyl/blog/item/ ba67366ef4202ddd80cb4afa.html

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.