C# 邮箱的使用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Configuration;
 5 using System.Data;
 6 using System.Drawing;
 7 using System.Linq;
 8 using System.Net;
 9 using System.Net.Mail;
10 using System.Text;
11 using System.Threading.Tasks;
12 using System.Windows.Forms;
13 
14 namespace 邮件发送
15 {
16     public partial class Form1 : Form
17     {
18         public Form1()
19         {
20             InitializeComponent();
21         }
22 
23         private void button1_Click(object sender, EventArgs e)
24         {
25             string email = "605490312@qq.com";
26             string emailBody = @"致:各位<br />
27 請查收附件的條碼資料。<br />
28 謝謝!!<br />
29 
30 
31         (該郵件由自動化系統後臺群發,請勿回覆。不便之處,敬請諒解!)";
32             SendEmail(email, "test主题", emailBody);
33         }
34         /// <summary>
35         /// 发送邮件
36         /// </summary>
37         /// <param name="toEmail"></param>
38         /// <param name="subject"></param>
39         /// <param name="body"></param>
40         public static void SendEmail(string toEmail, string subject, string body)
41         {
42             string smtpServer = ConfigurationManager.AppSettings["SmtpServer"];
43             string SmtpFrom = ConfigurationManager.AppSettings["SmtpFrom"];
44             string SmtpUserName = ConfigurationManager.AppSettings["SmtpUserName"];
45             string SmtpPassword = ConfigurationManager.AppSettings["SmtpPassword"];
46             MailMessage mailObj = new MailMessage();
47            
48             mailObj.IsBodyHtml = true;
49             mailObj.From = new MailAddress(SmtpFrom, SmtpUserName, Encoding.UTF8); //发送人邮箱地址
50             mailObj.To.Add(toEmail);   //收件人邮箱地址
51             mailObj.To.Add("771981371@qq.com");
52             mailObj.To.Add("huangjialiang@perfsmart.com");
53             mailObj.Subject = subject;    //主题
54             mailObj.Body = body;    //正文
55             String sFile = @"D:\haifengtiaoma\20161031\1610MM004.xls";
56             String sFile1 = @"D:\haifengtiaoma\20161031\1610MM004_lot.csv";
57             Attachment aAttch = new Attachment(sFile);//添加附件
58 
59             Attachment aAttch1 = new Attachment(sFile1);//每个附件就需要NEW一个Attachment
60             mailObj.Attachments.Add(aAttch);
61             mailObj.Attachments.Add(aAttch1);
62 
63             SmtpClient smtp = new SmtpClient();//通过.Net内置的SmtpClient类和邮件服务器进行通讯,发送邮件
64             //是和发邮件方的smtpt通讯,由发邮件方的邮件服务器和收邮件方的邮件服务器通信进行邮件的转接
65             smtp.Host = smtpServer;         //smtp服务器名称
66             smtp.UseDefaultCredentials = true;
67             smtp.EnableSsl = true;
68             smtp.Credentials = new NetworkCredential(SmtpUserName, SmtpPassword);  //发送人的登录名和密码
69            // smtp.Port = 587;
70              smtp.EnableSsl = false;//如果显示 服务器不支持安全连接。 则smtp.EnableSsl = false
71             smtp.Send(mailObj);
72         }
73     }
74 }
邮件发送代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    
    <add key="SmtpServer" value="smtp.test.com"/>
    <add key="SmtpFrom" value="hjl@test.com"/>
    <add key="SmtpUserName" value="hjl@test.com"/>
    <add key="SmtpPassword" value="123321"/>
   
  </appSettings>
</configuration>

 

转载于:https://www.cnblogs.com/qq605490312/p/6043961.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值