c#编程实现发email,用gmail邮箱-创新互联

1 : 加这两个 usingc#编程实现发email,用gm
ail邮箱

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

创新互联主营迪庆州网站建设的网络公司,主营网站建设方案,App定制开发,迪庆州h5小程序设计搭建,迪庆州网站营销推广欢迎迪庆州等地区企业咨询

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

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials= new NetworkCredential("myEmailAddress@gmail.com", "myPassword"),
                EnableSsl= true
            };


            MailAddressfrom = 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 : 写邮件正文 , 例子带一个表格 ,注意 换行用
, 而不是 " " , 如果用后者, 在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 :" + "
" + " below is ." + "
" + @""; foreach (DataRow row in table.Rows) { myMail.Body= myMail.Body + @""; } myMail.Body= myMail.Body + @"
  ASR ACD
" + row["ownerVendor"].ToString() + @" " + row["priority"].ToString() + @" " + row["aditionalParameter"].ToString() + @"
";

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


当前标题:c#编程实现发email,用gmail邮箱-创新互联
地址分享:http://csdahua.cn/article/cddchs.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流