• c# 发送邮件笔记(一)


    代码比较简单 

    using System;using System.Text;
    using System.Net.Mail;
    using System.Net;
    using System.IO;
    using System.Net.Mime;
    namespace 邮件发送程序
    {
        class SMTPHelper
        {
    
            private string user_name = "";
            private string pwd = "";
            private SmtpClient smtp_client = null;
            private MailAddress address_my = null;
    
            public SMTPHelper(string mail_address, string display_name, string pwd, bool ssl = false)
            {
                address_my = new MailAddress(mail_address, display_name, Encoding.UTF8);
                //保存用户帐号和密码
                this.user_name = address_my.User;
                this.pwd = pwd;
    
                smtp_client = new SmtpClient(address_my.Host, ssl ? 110 : 25);
                smtp_client.Timeout = 10 * 1000; //设置10秒超时
                smtp_client.UseDefaultCredentials = false;
                this.smtp_client.Host = "smtp." + address_my.Host;
    
            }
    
            public void setAuthenticateInfo(string name, string pwd)
            {
    
                NetworkCredential credential = new NetworkCredential(name, pwd);
                this.smtp_client.Credentials = credential; //填写验证            
    
            }
    
            public MailMessage createMailMessage(string to, string subject, string body)
            {
    
                string[] to_list = to.Split(';');//多个邮箱使用;隔开
    
                MailMessage mail_message = new MailMessage();
                mail_message.Sender = this.address_my;
                mail_message.From = this.address_my;
                mail_message.Subject = subject;
                mail_message.SubjectEncoding = Encoding.UTF8;
                mail_message.Body = body;
                mail_message.BodyEncoding = Encoding.UTF8; //UTF8编码
                mail_message.IsBodyHtml = false; //不是HTML
                foreach (string s in to_list)
                {
                    mail_message.To.Add(new MailAddress(s));
    
                }
                return mail_message;
            }
            /// <summary>
            /// 创建带附件的邮件
            /// </summary>
            /// <param name="address_to"></param>
            /// <param name="subject"></param>
            /// <param name="body"></param>
            /// <returns></returns>
            public MailMessage createMailMessageWithAttacth(string to, string subject, string body, string fileNames)
            {
                MailMessage mail_message = this.createMailMessage(to, subject, body);
                string[] files = fileNames.Split(';');
                foreach (string file in files)
                {
                    if (File.Exists(file))
                    {
                        Attachment attchment = new Attachment(file);
    
                        //设置mime对象
                        ContentDisposition disposition = attchment.ContentDisposition;
                        disposition.CreationDate = File.GetCreationTime(file); //附件创建日期
                        disposition.ModificationDate = File.GetLastWriteTime(file);//最后更改日期
                        disposition.ReadDate = File.GetLastAccessTime(file);//最后读取时间
                        mail_message.Attachments.Add(attchment);
                    }
    
                }
                return mail_message;
    
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="to">如果多个帐户用分隔符</param>
            /// <param name="subject"></param>
            /// <param name="body"></param>
            public bool doSend(string to, string subject, string body, string fileNames)
            {
                smtp_client.DeliveryMethod = SmtpDeliveryMethod.Network; //指定发送模式,由网络直接发送,一般都用这个
    
                setAuthenticateInfo(this.user_name, this.pwd); //设置验证信息
                //发送
                try
                {
                    smtp_client.Send(createMailMessageWithAttacth(to, subject, body, fileNames));
                    return true;
                }
                catch (SmtpException err)
                {
                    return false;
                }
    
            }
    
    
        }
    }

    调用

    if (new SMTPHelper(this.textBox1.Text, this.textBox1.Text, this.textBox2.Text, false).doSend(this.textBox4.Text, this.textBox3.Text, this.richTextBox1.Text, fileNames))
    {
    MessageBox.Show("发送成功");
    }
    else
    {
    MessageBox.Show("发送失败");
    }

  • 相关阅读:
    组队开发最后冲刺周第一次会议
    android 本地数据库sqlite的封装
    java 空指针异常造成的原因有哪些
    jsp usebean的使用
    PHP模拟登录并获取数据
    php rsa加密解密实例
    30个php操作redis常用方法代码例子
    官方微信接口(全接口)
    curl类封装
    网站微信登录
  • 原文地址:https://www.cnblogs.com/pengshaomin/p/3021830.html
Copyright © 2020-2023  润新知