zoukankan      html  css  js  c++  java
  • 邮件系列(二)-发送邮件

    通过上一篇配置本地邮件服务器,已经实现了手动发送,接收邮件的功能。

    接下来,我们就试试用代码实现发邮件。

    我们都知道,邮件一般包括,发件人,收件人,邮件服务器,邮件。那么我们就按照这个思路来写代码

    1.发送一封简单的邮件

    private static void SendSimpleMail()
    {
        //1.创建一封邮件对象
        MailMessage mail=new MailMessage();
        mail.Subject = "我是邮件的主题";
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Body = "我是邮件的正文";
        mail.BodyEncoding = Encoding.UTF8;
        //发件人
        mail.From=new MailAddress(lisi@163.com,"李小四");
        //收件人
        mail.To.Add(new MailAddress(wangwu@163.com,"王小五"));
        //2.创建一个发送邮件的类
        SmtpClient client=new SmtpClient("127.0.0.1");
        client.Credentials=new NetworkCredential("lisi@163.com","lisi");
        client.Send(mail);
        Console.WriteLine("ok");
    }

    在Foxmail中如下图

    image

    查看邮件源码

    image

    2.发送一封带html的邮件

    private static void SendHtmlMail()
    {
        //1.创建一封邮件对象
        MailMessage mail = new MailMessage();
        mail.Subject = "我是邮件的主题";
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Body = "我是邮件的正文";
        mail.BodyEncoding = Encoding.UTF8;
        //发件人
        mail.From = new MailAddress("lisi@163.com", "李四");
        //收件人
        mail.To.Add(new MailAddress("wangwu@163.com", "王五"));
        //-----------------------------------------------------------
    
        //设置该邮件的格式为html
        AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(
            "你好<font colof='red' size=7>中国</font><hr/>", null, "text/html");
        //将该html内容添加 到邮件中
        mail.AlternateViews.Add(htmlBody);
    
        //-----------------------------------------------------------
        //2.创建一个发送邮件的类
        SmtpClient client = new SmtpClient("127.0.0.1");
        client.Credentials = new NetworkCredential("lisi@163.com", "lisi");
        client.Send(mail);
        Console.WriteLine("ok");
    }

    在Foxmail中如下图

    image

    3.发送一封带显示图片的邮件

    private static void SendMailWithImg()
    {
        //1.创建一封邮件对象
        MailMessage mail = new MailMessage();
        mail.Subject = "我是邮件的主题";
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Body = "我是邮件的正文";
        mail.BodyEncoding = Encoding.UTF8;
        //发件人
        mail.From = new MailAddress("lisi@163.com", "李四");
        //收件人
        mail.To.Add(new MailAddress("wangwu@163.com", "王五"));
        //-----------------------------------------------------------
        //设置邮件格式为html,将img标签设置到html中
        AlternateView htmlBody =
           AlternateView.CreateAlternateViewFromString("<img src='cid:aa' width='100px' height='100px'/>", null,
               "text/html");
        //创建一个链接资源,
        LinkedResource lr=new LinkedResource("h:/1.jpg","image/gif");
        //设置此链接资源MIME内容ID
        lr.ContentId = "aa";
        //将该链接资源添加到html中
        htmlBody.LinkedResources.Add(lr);
        //将html内容添加到邮件中
        mail.AlternateViews.Add(htmlBody);
    
        //-----------------------------------------------------------
        //2.创建一个发送邮件的类
        SmtpClient client = new SmtpClient("127.0.0.1");
        client.Credentials = new NetworkCredential("lisi@163.com", "lisi");
        client.Send(mail);
        Console.WriteLine("ok");
    }

    在Foxmail中如下图

    image

    4.发送一封带附件的邮件

    private static void SendMailWithAttachment()
    {
        //1.创建一封邮件对象
        MailMessage mail = new MailMessage();
        mail.Subject = "我是邮件的主题";
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Body = "我是邮件的正文";
        mail.BodyEncoding = Encoding.UTF8;
        //发件人
        mail.From = new MailAddress("lisi@163.com", "李四");
        //收件人
        mail.To.Add(new MailAddress("wangwu@163.com", "王五"));
        //-----------------------------------------------------------
        //添加附件
        Attachment a1=new Attachment(@"h:/1.jpg");
        Attachment a2=new Attachment(@"h:/1.rar");
        Attachment a3=new Attachment(@"h:/1.docx");
    
        mail.Attachments.Add(a1);
        mail.Attachments.Add(a2);
        mail.Attachments.Add(a3);
        //-----------------------------------------------------------
        //2.创建一个发送邮件的类
        SmtpClient client = new SmtpClient("127.0.0.1");
        client.Credentials = new NetworkCredential("lisi@163.com", "lisi");
        client.Send(mail);
        Console.WriteLine("ok");
    }

    在Foxmail中如下图

    image

    更多精彩内容请看:http://www.cnblogs.com/2star
  • 相关阅读:
    补题报告 个人赛2020.4.12
    UCF Local Programming Contest 2017 2020.4.7
    比赛:ICPC Asia Taipei-Hsinchu Regional 2019 2020.4.1
    UCF Local Programming Contest 2016 2020.3.28
    Benelux Algorithm Programming Contest 2019 2020/3/21
    比赛名:Preliminaries for Benelux Algorithm Programming Contest 2019 时间2020.3.14
    【Scala】Scala使用scalikejdbc工具连接MySQL(推荐)
    【Scala】Scala使用JDBC连接Mysql/权限问题
    【异常】Specified key was too long;max key length is 767 bytes、解决由于HDFS格式化造成Hive数据全部丢失的问题
    【异常】Hive作业异常kill :Hadoop MapReduce Error
  • 原文地址:https://www.cnblogs.com/kimisme/p/4360939.html
Copyright © 2011-2022 走看看