C#实现发送邮件

本文属于个人原创作品、个人总结,谢绝转载、抄袭。如果您有疑问或者希望沟通交流,可以联系QQ:865562060。

一、邮件服务器。

    邮件服务器可以使用注册的企业开放的地址和端口号,例如:

    QQ邮箱的收取邮件支持POP/IMAP两种协议,发送邮件采用SMTP协议,收件和发件均使用SSL协议来进行加密传输,采用SSL协议需要单独对帐户进行设置。采用SSL协议和非SSL协议时端口号有所区别,参照下表的一些常见配置组合:

类型

服务器名称

服务器地址

非SSL协议端口号

SSL协议端口号

发件服务器

SMTP

smtp.qq.com

25

465/587

收件服务器

POP

pop.qq.com

110

995

收件服务器

IMAP

imap.qq.com

143

993

    

发送邮件我们这里就使用发件服务器的地址以及端口号25。如果是企业邮箱发送邮件,请更换邮件服务器地址、端口号。

服务器地址和端口号均正确,邮件发送失败?请查看公司内网的邮件发送协议,是否服务器IP地址在允许发送邮件的列表中;或者查看内网有没有禁用邮件服务器端口号。

 

二、代码实现。

//msg为邮件内容,邮件中可以识别标签,故msg可以为HTML内容;
//byteFile为附件字节组,如果邮件存在附件,先将邮件转换为字节组;AttDesciption为附件名称,在邮件中展示。
EmailSender.current().sendMsg("目标邮箱", "邮件标题", msg, byteFile, AttDesciption);

1、发送邮件帮助类。邮件内容可以是文字也可以是HTML内容,如果邮件包含附件,需要将附件先转换为字节组,并附上附件名称,使用System.Net.Mail将附件添加进邮件。

System.IO.MemoryStream ms = new System.IO.MemoryStream(byteFile);
System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(ms, FileName);
msg.Attachments.Add(att);
    public class EmailSender
    {
        volatile int status = 0;
        String from = "", fromName = "";
        String password = "", strHost = "smtp.163.com";
        int iPort = 25;

        private EmailSender()
        {
            from = "**@qq.com";//邮箱地址
            fromName = "**的邮箱";//邮箱名称
            password = "password";//邮箱密码
            strHost = "smtp.qq.com";//邮箱服务器地址
            iPort = 25;//邮箱服务器端口号
        }
        private static EmailSender instance = new EmailSender();
        public static EmailSender current()
        {
            return instance;
        }
        public void sendMsg(String toUser, String subject, String body)
        {
            List toUsers = new List();
            toUsers.Add(toUser);
            sendMsg(toUsers, null, subject, body);
        }
        public void sendMsg(String toUser, String subject, String body, byte[] byteFile, string FileName)
        {
            List toUsers = new List();
            toUsers.Add(toUser);
            sendMsg(toUsers, null, subject, body, byteFile, FileName);
        }
        public void sendMsg(List toUsers, List ccUsers, String subject, String body)
        {
            lock (this)
            {
                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                foreach (String user in toUsers)
                {
                    msg.To.Add(user);
                }

                if (ccUsers != null)
                {
                    foreach (String user in ccUsers)
                    {
                        msg.CC.Add(user);
                    }
                }
                msg.From = new MailAddress(from, fromName, System.Text.Encoding.UTF8);

                msg.Subject = subject;//邮件标题    
                msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码   
                AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                msg.AlternateViews.Add(htmlBody);
                //msg.Body = body; //邮件内容    
                msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
                msg.IsBodyHtml = true;//,false;//是否是HTML邮件    
                msg.Priority = MailPriority.High;//邮件优先级 
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential(from, password);

                client.Port = iPort;//25 465;//qqmail使用的端口    
                client.Host = strHost;
                //client.EnableSsl = true;//经过ssl加密    
                object userState = msg;

                try
                {
                    client.Send(msg);
                    //client.SendAsync(msg, userState);
                    //简单一点儿可以client.Send(msg);    
                    //MessageBox.Show("发送成功");

                }
                catch (System.Net.Mail.SmtpException ex)
                {
                    throw ex;
                    //MessageBox.Show(ex.Message, "发送邮件出错");
                }
            }
        }
        public void sendMsg(List toUsers, List ccUsers, String subject, String body, byte[] byteFile, string FileName)
        {
            lock (this)
            {
                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                foreach (String user in toUsers)
                {
                    msg.To.Add(user);
                }

                if (ccUsers != null)
                {
                    foreach (String user in ccUsers)
                    {
                        msg.CC.Add(user);
                    }
                }
                msg.From = new MailAddress(from, fromName, System.Text.Encoding.UTF8);

                msg.Subject = subject;//邮件标题    
                msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码   
                AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                msg.AlternateViews.Add(htmlBody);
                //msg.Body = body; //邮件内容    
                msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
                msg.IsBodyHtml = true;//,false;//是否是HTML邮件    
                msg.Priority = MailPriority.High;//邮件优先级 
                if (byteFile != null)
                {
                    System.IO.MemoryStream ms = new System.IO.MemoryStream(byteFile);
                    System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(ms, FileName);
                    msg.Attachments.Add(att);
                }
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential(from, password);

                client.Port = iPort;//25 465;//qqmail使用的端口    
                client.Host = strHost;
                //client.EnableSsl = true;//经过ssl加密    
                object userState = msg;

                try
                {
                    client.Send(msg);
                    //client.SendAsync(msg, userState);
                    //简单一点儿可以client.Send(msg);    
                    //MessageBox.Show("发送成功");

                }
                catch (System.Net.Mail.SmtpException ex)
                {
                    throw ex;
                    //MessageBox.Show(ex.Message, "发送邮件出错");
                }
            }
        }
    }

本文属于个人原创作品,属于个人完成公司项目之后的含泪总结,谢绝转载、抄袭。

如果您有疑问或者希望沟通交流,可以联系QQ:865562060。

你可能感兴趣的:(C#)