• 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:haifengtiaoma201610311610MM004.xls";
    56             String sFile1 = @"D:haifengtiaoma201610311610MM004_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>
  • 相关阅读:
    【从零开始】【Java】【3】改造成多模块项目
    atomic nonatomic区别
    UIBezierPath
    全局块、栈块、堆块
    动态修改可变数组元素
    layoutSubviews
    约束Constraints
    Objective-C实现一个简单的栈
    NSString 使用 copy、strong
    ARC中__weak;__strong;__unsafe_unretained;修饰词
  • 原文地址:https://www.cnblogs.com/qq605490312/p/6043961.html
Copyright © 2020-2023  润新知