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

时间:2021-12-22 01:40:23

I'm working on my very first project, and I need to add a function as the button_click event. 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.

我正在处理我的第一个项目,我需要添加一个函数作为button_click事件。功能应打开默认电子邮件客户端的“发送新电子邮件”表单,为空,没有任何目的地,主题或正文附加文件。

I went through many similar tutorial on the * and codeproject but couldn't solve it. 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.

我在*和codeproject上经历了许多类似的教程,但无法解决它。我发现类似的函数从代码发送消息,但不只是打开一个空的电子邮件表单并附加所需的文件。但无法成功修改。

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);
}

2 个解决方案

#1


1  

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

#2


-1  

You can't automate Outlook on the client-side from a web application. And you shouldn't invoke Outlook on the server.

您无法从Web应用程序在客户端自动化Outlook。并且您不应该在服务器上调用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() );           
    }              
}

#1


1  

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

#2


-1  

You can't automate Outlook on the client-side from a web application. And you shouldn't invoke Outlook on the server.

您无法从Web应用程序在客户端自动化Outlook。并且您不应该在服务器上调用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() );           
    }              
}