简体   繁体   English

从ASP.NET网站使用c#从默认的电子邮件客户端(例如Outlook,..)发送电子邮件

[英]Sending e-mail from default Email Client (ex. Outlook,.. ) using c# from an ASP.NET web site

I'm working on my very first project, and I need to add a function as the button_click event. 我正在做我的第一个项目,我需要添加一个函数作为button_click事件。 Function should open a "send new e-mail" form of the default email client, empty, without any destination,subject or body just with an attached file. 函数应打开默认电子邮件客户端的“发送新电子邮件”形式,为空,没有任何目的地,主题或正文,仅带有附件。

I went through many similar tutorial on the stackoverflow and codeproject but couldn't solve it. 我经历了许多关于stackoverflow和codeproject的类似教程,但无法解决。 I found similar functions which send a message from code, but not just open an empty e-mail form and attach the required file. 我发现类似的功能可以通过代码发送消息,而不仅仅是打开一个空的电子邮件表单并附加所需的文件。 But couldn't modify successfully. 但是无法成功修改。

I'm sure there are people looking for this kind of solution as well. 我敢肯定有人也在寻找这种解决方案。

what I tried so far is : 到目前为止,我尝试过的是:

protected void btnSend_Click(object sender, EventArgs e)
{
    string value;
    value = lstpdfList.SelectedItem.Text;
    string file = "W:/" + value + ".pdf";

    MailMessage message = new MailMessage();
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
    ContentDisposition disposition = data.ContentDisposition;
    disposition.CreationDate = System.IO.File.GetCreationTime(file);
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
    disposition.ReadDate = System.IO.File.GetLastWriteTime(file);
    message.Attachments.Add(data);
}

You can't attach a file from ASP.net to outlook, it's a security issue. 您不能将ASP.net中的文件附加到Outlook,这是一个安全问题。

If you have access to the Exchange Web Services you can interact directly with Exchange to send an e-mail from that users account with attachments etc. 如果您有权访问Exchange Web服务,则可以直接与Exchange进行交互,以从该用户帐户发送带有附件等的电子邮件。

You may have to delegate access to the user account used to execute the ASP.NET request to successfully be able to interact with the Exchange Server Services, you could use ASP.net impersonate as well. 您可能必须委派对用于执行ASP.NET请求的用户帐户的访问权限,才能成功与Exchange Server服务进行交互,也可以使用ASP.net模拟。

Check out the documentation at: 在以下位置查看文档:

http://msdn.microsoft.com/en-us/library/exchange/bb409286(v=exchg.140).aspx http://msdn.microsoft.com/zh-cn/library/exchange/bb409286(v=exchg.140).aspx

You can't automate Outlook on the client-side from a web application. 您无法通过Web应用程序在客户端上自动执行Outlook。 And you shouldn't invoke Outlook on the server. 而且您不应该在服务器上调用Outlook。

What you can do however is to send an email from the web server, without involving Outlook. 但是,您可以做的是从Web服务器发送电子邮件,而不涉及Outlook。

To do this, just follow the example on MSDN for SmtpClient.Send() . 为此,只需遵循MSDN上SmtpClient.Send()示例

There's also an example of programmatically creating a MailMessage with attachments here . 还有以编程方式创建的一个例子MailMessage带附件的位置

public static void CreateTestMessage2(string server)
{
    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    MailMessage message = new MailMessage(from, to);
    message.Subject = "Using the new SMTP client.";
    message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
    SmtpClient client = new SmtpClient(server);
    // Credentials are necessary if the server requires the client  
    // to authenticate before it will send e-mail on the client's behalf.
    client.UseDefaultCredentials = true;

    try 
    {
        client.Send(message);
    }  
    catch (Exception ex) 
    {
        Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString() );           
    }              
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM