当前位置: 移动技术网 > IT编程>开发语言>c# > c#使用netmail方式发送邮件示例

c#使用netmail方式发送邮件示例

2019年07月18日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

/// <summary>
    /// netmail方式测试通过
    /// </summary>
    private void testsend()
    {
        system.net.mail.mailmessage mm = new system.net.mail.mailmessage();
        //收件人地址
        mm.to.add(new system.net.mail.mailaddress("xxxxxx@163.com", "name"));
        //发件人地址
        mm.from = new system.net.mail.mailaddress("xxxxx@sina.com");
        //这个可以不指定
        //mm.sender = new system.net.mail.mailaddress("xxx@sina.com", "sendername");、

        mm.subject = "this is test email";
        mm.body = "<h3>this is testing smtp mail send by me</h3>";
        mm.isbodyhtml = true;
        mm.priority = system.net.mail.mailpriority.high; // 设置发送邮件的优先级
        system.net.mail.smtpclient smtcliend = new system.net.mail.smtpclient();
        //指定邮件服务器
        smtcliend.host = "smtp.sina.com";
        //smtp邮件服务器的端口号 
        smtcliend.port = 25;  
        //设置发件人邮箱的用户名和地址,使用公共邮件服务器一般需要提供,不然发送不会成功
        smtcliend.credentials = new networkcredential("xxxxxxx", "xxxxxxx");

        //指定邮件的发送方式
        smtcliend.deliverymethod = system.net.mail.smtpdeliverymethod.network;
        try
        {
            smtcliend.send(mm);
        }
        catch (system.net.mail.smtpexception ex)
        {
            response.write(ex.message);
        }
        catch (exception ex)
        {
            response.write(ex.message);
        }
    }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网