zoukankan      html  css  js  c++  java
  • c# 编程实现发email , 用gmail邮箱

    1 : 加这两个 using

    using System.Net.Mail;
    using System.Net;

    2 : 发送人 和  收件人的设置

                SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new NetworkCredential("myEmailAddress@gmail.com", "myPassword"),
                    EnableSsl = true
                };
    
    
                MailAddress from = new MailAddress(@"myEmailAddress@gmail.com", "myName");
                MailAddress to = new MailAddress(@"whereToSend@com.cn", "receiverName");
                MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

                // set subject and encoding
                myMail.Subject = "Auto send email of route 's perfermance at sometime ";
                myMail.SubjectEncoding = System.Text.Encoding.UTF8;

           //如果要添加抄送

               MailAddress copy = new MailAddress("someone@.com.cn");
                myMail.CC.Add(copy);

    3 : 写邮件正文 , 例子带一个表格 ,注意 换行用 <br/> , 而不是 " " , 如果用后者, 在outlook显示不出换行的效果。

                DataTable table = new DataTable();
                table.Columns.Add("ownerVendor", typeof(string));
                table.Columns.Add("priority", typeof(string));
                table.Columns.Add("aditionalParameter", typeof(string));
    
    
    
                table.Rows.Add("JJTEL", "3000", "30");
                table.Rows.Add("ARCA", "2000", "5");
                table.Rows.Add("SHINETOWN", "2000", "5");
    
    
                myMail.Body = @"Hi : " + "<br/>" + " below is ." + "<br/>" + @"<table border=""1"">
                <tr>
                    <td >
                        &nbsp;</td>
                    <td>
                        ASR</td>
                    <td>
                        ACD</td>
                </tr>";
    
                foreach (DataRow row in table.Rows)
                {
                    myMail.Body = myMail.Body + @"<tr>
                    <td>" +  row["ownerVendor"].ToString() +
                        @"</td>
                    <td >" + row["priority"].ToString() +
                    @"</td>
                    <td>" + row["aditionalParameter"].ToString() +
                    @"</td>
                </tr>";
                }
    
                myMail.Body = myMail.Body + @"</table>";

    4 : 其他设置

                myMail.BodyEncoding = System.Text.Encoding.UTF8;
    
                myMail.IsBodyHtml = true;                  //这句很重要

    5 : 发送

                client.Send(myMail);

    6 : 邮件的效果类似这样

    Hi :
    below is .

    ASR

    ACD

    JJTEL

    3000

    30

    ARCA

    2000

    5

    SHINETOWN

    2000

    5

  • 相关阅读:
    LeetCode_35.搜索插入位置
    LeetCode_349.两个数组的交集
    LeetCode_344.反转字符串
    LeetCode_34.在排序数组中查找元素的第一个和最后一个位置
    LeetCode_303.区域和检索
    LeetCode_3.无重复字符的最长子串
    LeetCode_292.Nim 游戏
    LeetCode_283.移动零
    LeetCode_27.移除元素
    LeetCode_268.丢失的数字
  • 原文地址:https://www.cnblogs.com/lthxk-yl/p/3512382.html
Copyright © 2011-2022 走看看