2

我需要通过我使用的默认邮件管理器(没有 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 ......

4

1 回答 1

0

不可能 ?!
我提供了使用 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 响应的,但是,如果用户在邮件仍在发送的情况下关闭主应用程序窗口,它将被抢占。
于 2012-09-03T13:07:18.143 回答