C#发送邮件(添加附件)!

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;
namespace WindowsFormsApplication63
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //打开上传附件的对话框
        private void btnUP_Click(object sender, EventArgs e)
        {
            oFDialogSFile.InitialDirectory = "C:\\";//设置对话框的初始目录为C盘
              oFDialogSFile.Filter = "all files (*.*)|*.*";//筛选字符串为所有文件
              oFDialogSFile.RestoreDirectory = true;
            oFDialogSFile.ShowDialog();
            cboxAccessories.Items.Add(oFDialogSFile.FileName.Trim());//当选择好文件后将文件名赋值给下拉框
        }
        //发送邮件
        private void btnSend_Click(object sender, EventArgs e)
        {
           
                try
          
  • 1
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
以下是一个使用C#编写的控制台程序,用于发送邮件: ```csharp using System; using System.Net; using System.Net.Mail; class Program { static void Main(string[] args) { // 设置邮件服务器 string smtpServer = args[0]; int smtpPort = 25; SmtpClient client = new SmtpClient(smtpServer, smtpPort); // 设置发件人 string from = args[1]; string displayName = args[2]; MailAddress fromAddress = new MailAddress(from, displayName); // 设置收件人 string to = args[3]; MailAddress toAddress = new MailAddress(to); // 创建邮件对象 MailMessage message = new MailMessage(fromAddress, toAddress); // 设置抄送和密送 if (args.Length > 4) { string[] cc = args[4].Split(','); foreach (string c in cc) { message.CC.Add(c); } } if (args.Length > 5) { string[] bcc = args[5].Split(','); foreach (string b in bcc) { message.Bcc.Add(b); } } // 设置主题和内容 message.Subject = args[6]; message.Body = args[7]; // 添加附件 if (args.Length > 8) { string[] attachments = args[8].Split(','); foreach (string attachment in attachments) { Attachment file = new Attachment(attachment); message.Attachments.Add(file); } } // 发送邮件 client.Send(message); Console.WriteLine("邮件发送成功!"); } } ``` 在命令行中输入以下命令即可发送邮件: ``` SendMail 邮件服务器IP 发件人 显示名称 收件人 抄送 密送 主题 内容 文件 ``` 其中,邮件服务器IP、发件人、显示名称、收件人、主题和内容是必填项,抄送、密送和文件是可选项。如果有多个收件人、抄送或密送,可以用逗号分隔。如果有多个文件,也可以用逗号分隔。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值