C#发送邮件的方法及实例代码

33 篇文章 0 订阅
21 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
namespace com.ykmaiz.email
{
    public class Mail
    {
        private string username = "";
        private string password = "";
        private string domain = "";
        public Mail(string username,string password,string domain)
        {
            this.username = username;
            this.password = password;
            this.domain = domain;
        }
        public void send(string from,string [] to,string [] cc,string title,string content)
        {
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new MailAddress(from);
            if (to.Length > 0)
            {
                foreach(string s in to)
                {
                    mailMsg.To.Add(s);
                }
            }
            if (cc.Length > 0)
            {
                foreach (string s in cc)
                {
                    mailMsg.CC.Add(s);
                }
            }
            mailMsg.Subject = title;
            mailMsg.Body = content;
            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.IsBodyHtml = false;
            mailMsg.Priority = MailPriority.High;
            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new NetworkCredential(username, password);
            smtp.Port = 25;
            smtp.Host = domain;
            smtp.EnableSsl = false;
            smtp.SendCompleted += new SendCompletedEventHandler(SendMailCompleted);
            try
            {
                smtp.SendAsync(mailMsg, mailMsg);
            }
            catch (SmtpException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
然后直接调用该类的send()方法即可,
实例代码如下:
Mail mail = new Mail("发邮件的地址", "发邮件的密码", "邮件的smtp地址");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值