• [转]C#发送Email邮件 (实例:QQ邮箱和Gmail邮箱)


    下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号。

    需要引用:
    using System.Net.Mail;
    using System.Text;
    using System.Net;

    程序代码:

            MailMessage myMail = new MailMessage(); //创建邮件实例对象

            myMail.From = new MailAddress("bluesky@sina.com"); //发送者,要和邮件服务器的验证信息对应,不能随便更改
            
            myMail.To.Add(new MailAddress("848594@qq.com")); //接收者

            myMail.Subject = "C#发送Email"; //邮件标题
            myMail.SubjectEncoding = Encoding.UTF8; //标题编码

            myMail.Body = "this is a test email!"; //邮件内容
            myMail.BodyEncoding = Encoding.UTF8; //邮件内容编码
            myMail.IsBodyHtml = true;     //邮件内容是否支持html

            SmtpClient smtp = new SmtpClient(); //创建smtp实例对象
            smtp.Host = "mail.sina.com"; //邮件服务器SMTP
            smtp.Port = 25;                  //邮件服务器端口
            smtp.Credentials = new NetworkCredential("bluesky@sina.com", "123456"); //邮件服务器验证信息

            smtp.Send(myMail); //发送邮件

    使用Gmail邮箱发送邮件示例

            MailMessage myMail = new MailMessage();

            myMail.From = new MailAddress("bluesky@gmail.com");
            myMail.To.Add(new MailAddress("444354@qq.com"));

            myMail.Subject = "C#发送Email";
            myMail.SubjectEncoding = Encoding.UTF8;

            myMail.Body = "this is a test email from gmail!<a href='http://www.sina.com.cn'>sina</a>";
            myMail.BodyEncoding = Encoding.UTF8;
            myMail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587; //Gmail的smtp端口
            smtp.Credentials = new NetworkCredential("bluesky@gmail.com", "123456");
            smtp.EnableSsl = true; //Gmail要求SSL连接

            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //Gmail的发送方式是通过网络的方式,需要指定

            smtp.Send(myMail);

    使用QQ邮箱发送邮件示例

            MailMessage myMail = new MailMessage();

            myMail.From = new MailAddress("4443233@qq.com");
            myMail.To.Add(new MailAddress("bluesky@gmail.com"));

            myMail.Subject = "C#发送Email";
            myMail.SubjectEncoding = Encoding.UTF8;

            myMail.Body = "this is a test email from QQ!";
            myMail.BodyEncoding = Encoding.UTF8;
            myMail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.qq.com";
            smtp.Credentials = new NetworkCredential("4443233@qq.com", "123456");

            smtp.Send(myMail);

  • 相关阅读:
    jmeter脚本开发:插件安装和设计场景(五)
    jmeter脚本开发:SOAP接口和JDBC(四)
    jmeter脚本开发:控制器和参数化(三)
    gauge自动化框架踩坑(六):关于csv
    gauge自动化框架踩坑(五):关于表格
    gauge自动化框架踩坑(四):在测试报告中自定义messages
    MediaPlayer播放音频,也可以播放视频
    soundpool播放声音
    ContentProvider
    了解 IMyInterface.Stub
  • 原文地址:https://www.cnblogs.com/Griffin/p/3343191.html
Copyright © 2020-2023  润新知