繁体   English   中英

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

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

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

我经历了许多关于stackoverflow和codeproject的类似教程,但无法解决。 我发现类似的功能可以通过代码发送消息,而不仅仅是打开一个空的电子邮件表单并附加所需的文件。 但是无法成功修改。

我敢肯定有人也在寻找这种解决方案。

到目前为止,我尝试过的是:

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

您不能将ASP.net中的文件附加到Outlook,这是一个安全问题。

如果您有权访问Exchange Web服务,则可以直接与Exchange进行交互,以从该用户帐户发送带有附件等的电子邮件。

您可能必须委派对用于执行ASP.NET请求的用户帐户的访问权限,才能成功与Exchange Server服务进行交互,也可以使用ASP.net模拟。

在以下位置查看文档:

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

您无法通过Web应用程序在客户端上自动执行Outlook。 而且您不应该在服务器上调用Outlook。

但是,您可以做的是从Web服务器发送电子邮件,而不涉及Outlook。

为此,只需遵循MSDN上SmtpClient.Send()示例

还有以编程方式创建的一个例子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