其实大同小异,记录下

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Mail;
  6. using System.Net.Mime;
  7. using System.IO;
  8. using System.ComponentModel;
  9. namespace Mail
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15.  
  16. }
  17. static void PlainText()
  18. {
  19. //create the mail message
  20. MailMessage mail = new MailMessage();
  21.  
  22. //set the addresses
  23. mail.From = new MailAddress("me@mycompany.com");
  24. mail.To.Add("you@yourcompany.com");
  25.  
  26. //set the content
  27. mail.Subject = "This is an email";
  28. mail.Body = "this is the body content of the email.";
  29.  
  30. //send the message
  31. SmtpClient smtp = new SmtpClient("127.0.0.1");
  32. smtp.Send(mail);
  33. }
  34. static void HtmlEmail()
  35. {
  36. //create the mail message
  37. MailMessage mail = new MailMessage();
  38.  
  39. //set the addresses
  40. mail.From = new MailAddress("me@mycompany.com");
  41. mail.To.Add("you@yourcompany.com");
  42.  
  43. //set the content
  44. mail.Subject = "This is an email";
  45. mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
  46. mail.IsBodyHtml = true;
  47.  
  48. //send the message
  49. SmtpClient smtp = new SmtpClient("127.0.0.1");
  50. smtp.Send(mail);
  51. }
  52.  
  53. static void MultiPartMime()
  54. {
  55. //create the mail message
  56. MailMessage mail = new MailMessage();
  57.  
  58. //set the addresses
  59. mail.From = new MailAddress("me@mycompany.com");
  60. mail.To.Add("you@yourcompany.com");
  61.  
  62. //set the content
  63. mail.Subject = "This is an email";
  64.  
  65. //first we create the Plain Text part
  66. AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don\'t support html", null, "text/plain");
  67. //then we create the Html part
  68. AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by those mail clients that support html</b>", null, "text/html");
  69. mail.AlternateViews.Add(plainView);
  70. mail.AlternateViews.Add(htmlView);
  71.  
  72.  
  73. //send the message
  74. SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
  75. smtp.Send(mail);
  76. }
  77.  
  78. static void FriendlyFromName()
  79. {
  80. //create the mail message
  81. MailMessage mail = new MailMessage();
  82.  
  83. //set the addresses
  84. //to specify a friendly \'from\' name, we use a different ctor
  85. mail.From = new MailAddress("me@mycompany.com", "Steve James" );
  86. mail.To.Add("you@yourcompany.com");
  87.  
  88. //set the content
  89. mail.Subject = "This is an email";
  90. mail.Body = "this is the body content of the email.";
  91.  
  92. //send the message
  93. SmtpClient smtp = new SmtpClient("127.0.0.1");
  94. smtp.Send(mail);
  95.  
  96. }
  97.  
  98. static void FriendlyToName()
  99. {
  100. //create the mail message
  101. MailMessage mail = new MailMessage();
  102.  
  103. //set the addresses
  104. //to specify a friendly \'from\' name, we use a different ctor
  105. mail.From = new MailAddress("me@mycompany.com", "Steve James");
  106.  
  107. //since the To,Cc, and Bcc accept addresses, we can use the same technique as the From address
  108. mail.To.Add( new MailAddress( "you@yourcompany.com", "Beth Jones") );
  109. mail.CC.Add(new MailAddress("donna@yourcompany.com", "Donna Summers"));
  110. mail.Bcc.Add(new MailAddress("bob@yourcompany.com", "Bob Smith"));
  111.  
  112. //set the content
  113. mail.Subject = "This is an email";
  114. mail.Body = "this is the body content of the email.";
  115.  
  116. //send the message
  117. SmtpClient smtp = new SmtpClient("127.0.0.1");
  118. smtp.Send(mail);
  119. }
  120.  
  121. static void MultipleRecipients()
  122. {
  123. //create the mail message
  124. MailMessage mail = new MailMessage();
  125.  
  126. //set the addresses
  127. //to specify a friendly \'from\' name, we use a different ctor
  128. mail.From = new MailAddress("me@mycompany.com", "Steve James");
  129.  
  130. //since the To,Cc, and Bcc accept addresses, we can use the same technique as the From address
  131. //since the To, Cc, and Bcc properties are collections, to add multiple addreses, we simply call .Add(...) multple times
  132. mail.To.Add("you@yourcompany.com");
  133. mail.To.Add("you2@yourcompany.com");
  134. mail.CC.Add("cc1@yourcompany.com");
  135. mail.CC.Add("cc2@yourcompany.com");
  136. mail.Bcc.Add("blindcc1@yourcompany.com");
  137. mail.Bcc.Add("blindcc2@yourcompany.com");
  138.  
  139. //set the content
  140. mail.Subject = "This is an email";
  141. mail.Body = "this is the body content of the email.";
  142.  
  143. //send the message
  144. SmtpClient smtp = new SmtpClient("127.0.0.1");
  145. smtp.Send(mail);
  146. }
  147.  
  148. static void FriendlyNonAsciiName()
  149. {
  150. //create the mail message
  151. MailMessage mail = new MailMessage();
  152.  
  153. //set the addresses
  154. //to specify a friendly non ascii name, we use a different ctor.
  155. //A ctor that accepts an encoding that matches the text of the name
  156. mail.From = new MailAddress("me@mycompany.com", "Steve 豣irk", Encoding.GetEncoding( "iso-8859-1"));
  157. mail.To.Add("you@yourcompany.com");
  158.  
  159. //set the content
  160. mail.Subject = "This is an email";
  161. mail.Body = "this is the body content of the email.";
  162.  
  163. //send the message
  164. SmtpClient smtp = new SmtpClient("127.0.0.1");
  165. smtp.Send(mail);
  166.  
  167. }
  168.  
  169. static void SetPriority()
  170. {
  171. //create the mail message
  172. MailMessage mail = new MailMessage();
  173.  
  174. //set the addresses
  175. mail.From = new MailAddress("me@mycompany.com");
  176. mail.To.Add("you@yourcompany.com");
  177.  
  178. //set the content
  179. mail.Subject = "This is an email";
  180. mail.Body = "this is the body content of the email.";
  181.  
  182. //specify the priority of the mail message
  183. mail.Priority = MailPriority.High;
  184.  
  185. //send the message
  186. SmtpClient smtp = new SmtpClient("127.0.0.1");
  187. smtp.Send(mail);
  188. }
  189.  
  190. static void SetTheReplyToHeader()
  191. {
  192. //create the mail message
  193. MailMessage mail = new MailMessage();
  194.  
  195. //set the addresses
  196. mail.From = new MailAddress("me@mycompany.com");
  197. mail.To.Add("you@yourcompany.com");
  198.  
  199. //set the content
  200. mail.Subject = "This is an email";
  201. mail.Body = "this is the body content of the email.";
  202.  
  203. //specify the priority of the mail message
  204. mail.ReplyTo = new MailAddress("SomeOtherAddress@mycompany.com");
  205.  
  206. //send the message
  207. SmtpClient smtp = new SmtpClient("127.0.0.1");
  208. smtp.Send(mail);
  209. }
  210.  
  211. static void CustomHeaders()
  212. {
  213. //create the mail message
  214. MailMessage mail = new MailMessage();
  215.  
  216. //set the addresses
  217. mail.From = new MailAddress("me@mycompany.com");
  218. mail.To.Add("you@yourcompany.com");
  219.  
  220. //set the content
  221. mail.Subject = "This is an email";
  222. mail.Body = "this is the body content of the email.";
  223.  
  224. //to add custom headers, we use the Headers.Add(...) method to add headers to the
  225. //.Headers collection
  226. mail.Headers.Add("X-Company", "My Company");
  227. mail.Headers.Add("X-Location", "Hong Kong");
  228.  
  229.  
  230. //send the message
  231. SmtpClient smtp = new SmtpClient("127.0.0.1");
  232. smtp.Send(mail);
  233. }
  234. static void ReadReceipts()
  235. {
  236. //create the mail message
  237. MailMessage mail = new MailMessage();
  238.  
  239. //set the addresses
  240. mail.From = new MailAddress("me@mycompany.com");
  241. mail.To.Add("you@yourcompany.com");
  242.  
  243. //set the content
  244. mail.Subject = "This is an email";
  245. mail.Body = "this is the body content of the email.";
  246.  
  247. //To request a read receipt, we need add a custom header named \'Disposition-Notification-To\'
  248. //in this example, read receipts will go back to \'someaddress@mydomain.com\'
  249. //it\'s important to note that read receipts will only be sent by those mail clients that
  250. //a) support them
  251. //and
  252. //b)have them enabled.
  253. mail.Headers.Add("Disposition-Notification-To", "<someaddress@mydomain.com>");
  254.  
  255.  
  256. //send the message
  257. SmtpClient smtp = new SmtpClient("127.0.0.1");
  258. smtp.Send(mail);
  259. }
  260.  
  261. static void AttachmentFromFile()
  262. {
  263. //create the mail message
  264. MailMessage mail = new MailMessage();
  265.  
  266. //set the addresses
  267. mail.From = new MailAddress("me@mycompany.com");
  268. mail.To.Add("you@yourcompany.com");
  269.  
  270. //set the content
  271. mail.Subject = "This is an email";
  272. mail.Body = "this content is in the body";
  273.  
  274. //add an attachment from the filesystem
  275. mail.Attachments.Add(new Attachment("c:\\temp\\example.txt"));
  276.  
  277. //to add additional attachments, simply call .Add(...) again
  278. mail.Attachments.Add(new Attachment("c:\\temp\\example2.txt"));
  279. mail.Attachments.Add(new Attachment("c:\\temp\\example3.txt"));
  280.  
  281. //send the message
  282. SmtpClient smtp = new SmtpClient("127.0.0.1");
  283. smtp.Send(mail);
  284.  
  285. }
  286.  
  287. static void AttachmentFromStream()
  288. {
  289.  
  290. //create the mail message
  291. MailMessage mail = new MailMessage();
  292.  
  293. //set the addresses
  294. mail.From = new MailAddress("me@mycompany.com");
  295. mail.To.Add("you@yourcompany.com");
  296.  
  297. //set the content
  298. mail.Subject = "This is an email";
  299. mail.Body = "this content is in the body";
  300.  
  301. //Get some binary data
  302. byte[] data = GetData();
  303. //save the data to a memory stream
  304. MemoryStream ms = new MemoryStream(data);
  305.  
  306. //create the attachment from a stream. Be sure to name the data with a file and
  307. //media type that is respective of the data
  308. mail.Attachments.Add( new Attachment( ms, "example.txt", "text/plain" ));
  309.  
  310. //send the message
  311. SmtpClient smtp = new SmtpClient("127.0.0.1");
  312. smtp.Send(mail);
  313. }
  314. static byte[] GetData()
  315. {
  316. //this method just returns some binary data.
  317. //it could come from anywhere, such as Sql Server
  318. string s = "this is some text";
  319. byte[] data = Encoding.ASCII.GetBytes(s);
  320. return data;
  321. }
  322.  
  323. static void LoadFromConfig(){
  324. //the from address, along with the server properties will be set in the app.config,
  325. //thus we don\'t need to specify them in code
  326.  
  327. //create the mail message
  328. MailMessage mail = new MailMessage();
  329.  
  330. mail.To.Add("you@yourcompany.com");
  331.  
  332. //set the content
  333. mail.Subject = "This is an email";
  334. mail.Body = "this is the body content of the email.";
  335.  
  336. //send the message
  337. SmtpClient smtp = new SmtpClient();
  338. smtp.Send(mail);
  339.  
  340. }
  341.  
  342. static void Authenticate()
  343. {
  344. //create the mail message
  345. MailMessage mail = new MailMessage();
  346.  
  347. //set the addresses
  348. mail.From = new MailAddress("me@mycompany.com");
  349. mail.To.Add("you@yourcompany.com");
  350.  
  351. //set the content
  352. mail.Subject = "This is an email";
  353. mail.Body = "this is the body content of the email.";
  354.  
  355. //send the message
  356. SmtpClient smtp = new SmtpClient("127.0.0.1");
  357.  
  358. //to authenticate we set the username and password properites on the SmtpClient
  359. smtp.Credentials = new NetworkCredential("username", "secret");
  360. smtp.Send(mail);
  361.  
  362. }
  363.  
  364. static void ChangePort()
  365. {
  366. //create the mail message
  367. MailMessage mail = new MailMessage();
  368.  
  369. //set the addresses
  370. mail.From = new MailAddress("me@mycompany.com");
  371. mail.To.Add("you@yourcompany.com");
  372.  
  373. //set the content
  374. mail.Subject = "This is an email";
  375. mail.Body = "this is the body content of the email.";
  376.  
  377. //send the message
  378. SmtpClient smtp = new SmtpClient("127.0.0.1");
  379.  
  380. //to change the port (default is 25), we set the port property
  381. smtp.Port = 587;
  382. smtp.Send(mail);
  383. }
  384.  
  385. static void EmbedImages()
  386. {
  387. //create the mail message
  388. MailMessage mail = new MailMessage();
  389.  
  390. //set the addresses
  391. mail.From = new MailAddress("me@mycompany.com");
  392. mail.To.Add("you@yourcompany.com");
  393.  
  394. //set the content
  395. mail.Subject = "This is an email";
  396.  
  397. //first we create the Plain Text part
  398. AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don\'t support html", null, "text/plain");
  399.  
  400. //then we create the Html part
  401. //to embed images, we need to use the prefix \'cid\' in the img src value
  402. //the cid value will map to the Content-Id of a Linked resource.
  403. //thus <img src=\'cid:companylogo\'> will map to a LinkedResource with a ContentId of \'companylogo\'
  404. AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");
  405.  
  406. //create the LinkedResource (embedded image)
  407. LinkedResource logo = new LinkedResource( "c:\\temp\\logo.gif" );
  408. logo.ContentId = "companylogo";
  409. //add the LinkedResource to the appropriate view
  410. htmlView.LinkedResources.Add(logo);
  411. //add the views
  412. mail.AlternateViews.Add(plainView);
  413. mail.AlternateViews.Add(htmlView);
  414.  
  415.  
  416. //send the message
  417. SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
  418. smtp.Send(mail);
  419. }
  420.  
  421. static void SSL()
  422. {
  423. //create the mail message
  424. MailMessage mail = new MailMessage();
  425.  
  426. //set the addresses
  427. mail.From = new MailAddress("me@mycompany.com");
  428. mail.To.Add("you@yourcompany.com");
  429.  
  430. //set the content
  431. mail.Subject = "This is an email";
  432. mail.Body = "this is the body content of the email.";
  433.  
  434. //Port 587 is another SMTP port
  435. SmtpClient smtp = new SmtpClient("127.0.01", 587);
  436. smtp.EnableSsl = true;
  437. smtp.Send(mail);
  438. }
  439.  
  440. static void SendAsync()
  441. {
  442. //create the mail message
  443. MailMessage mail = new MailMessage();
  444.  
  445. //set the addresses
  446. mail.From = new MailAddress("me@mycompany.com");
  447. mail.To.Add("you@yourcompany.com");
  448.  
  449. //set the content
  450. mail.Subject = "This is an email";
  451. mail.Body = "this is the body content of the email.";
  452.  
  453. //send the message
  454. SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
  455. //the userstate can be any object. The object can be accessed in the callback method
  456. //in this example, we will just use the MailMessage object.
  457. object userState = mail;
  458.  
  459. //wire up the event for when the Async send is completed
  460. smtp.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);
  461.  
  462. smtp.SendAsync( mail, userState );
  463. }
  464. public static void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
  465. {
  466. //Get the Original MailMessage object
  467. MailMessage mail= (MailMessage)e.UserState;
  468.  
  469. //write out the subject
  470. string subject = mail.Subject;
  471.  
  472. if (e.Cancelled)
  473. {
  474. Console.WriteLine("Send canceled for mail with subject [{0}].", subject);
  475. }
  476. if (e.Error != null)
  477. {
  478. Console.WriteLine("Error {1} occurred when sending mail [{0}] ", subject, e.Error.ToString());
  479. }
  480. else
  481. {
  482. Console.WriteLine("Message [{0}] sent.", subject );
  483. }
  484. }
  485.  
  486. public static void PickupDirectory()
  487. {
  488. //create the mail message
  489. MailMessage mail = new MailMessage();
  490.  
  491. //set the addresses
  492. mail.From = new MailAddress("me@mycompany.com");
  493. mail.To.Add("you@yourcompany.com");
  494.  
  495. //set the content
  496. mail.Subject = "This is an email";
  497. mail.Body = "this is the body content of the email.";
  498.  
  499. //if we are using the IIS SMTP Service, we can write the message
  500. //directly to the PickupDirectory, and bypass the Network layer
  501. SmtpClient smtp = new SmtpClient();
  502. smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
  503. smtp.Send(mail);
  504. }
  505.  
  506. public static void EmailWebPage()
  507. {
  508. //create the mail message
  509. MailMessage mail = new MailMessage();
  510.  
  511. //set the addresses
  512. mail.From = new MailAddress("me@mycompany.com");
  513. mail.To.Add("you@yourcompany.com");
  514.  
  515. //set the content
  516. mail.Subject = "This is an email";
  517.  
  518. //screen scrape the html
  519. string html = ScreenScrapeHtml("http://localhost/example.htm");
  520. mail.Body = html;
  521. mail.IsBodyHtml = true;
  522.  
  523. //send the message
  524. SmtpClient smtp = new SmtpClient("127.0.0.1");
  525. smtp.Send(mail);
  526.  
  527. }
  528. public static string ScreenScrapeHtml(string url)
  529. {
  530. WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
  531. StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
  532. string result = sr.ReadToEnd();
  533. sr.Close();
  534. return result;
  535. }
  536.  
  537. public static void NonAsciiMail()
  538. {
  539. //create the mail message
  540. MailMessage mail = new MailMessage();
  541.  
  542. //set the addresses
  543. mail.From = new MailAddress("me@mycompany.com");
  544. mail.To.Add("you@yourcompany.com");
  545.  
  546. //set the content
  547. mail.Subject = "This is an email";
  548.  
  549. //to send non-ascii content, we need to set the encoding that matches the
  550. //string characterset.
  551. //In this example we use the ISO-8859-1 characterset
  552. mail.Body = "this text has some ISO-8859-1 characters: 庖涨";
  553. mail.BodyEncoding = Encoding.GetEncoding("iso-8859-1");
  554.  
  555. //send the message
  556. SmtpClient smtp = new SmtpClient("127.0.0.1");
  557. smtp.Send(mail);
  558.  
  559. }
  560.  
  561. public static void InnerExceptions()
  562. {
  563. //create the mail message
  564. MailMessage mail = new MailMessage();
  565.  
  566. //set the addresses
  567. mail.From = new MailAddress("me@mycompany.com");
  568. mail.To.Add("him@hiscompany.com");
  569.  
  570. //set the content
  571. mail.Subject = "This is an email";
  572. mail.Body = "this is the body content of the email.";
  573.  
  574. //send the message
  575. SmtpClient smtp = new SmtpClient("127.0.0.1");
  576. try
  577. {
  578. smtp.Send(mail);
  579. }
  580. catch (Exception ex)
  581. {
  582. Exception ex2 = ex;
  583. string errorMessage = string.Empty;
  584. while (ex2 != null)
  585. {
  586. errorMessage += ex2.ToString();
  587. ex2 = ex2.InnerException;
  588. }
  589.  
  590. Console.WriteLine(errorMessage);
  591. }
  592. }
  593.  
  594.  
  595.  
  596. }
  597. }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

版权声明:本文为jinzhao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jinzhao/archive/2009/11/20/1607020.html