整合營銷服務(wù)商

          電腦端+手機端+微信端=數(shù)據(jù)同步管理

          免費咨詢熱線:

          C#發(fā)送郵件-C#教程

          C#發(fā)送郵件-C#教程

          何利用C#實現(xiàn)郵件發(fā)送功能?閑話不多說請看代碼:

          public static void SendMail(MyEmail email)

          {

          //發(fā)送驗證郵箱郵件。

          //1.創(chuàng)建郵件

          MailMessage mail=new MailMessage();

          mail.Subject=email.Title;

          mail.SubjectEncoding=System.Text.Encoding.UTF8;

          mail.Body=email.Content;

          mail.BodyEncoding=System.Text.Encoding.UTF8;

          mail.IsBodyHtml=true;

          //發(fā)件人

          mail.From=new MailAddress("cxx7177@163.com", email.Subject);

          //收件人

          mail.To.Add(new MailAddress(email.SendEmail));

          //創(chuàng)建一個發(fā)送郵件的類

          SmtpClient client=new SmtpClient("smtp.163.com", 25);

          client.Credentials=new NetworkCredential("帳號", "密碼");

          client.Send(mail);

          }

          public class MyEmail

          {

          //string content,string sendEmail,string Uid,string subject

          public string Content { get; set; }

          public string SendEmail { get; set; }

          public string Subject { get; set; }

          public string Title { get; set; }

          }

          }

          當然本代碼可用于:C#發(fā)送郵件,asp.net發(fā)送郵件,.net發(fā)送郵件,希望可以幫到您

          www.chengxiaoxiao.com/

           前言

            郵件是許多項目里都需要用到的功能,之前一直都是用JavaMail來發(fā),現(xiàn)在Spring框架為使用JavaMailSender接口發(fā)送電子郵件提供了一個簡單的抽象,Spring Boot為它提供了自動配置以及啟動模塊。springboot參考手冊介紹:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-email

            作為發(fā)送方,首先需要開啟POP3/SMTP服務(wù),登錄郵箱后前往設(shè)置進行開啟,開啟后取得授權(quán)碼。

            POP3 :
            POP3是Post Office Protocol 3的簡稱,即郵局協(xié)議的第3個版本,規(guī)定怎樣將個人計算機連接到Internet的郵件服務(wù)器和下載電子郵件的電子協(xié)議。是因特網(wǎng)電子郵件的第一個離線協(xié)議標準,POP3允許用戶從服務(wù)器上把郵件存儲到本地主機(即自己的計算機)上,同時刪除保存在郵件服務(wù)器上的郵件,而POP3服務(wù)器則是遵循POP3協(xié)議的接收郵件服務(wù)器,用來接收電子郵件的。
            

            SMTP:
            SMTP 的全稱是“Simple Mail Transfer Protocol”,即簡單郵件傳輸協(xié)議。是一組用于從源地址到目的地址傳輸郵件的規(guī)范,通過來控制郵件的中轉(zhuǎn)方式。SMTP 協(xié)議屬于 TCP/IP 協(xié)議簇,幫助每臺計算機在發(fā)送或中轉(zhuǎn)信件時找到下一個目的地。SMTP 服務(wù)器就是遵循 SMTP 協(xié)議的發(fā)送郵件服務(wù)器。


            代碼編寫

            maven引包,其中,郵件模板需要用到thymeleaf

                  <!-- springboot mail -->
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-mail</artifactId>
                  </dependency>
                  <!-- thymeleaf模板 -->
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-thymeleaf</artifactId>
                  </dependency>
                  <!-- springboot web(MVC)-->
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
                  <!-- springboot -->
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-test</artifactId>
                      <scope>test</scope>
                  </dependency>


            appliaction.propertise配置文件

          #設(shè)置服務(wù)端口
          server.port=10010
          
          # Email (MailProperties)
          spring.mail.default-encoding=UTF-8
          spring.mail.host=smtp.qq.com
          spring.mail.username=huanzi.qch@qq.com #發(fā)送方郵件名 
          spring.mail.password=#授權(quán)碼 
          spring.mail.properties.mail.smtp.auth=true 
          spring.mail.properties.mail.smtp.starttls.enable=true 
          spring.mail.properties.mail.smtp.starttls.required=true


            SpringBootMailServiceImpl.java

          @Service
          class SpringBootMailServiceImpl implements SpringBootMailService {
          
              @Autowired
              private JavaMailSender mailSender;
          
              /**
               * 發(fā)送方
               */
              @Value("${spring.mail.username}")
              private String from;
          
              /**
               * 發(fā)送簡單郵件
               *
               * @param to      接收方
               * @param subject 郵件主題
               * @param text    郵件內(nèi)容
               */
              @Override
              public void sendSimpleMail(String to, String subject, String text) {
                  SimpleMailMessage message=new SimpleMailMessage();
                  message.setFrom(from);
                  message.setTo(to);
                  message.setSubject(subject);
                  message.setText(text);
          
                  mailSender.send(message);
              }
          
              /**
               * 發(fā)送HTML格式的郵件
               *
               * @param to      接收方
               * @param subject 郵件主題
               * @param content HTML格式的郵件內(nèi)容
               * @throws MessagingException
               */
              @Override
              public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
                  MimeMessage message=mailSender.createMimeMessage();
                  //true表示需要創(chuàng)建一個multipart message
                  MimeMessageHelper helper=new MimeMessageHelper(message, true);
                  helper.setFrom(from);
                  helper.setTo(to);
                  helper.setSubject(subject);
                  helper.setText(content, true);
          
                  mailSender.send(message);
              }
          
              /**
               * 發(fā)送HTML格式的郵件,并可以添加附件
               * @param to      接收方
               * @param subject 郵件主題
               * @param content HTML格式的郵件內(nèi)容
               * @param files   附件
               * @throws MessagingException
               */
              @Override
              public void sendAttachmentsMail(String to, String subject, String content, List<File> files) throws MessagingException {
                  MimeMessage message=mailSender.createMimeMessage();
                  MimeMessageHelper helper=new MimeMessageHelper(message, true);
                  helper.setFrom(from);
                  helper.setTo(to);
                  helper.setSubject(subject);
                  helper.setText(content, true);
                  //添加附件
                  for(File file : files){
                      helper.addAttachment(file.getName(), new FileSystemResource(file));
                  }
          
                  mailSender.send(message);
              }
          }


            測試controller

              @Autowired
              private SpringBootMailService springBootMailService;
          
              @Autowired
              private TemplateEngine templateEngine;
          
              @GetMapping("/index")
              public String index() throws MessagingException {
                  //簡單郵件
                  springBootMailService.sendSimpleMail("1726639183@qq.com","Simple Mail","第一封簡單郵件");
          
                  //HTML格式郵件
                  Context context=new Context();
                  context.setVariable("username","我的小號");
                  springBootMailService.sendHtmlMail("1726639183@qq.com","HTML Mail",templateEngine.process("mail/mail",context));
          
                  //HTML格式郵件,帶附件
                  Context context2=new Context();
                  context2.setVariable("username","我的小號(帶附件)");
                  ArrayList<File> files=new ArrayList<>();
                  files.add(new File("C:\\Users\\Administrator\\Desktop\\上傳測試.txt"));
                  files.add(new File("C:\\Users\\Administrator\\Desktop\\上傳測試2.txt"));
                  springBootMailService.sendAttachmentsMail("1726639183@qq.com","Attachments Mail",templateEngine.process("mail/attachment",context2),files);
          
                  return "hello springboot!";
              }


            兩個html模板,路徑:myspringboot\src\main\resources\templates\mail\

            mail.html

          <!DOCTYPE html>
          <html xmlns:th="http://www.thymeleaf.org">
          <head>
              <meta charset="UTF-8">
              <title>Mail Templates</title>
          </head>
          <body>
              <h3><span th:text="${username}"></span>,你好!</h3>
              <p style="color: red;">這是一封HTML格式的郵件。</p>
          </body>
          </html>

            attachment.html

          <!DOCTYPE html>
          <html xmlns:th="http://www.thymeleaf.org">
          <head>
              <meta charset="UTF-8">
              <title>Mail Templates Accessory</title>
          </head>
          <body>
              <h3><span th:text="${username}"></span>,你好!</h3>
              <p>這是一封HTML格式的郵件。請收下附件!</p>
          </body>
          </html>


            效果

            Simple Mail

            HTML Mail


            Attachments Mail



            后記

            本文章部分參考:https://www.cnblogs.com/yangtianle/p/8811732.html


            代碼開源

            代碼已經(jīng)開源、托管到我的GitHub、碼云:

            GitHub:https://github.com/huanzi-qch/springBoot

            碼云:https://gitee.com/huanzi-qch/springBoot



          版權(quán)聲明

          作者:huanzi-qch

          出處:https://www.cnblogs.com/huanzi-qch

          若標題中有“轉(zhuǎn)載”字樣,則本文版權(quán)歸原作者所有。若無轉(zhuǎn)載字樣,本文版權(quán)歸作者所有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,否則保留追究法律責任的權(quán)利.

          子郵件能讓訪問者方便地向網(wǎng)站提供反饋或聯(lián)系信息。它可以自動填寫抄送和密件抄送,甚至能自動填充主題行。下面介紹如何定制 Mailto功能。

          實現(xiàn) Mailto的基本html代碼:

          <a href="mailto:stephen.zhaoyf@163.com">點擊這里發(fā)郵件給站長!</a>

          當訪問者點擊這個鏈接時,會調(diào)用他們客戶端的email程序,并在收件人框中自動填上收件人的地址。下面,我們將分以下幾步介紹如何增加mailto的功能。

          第一步:

          創(chuàng)建一個基本的mailto,包含收件人的地址。注意:后面的參數(shù)符需要采用英文的符號

          第二步:

          在收件人地址后用“?cc="開頭,你可以填寫抄送(CC:)地址,下面這個例子將實現(xiàn)該功能:

          <a href="mailto:stephen.zhaoyf@163.com?cc=guest@163.net">點擊這里發(fā)郵件給站長并“抄送”給guest!</a>

          第三步:

          就像下面這個例子一樣,緊跟著抄送地址之后,寫上"&bcc=",就可以填上密件抄送(BCC)地址 (在添加這些功能時,第一個功能以"?"開頭,后面的以"&"開頭)

          <a href="mailto:stephen.zhaoyf@163.com?cc=guest@163.net&bcc=guest@163.net">點擊這里發(fā)郵件給站長并同時“抄送”、“暗送”給guest!</a>

          還可以同時發(fā)信給N個人!地址之間用逗號分開:

          <a href="mailto:stephen.zhaoyf@163.com,guest@163.net,guest1@163.net">點擊這里同時發(fā)送N個郵件!</a>

          第四步:

          用?subject=或者&subject(當前面還有抄送或暗送時使用)填上主題。

          <a href="mailto:stephen.zhaoyf@163.com?subject=不好意思,只是做個實驗!">點擊這里發(fā)送有主題的郵件!</a>

          更加夸張的是連郵件內(nèi)容都可以事先寫好!

          <a href="mailto:stephen.zhaoyf@163.com?subject=不好意思,只是做個實驗!&body=特別無聊,所以發(fā)現(xiàn)這么一個功能">這個郵件地址、內(nèi)容都有了!以后可就懶了!</a>

          下面我們來總結(jié)一下:

          Mailto后為收件人地址,cc后為抄送地址,bcc后為密件抄送地址,subject后為郵件的主題,body后為郵件的內(nèi)容,如果Mailto后面同時有多個參數(shù)的話,第一個參數(shù)必須以“?”開頭,后面的每一個都以“&”開頭。下面是一個完整的實例:

          Mailto:aaa@xxx.com?cc=bbb@yyy.com&bcc=ccc@zzz.com&subject=主題&body=郵件內(nèi)容

          亂碼問題:

          比如調(diào)用個下載程序,當下載的URL中含有中文的時候,無法下載,比如:

          http://www.huachu.com.cn/itbook/booklist.asp?tsmc=匯編

          試著用 .net 中的 Server.UrlEncode 函數(shù)進行轉(zhuǎn)換。但是這樣仍然不行,試驗了很久也沒有找到原因。后來懷疑 ASP.net中的Server.UrlEncode函數(shù)和ASP中的Server.URLEncode函數(shù)返回的值竟然不一樣。一實驗,竟然確實是。

          試驗代碼:

          ASP.net 中 如下代碼?

          Response.Write(Server.UrlEncode("匯編")); ?返回: %e6%b1%87%e7%bc%96

          ASP 中 如下代碼 Response.Write Server.URLEncode("匯編")?? 返回: %BB%E3%B1%E0

          產(chǎn)生這個問題的原因:ASP.net 中的 Server.UrlEncode 默認是按照 UTF-8 編碼方式進行處理的。而ASP中是按照本地設(shè)置編碼方式進行處理的。

          如果你在 ASP.net 下采用如下的編碼: ASP 和 ASP.net 的結(jié)果就會一樣:

          Response.Write(HttpUtility.UrlEncode("匯編",Encoding.Default));

          采用:Response.Write(HttpUtility.UrlEncode("匯編",Encoding.UTF8));? 返回的就是 Response.Write(Server.UrlEncode("匯編"));?? 返回的結(jié)果。


          主站蜘蛛池模板: 一区二区三区波多野结衣| 国产精品久久久久久麻豆一区| AV无码精品一区二区三区宅噜噜| 中文字幕一区二区三区乱码| 亚洲av色香蕉一区二区三区蜜桃| 精品国产免费一区二区三区| 色窝窝免费一区二区三区| 国产精品一区二区久久不卡| 日本一区二区高清不卡| 日韩精品一区二区三区中文版| 精品成人乱色一区二区| 日本不卡免费新一区二区三区| 韩国福利一区二区三区高清视频| 国模无码视频一区| 国产香蕉一区二区三区在线视频 | 久久亚洲AV午夜福利精品一区| 国产无吗一区二区三区在线欢 | 亚洲AV无码一区二区三区在线观看 | 国产精品高清一区二区三区不卡| 中文字幕AV一区中文字幕天堂| 国产另类ts人妖一区二区三区| 色综合视频一区二区三区| 国产日韩精品一区二区在线观看播放| 亚洲无码一区二区三区| 97精品一区二区视频在线观看| 精品一区二区三区波多野结衣 | 国产伦理一区二区三区| 夜精品a一区二区三区| 精品久久久久久中文字幕一区| 无码中文人妻在线一区| 韩国精品一区二区三区无码视频| 国内精品视频一区二区八戒| 久久精品日韩一区国产二区| 日韩在线一区视频| 免费一区二区三区四区五区| 一区二区三区国模大胆| 九九久久99综合一区二区| 91福利国产在线观一区二区| 无码少妇一区二区浪潮免费| 精品在线视频一区| 国产区精品一区二区不卡中文|