如何使用MVC webapi发送带有电子邮件的附件

时间:2015-03-11 06:04:21

标签: c# asp.net asp.net-mvc asp.net-web-api

附件大小最大可达1 MB。 这里客户端应用程序可以是网站或Windows应用程序

2 个答案:

答案 0 :(得分:0)

使用SmtpClient:https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient%28v=vs.110%29.aspx

附件:https://msdn.microsoft.com/en-us/library/system.net.mail.attachment(v=vs.110).aspx

备注部分显示了一些例子:

    public static void CreateMessageWithAttachment(string server)
    {
        // Specify the file to be attached and sent. 
        // This example assumes that a file named Data.xls exists in the 
        // current working directory. 
        string file = "data.xls";
        // Create a message and set up the recipients.
        MailMessage message = new MailMessage(
           "jane@contoso.com",
           "ben@contoso.com",
           "Quarterly data report.",
           "See the attached spreadsheet.");

        // Create  the file attachment for this e-mail message.
        Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
        // Add time stamp information for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(file);
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
        disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
        // Add the file attachment to this e-mail message.
        message.Attachments.Add(data);

        //Send the message.
        SmtpClient client = new SmtpClient(server);
        // Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;

  try {
          client.Send(message);
        }
        catch (Exception ex) {
          Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", 
                ex.ToString() );              
        }
        // Display the values in the ContentDisposition for the attachment.
        ContentDisposition cd = data.ContentDisposition;
        Console.WriteLine("Content disposition");
        Console.WriteLine(cd.ToString());
        Console.WriteLine("File {0}", cd.FileName);
        Console.WriteLine("Size {0}", cd.Size);
        Console.WriteLine("Creation {0}", cd.CreationDate);
        Console.WriteLine("Modification {0}", cd.ModificationDate);
        Console.WriteLine("Read {0}", cd.ReadDate);
        Console.WriteLine("Inline {0}", cd.Inline);
        Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
        foreach (DictionaryEntry d in cd.Parameters)
        {
            Console.WriteLine("{0} = {1}", d.Key, d.Value);
        }
        data.Dispose();
    }

答案 1 :(得分:0)

要使用WebAPI发送,您需要按顺序执行以下操作

1。)使用Jquery AJAX向WebAPI发帖。

type: "POST",         // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs  
contentType: false,   // The content type used when sending data to the server.
cache: false,         // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data)   // A function to be called if request succeeds

2.。)从您在业务层中编写的其中一个Helper类进行SMTP调用。

如果您可以分享已有的内容,我们可以就此进行更多讨论。如果不使用上面的逻辑步骤。