【问题标题】:How to open default Mail Client with prepopulated attachment in Asp.net如何在 Asp.net 中打开带有预填充附件的默认邮件客户端
【发布时间】:2017-08-10 16:05:14
【问题描述】:

我正在尝试在 Asp.net 中打开带有预填充附件的 default Mail Client

如果我在本地服务器上尝试这个,它工作正常,但是当部署到服务器时它不再工作了。

这是我到目前为止所做的:

 public void SendEmail()
    {
        try
        {

            int count = GridViewDocuments.Rows.Count;
            Outlook.Application oApp = new Outlook.Application();
            Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

            if (count > 0)
            {

                foreach (GridViewRow gr in GridViewDocuments.Rows)
                {
                    CheckBox chckEmail = (CheckBox)gr.FindControl("chckEmail");
                    Label lblDocumentId = (Label)gr.FindControl("lblDocumentId");
                    if (chckEmail.Checked == true)
                    {
                        string documentId = lblDocumentId.Text;
                        string DocumentTile = "";
                        int DocumentId = 0;
                        if (documentId != "")
                        {
                            DocumentId = Convert.ToInt32(documentId);
                        }
                        DocumentTile = GridViewDocuments.Rows[gr.RowIndex].Cells[1].Text;
                        EmailLogs(DocumentId, DocumentTile);
                        string filepath = GetDocumentByReceivingInquiryIdandReceivingInquiryForm(DocumentId);

                        //OpenFileDialog attachment = new OpenFileDialog();
                        if (filepath.Length > 0)
                        {
                            oMailItem.Attachments.Add(Server.MapPath(filepath),
                                Outlook.OlAttachmentType.olByValue,
                                1);

                        }
                        filepath = "";
                    }
                }
            }
            oMailItem.To = toEmail;
            oMailItem.Subject = Subject;

            InserLog("Sending Email By Sending Inquriy Workspace", SendingInqId.ToString());

        }
        catch (Exception ex)
        {

        }
    }

【问题讨论】:

  • 当您调用 new Outlook.Application() 时,它正在服务器中运行。这就是为什么当你在本地运行时它可以工作,但在部署时它显然不在客户端机器上运行(这将是一个巨大的安全漏洞,而且如果没有扩展,浏览器无论如何都不可能做到这一点)

标签: c# asp.net


【解决方案1】:

使用Williwyg 在this question 上的答案,我想知道它是否可以在asp.net 中工作。经过一些修改,它可以工作。这个 sn-p 将创建一个带附件的电子邮件,将其转换为 eml 文件并将其发送给客户端。

using System.Web;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Reflection;

protected void Button1_Click(object sender, EventArgs e)
{
    //create an email
    MailMessage message = new MailMessage();
    message.From = new MailAddress("your@email.com", "MyCompany");
    message.Subject = "Email subject goes here";
    message.IsBodyHtml = true;
    message.Body = "<html><head></head><body><font size=\"3\" color=\"#ff0000\">HTML formatted email body.</body></html>";

    //add the attachment
    message.Attachments.Add(new Attachment(Server.MapPath("test.pdf")));

    //get the message as byte array
    byte[] bin = getEmailAsEML(message);

    //send the email to the client as eml
    Response.ClearHeaders();
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "message/rfc822";
    Response.AddHeader("content-length", bin.Length.ToString());
    Response.AddHeader("content-disposition", "attachment; filename=\"email.eml\"");
    Response.OutputStream.Write(bin, 0, bin.Length);
    Response.Flush();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

public byte[] getEmailAsEML(MailMessage message)
{
    using (MemoryStream ms = new MemoryStream())
    {
        var binaryWriter = new BinaryWriter(ms);
        //Write the Unsent header to the file so the mail client knows this mail must be presented in "New message" mode
        binaryWriter.Write(Encoding.UTF8.GetBytes("X-Unsent: 1" + Environment.NewLine));

        var assembly = typeof(SmtpClient).Assembly;
        var mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");

        // Get reflection info for MailWriter contructor
        var mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);

        // Construct MailWriter object with our FileStream
        var mailWriter = mailWriterContructor.Invoke(new object[] { ms });

        // Get reflection info for Send() method on MailMessage
        var sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);

        sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { mailWriter, true, true }, null);

        // Finally get reflection info for Close() method on our MailWriter
        var closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);

        // Call close method
        closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null);

        return ms.ToArray();
    }
}

【讨论】:

  • 我尝试使用此代码时未设置来自,您无法填充它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2012-10-25
  • 2018-02-09
  • 2017-01-02
  • 1970-01-01
  • 2011-06-27
相关资源
最近更新 更多