如何使用asp.net mvc webapi发送电子邮件附件 [英] how to send an email attachment using asp.net mvc webapi

查看:127
本文介绍了如何使用asp.net mvc webapi发送电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASPMVC webapi应用程序,客户端将在其中附加附件。这个附件是表格byte []。但是在服务器端我们无法收到附件。



这是客户代码。

 StringBuilder sbAPIRequest =  new  StringBuilder(); 
sbAPIRequest.Append( http:// localhost:58229 / api / SendEmailMessage) ;
sbAPIRequest.Append( & From =);
sbAPIRequest.Append(request.From);
sbAPIRequest.Append( & Subject =);
sbAPIRequest.Append(request.Subject);
sbAPIRequest.Append( & To =);
sbAPIRequest.Append(request.To);
sbAPIRequest.Append( & FileName =);
sbAPIRequest.Append(request.FileName);
sbAPIRequest.Append( & Body =);
sbAPIRequest.Append(request.Body);
sbAPIRequest.Append( & Data =);
sbAPIRequest.Append(System.IO.File.ReadAllBytes( File Path)) ;
HttpWebRequest httpWReq =(HttpWebRequest)WebRequest.Create(sbAPIRequest.ToString());
httpWReq.Method = GET;
httpWReq.ContentType = application / json; charset = UTF-8;
httpWReq.ContentLength = 0 ;
HttpWebResponse response =(HttpWebResponse)httpWReq.GetResponse();







这是服务边码,即ASPMVC webapi



  public  IEnumerable< string>获取([FromUri] Models.EmailMessageRequest emailMessage)
{
MailMessage _MailMessage = new MailMessage();
System.Net.Mail.Attachment附件;
List< string> Messages = new List< string>();
try
{
UpdateMailAddressCollection(_MailMessage.Bcc,emailMessage.Bcc);
_MailMessage.Body = emailMessage.Body;
UpdateMailAddressCollection(_MailMessage.CC,emailMessage.CC);
_MailMessage.From = getMailAddress(emailMessage.From);
_MailMessage.IsBodyHtml = emailMessage.IsBodyHtml;
_MailMessage.Subject = emailMessage.Subject;
UpdateMailAddressCollection(_MailMessage.To,emailMessage.To);
_MailMessage.Priority = getPriority(emailMessage.Priority);

if (emailMessage.Data!= null &&&&#emailMessage。数据[ 0 ]!= 0
{
Stream _Stream = new MemoryStream(emailMessage.Data);
attachment = new 附件(_Stream,emailMessage.FileName);
_MailMessage.Attachments.Add(附件);
}
string _smtp_host =( new S2.Services.Contracts.Schemas。 Customer.AppSettingHelper())smtp_host。
SmtpClient client = new SmtpClient(_smtp_host);
client.Send(_MailMessage);
response.Success = true ;
response.Messages.Add( 电子邮件已成功发送。);
}
catch (例外情况)
{
response.Messages.Add( 内部异常);
response.Success = false ;
}

Messages.Add(response.Messages.Select(x = > x).FirstOrDefault()+ 成功: + response.Success);
return Messages.ToList();
}

解决方案

你必须使用httpPostedFileBased在mvc中获取服务器端的完整文件信息。



您也可以参考以下链接



http://stackoverflow.com/questions/16030034/asp-净-MVC读取文件从 - httppostedfilebase-而不保存

I have an ASPMVC webapi application in which the client will attach an attachment. this attachment is the form byte[]. But on the server side we are not able to receive the attachment.

Here is the client code.

StringBuilder sbAPIRequest = new StringBuilder();
sbAPIRequest.Append("http://localhost:58229/api/SendEmailMessage");
sbAPIRequest.Append("&From=");
sbAPIRequest.Append(request.From);
sbAPIRequest.Append("&Subject=");
sbAPIRequest.Append(request.Subject);
sbAPIRequest.Append("&To=");
sbAPIRequest.Append(request.To);
sbAPIRequest.Append("&FileName=");
sbAPIRequest.Append(request.FileName);
sbAPIRequest.Append("&Body=");
sbAPIRequest.Append(request.Body);
sbAPIRequest.Append("&Data=");
sbAPIRequest.Append(System.IO.File.ReadAllBytes("File Path"));
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(sbAPIRequest.ToString());
httpWReq.Method = "GET";
httpWReq.ContentType = "application/json;charset=UTF-8";
httpWReq.ContentLength = 0;
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();




Here is the service Side Code i.e. ASPMVC webapi

public IEnumerable<string> Get([FromUri] Models.EmailMessageRequest emailMessage)
        {
            MailMessage _MailMessage = new MailMessage();
            System.Net.Mail.Attachment attachment;
            List<string> Messages = new List<string>();    
            try
            {
                        UpdateMailAddressCollection(_MailMessage.Bcc, emailMessage.Bcc);
                        _MailMessage.Body = emailMessage.Body;
                        UpdateMailAddressCollection(_MailMessage.CC, emailMessage.CC);
                        _MailMessage.From = getMailAddress(emailMessage.From);
                        _MailMessage.IsBodyHtml = emailMessage.IsBodyHtml;
                        _MailMessage.Subject = emailMessage.Subject;
                        UpdateMailAddressCollection(_MailMessage.To, emailMessage.To);
                        _MailMessage.Priority = getPriority(emailMessage.Priority);

                        if (emailMessage.Data != null && emailMessage.Data[0] != 0)
                        {
                            Stream _Stream = new MemoryStream(emailMessage.Data);
                            attachment = new Attachment(_Stream, emailMessage.FileName);
                            _MailMessage.Attachments.Add(attachment);
                        }
                        string _smtp_host = (new S2.Services.Contracts.Schemas.Customer.AppSettingHelper()).smtp_host;
                        SmtpClient client = new SmtpClient(_smtp_host);
                        client.Send(_MailMessage);
                        response.Success = true;
                        response.Messages.Add("Email Sent successfully.");
            }
            catch (Exception ex)
            {
                response.Messages.Add("Internal Exception");
                response.Success = false;
            }

            Messages.Add(response.Messages.Select(x => x).FirstOrDefault() + " Success:" + response.Success);
            return Messages.ToList();
        }

解决方案

you have to use httpPostedFileBased to get the full file information on the server side in mvc.

you can also refer the link below

http://stackoverflow.com/questions/16030034/asp-net-mvc-read-file-from-httppostedfilebase-without-save


这篇关于如何使用asp.net mvc webapi发送电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆