Skip to main content
 首页 » 编程设计

c#之打开包含附件的默认邮件管理器 (Windows)

2024年01月02日30pengyingh

我需要通过默认邮件管理器打开一封带有附件的新邮件(没有 SMTP 代码) 我使用:

System.Diagnostics.Process.Start(String.Format("mailto:{0}", txtEmail.Text))  

是否也可以添加附件?


我可以试试这个

http://www.codeproject.com/Articles/17561/Programmatically-adding-attachments-to-emails-in-C

但我应该明白,客户端计算机始终需要 Microsoft Outlook...

请您参考如下方法:

这不可能?!
我提供了使用 System.Net.Mail 命名空间的详细方法;

private void button1_Click(object sender, EventArgs e) 
{ 
    SmtpClient smtpserver = new SmtpClient(); 
    smtpserver.Credentials = new NetworkCredential("email@domain", "passphrase"); 
    smtpserver.Port = 587; 
    smtpserver.Host = "smtp.live.com"; 
    smtpserver.EnableSsl = true; 
 
    MailMessage mail = new MailMessage(); 
    mail.From = new MailAddress("email@domain"); 
    mail.To.Add("recipient@domian"); 
    mail.Subject = "testing"; 
 
    string pathTOAttachment; 
    string _Attachment = pathToAttachment; 
    Attachment oAttch = new Attachment(_Attachment); 
    mail.Attachments.Add(oAttch); 
 
    mail.Body = "message body"; 
 
    ThreadPool.QueueUserWorkItem(delegate 
    { 
        try 
        { 
            smtpserver.Send(mail); 
        } 
        //you can get more specific in here 
        catch 
        { 
            MessageBox.Show("failure sending message"); 
        } 
    }); 
} 

值得注意(未在此代码示例中考虑):

  1. 有些 isp 可能会对附件的大小施加限制,有些则确保您在尝试发送电子邮件之前检查它。
  2. smtp 主机/端口可能会有所不同,最有效的方法是检查定期更新的数据库,或者让用户自己设置。
  3. 线程部分是关于 UI 响应的,但是,如果用户在邮件仍在发送的情况下关闭主应用程序窗口,它将被抢占。