Send mail with. Net

Source: Internet
Author: User
Tags mailmessage smtpclient

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Mail;
  7. namespace ConsoleApplication1
  8. {
  9. Class Program
  10. {
  11. static void Main (string[] args)
  12. {
  13. SmtpClient SMTP = new smtpclient (); //instantiation of a smtpclient
  14. Smtp. Deliverymethod = Smtpdeliverymethod.network; //Set the SMTP outbound mode to Network
  15. Smtp. Enablessl = false; Whether the SMTP server enables SSL encryption
  16. Smtp. Host = "smtp.qq.com"; //Specify SMTP server address
  17. Smtp.             Port = 25; //Specifies the port of the SMTP server, which defaults to 25, and if the default port is used, save
  18. //If your SMTP server does not require authentication, use the following method, but there is no need for authentication at this time.
  19. Smtp.  useDefaultCredentials = true;
  20. //If authentication is required, use the following method
  21. Smtp.  Credentials = new NetworkCredential ("[email protected]", "password");
  22. MailMessage mm = new MailMessage (); //instantiation of a message class
  23. Mm. priority = Mailpriority.high; //Mail priority, divided into low, normal, high, usually with normal
  24. Mm.  From = new MailAddress ("[email protected]", "admin", encoding.getencoding (936));
  25. //Recipient sees the source of the message;
  26. //The first parameter is the sender's email address
  27. //The second parameter is the name displayed by the sender .
  28. The third parameter is the encoding used by the second parameter, and if it is specified incorrectly, the message is garbled after it is received
  29. //936 is a codepage value for Simplified Chinese
  30. //Note: The above mail source, must be the same as the account you log in to the mailbox, otherwise it will fail authentication
  31. //mm.  ReplyTo = new MailAddress ("[Email protected]", "My Receive Mailbox", Encoding.GetEncoding (936));
  32. //replyto means the default receive address when replying to a message, i.e. you use one mailbox to send it, but you use the other to collect the letter .
  33. The meaning of the previous two parameters, with the meaning of the from
  34. Mm. Cc.  ADD ("[email protected]");
  35. //E-mail cc, support mass, multiple email addresses separated by commas
  36. //Of course you can use the full address, as follows:
  37. //mm. Cc.  ADD (New MailAddress ("[Email protected]", "cc Person A", encoding.getencoding (936)));
  38. //mm. Cc.  ADD (New MailAddress ("[Email protected]", "cc person B", encoding.getencoding (936)));
  39. //mm. Cc.  ADD (New MailAddress ("[Email protected]", "CC person C", encoding.getencoding (936)));
  40. //mm.  Bcc.add ("[Email protected],[email protected]");
  41. //Mail secret sender, support mass, multiple email addresses separated by commas with half-width
  42. //Of course you can use the full address, as follows:
  43. //mm. Cc.  ADD (New MailAddress ("[Email protected]", "Bcc D", encoding.getencoding (936)));
  44. //mm. Cc.  ADD (New MailAddress ("[Email protected]", "Bcc e", encoding.getencoding (936)));
  45. //mm.  Sender = new MailAddress ("[Email protected]", "Mail Sender", encoding.getencoding (936));
  46. //Can be set arbitrarily, this information is included in the message header, but is not validated and is not displayed to the recipient
  47. //To tell the truth, I don't know what the actual effect, we can not listen to, or do not write this
  48. Mm.  To.add ("[email protected]");
  49. //Mail recipient, support mass, multiple addresses separated by commas with half-width
  50. //can also be added with full address
  51. //mm.  To.add (New MailAddress ("[Email protected]", "Recipient G", encoding.getencoding (936)));
  52. //mm.  To.add (New MailAddress ("[Email protected]", "Receiver H", encoding.getencoding (936)));
  53. Mm. Subject = "This is a test message"; //Mail header
  54. Mm. subjectencoding = encoding.getencoding (936);
  55. //Here is very important, if your mail title contains Chinese, it must be specified here, otherwise the other party received the most likely is garbled.
  56. ///936 is a simplified Chinese pagecode, if the English title, this sentence can be ignored without
  57. Mm. isbodyhtml = true; //Whether the message body is in HTML format
  58. Mm. bodyencoding = encoding.getencoding (936);
  59. //message body encoding, incorrect settings, receiver will receive garbled
  60. Mm.  Body = "<font color= ' Red ' > Mail test, hehe </font>";
  61. //Message body
  62. //mm.  Attachments.Add (New Attachment (@ "D:a.doc", SYSTEM.NET.MIME.MEDIATYPENAMES.APPLICATION.RTF));
  63. //Add attachment, second parameter, file type representing attachment, can not specify
  64. //Can add multiple attachments
  65. //mm.  Attachments.Add (New Attachment (@ "D:b.doc"));
  66. Try
  67. {
  68. Smtp. Send (mm);  //Send the message, and if you do not return an exception, you are done.
  69. }
  70. catch (Exception ex)
  71. {
  72. Console.WriteLine (ex. Message);
  73. }
  74. Console.read ();
  75. }
  76. }
  77. }

Send mail with. Net

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.