zoukankan      html  css  js  c++  java
  • 邮件发送

    前台界面:

    后台代码:(不同的邮件主机可能会需要端口号)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Mail;
    using System.Net;
    using System.Threading;
    
    namespace Mail
    {
        public partial class SendMail : Form
        {
            public SendMail()
            {
                InitializeComponent();
            }
    
            string[] FileNames
            {
                get;
                set;
            }
    
            //发送邮件按钮
            private void btnSend_Click(object sender, EventArgs e)
            {
                string to = tbTo.Text;
                string from = "xxx@sina.com";
                string subject = tbTheme.Text;
                string content = @tbContent.Text;
                MailMessage msg = new MailMessage(from, to, subject, content);
                msg.IsBodyHtml = true;
    
                //Thread threadMail = new Thread(new  ParameterizedThreadStart(SendEmail));
                //threadMail.Start(msg);
                ThreadPool.SetMaxThreads(0, 0);
                ThreadPool.QueueUserWorkItem(new WaitCallback(SendEmail), msg);
                btnSend.Enabled = false;
            }
    
            /// <summary>
            /// 异步发邮件
            /// </summary>
            /// <param name="message"></param>
            private void SendEmail(object message)
            {
                SmtpClient client = new SmtpClient("smtp.sina.com");//有可能需要端口号
                client.Credentials = new NetworkCredential("xxx@sina.com", "***");
                MailMessage msg = message as MailMessage;
                if (FileNames != null)
                {
                    foreach (string fileName in FileNames)
                    {
                        Attachment atta = new Attachment(fileName);
                        msg.Attachments.Add(atta);
                    }
                }
                client.Send(msg);
                this.Invoke(new ButtonEnableDelegate(EnableControl), new object[] { btnSend });
                //Thread.CurrentThread.Abort();
            }
    
            delegate void ButtonEnableDelegate(Control control);
    
            private void EnableControl(Control control)
            {
                control.Enabled = true;
            }
    
            //选择附件
            private void btnAttach_Click(object sender, EventArgs e)
            {
                OpenFileDialog dia = new OpenFileDialog();
                dia.Filter = "JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png";
                dia.RestoreDirectory = true;
                dia.Multiselect = true;
                if (dia.ShowDialog() == DialogResult.OK)
                {
                    if (dia.FileNames.Length == 0)
                    {
                        return;
                    }
                    this.FileNames = dia.FileNames;
                    this.tbAttach.Text = string.Empty;
                    foreach (string fileName in dia.FileNames)
                    {
                        tbAttach.Text += fileName.Substring(fileName.LastIndexOf('\\')+1) + ";";
                    }
                }
            }
        }
    }
  • 相关阅读:
    百度地图常用 获取中心点 缩放级别等
    sqlserver 临时表,多用户同时访问冲突吗?
    批量改ID 行形式
    C# post Json数据
    windows 激活venv问题
    spring 改变url
    conductor编译镜像
    springboot教程
    Microsoft Visual C++ Compiler for Python 2.7
    java 方法引用(method reference)
  • 原文地址:https://www.cnblogs.com/xingbinggong/p/2595652.html
Copyright © 2011-2022 走看看