C# System.Net.Mail.MailMessage 发邮件

C# System.Net.Mail.MailMessage 发邮件

上篇文化在哪个可以看到使用 System.Web.Mail.MailMessage 发邮件时会提示

,提供用于构造电子邮件的属性和方法。建议使用的替代项:System.Net.Mail,The recommended alternative is System.Net.Mail.MailMessage ,因此,我们新建控制台Console项目,然后添加 System.Net引用

代码如下:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
//using System.IO.Pipes;
using System.Net;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace LongtengSupremeConsole
{
    class Program
    {        
        static void Main(string[] args)
        {
             System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
            //mm.Sender = new MailAddress("mail@aliyun.com", "linjie");
            mm.From = new MailAddress("test@aliyun.com", "123");//发送方
            mm.To.Add(new MailAddress("test@qq.com", "456"));//接受方
            mm.CC.Add(new MailAddress("test@163.com", "123789"));//抄送方,CC就是carbon copy,副本,及抄送的意思
            mm.Subject = "Hello!";//主题
            mm.Body = "Hello. Here's the myphoto!";//内容
            mm.IsBodyHtml = false;//是否使用html格式
            mm.Priority = MailPriority.High;//优先级
            Attachment a = new Attachment("myphoto.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg);//附件
            mm.Attachments.Add(a);

            SmtpClient client = new SmtpClient();//smtp客户端
            client.Host = "smtp.aliyun.com";//服务器主机
            client.DeliveryMethod = SmtpDeliveryMethod.Network;//发送方式
            client.Port = 25;//端口
            client.Credentials = new NetworkCredential("test@aliyun.com", "***");//用户名和密码
            client.Send(mm);
            Console.WriteLine("邮件发送完成!!");
            Console.ReadKey();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值