使用asp.net mvc通过Godaddy上的Godaddy电子邮件工作区发送带有附件的电子邮件

时间:2016-12-31 12:50:17

标签: c# asp.net asp.net-mvc email asp.net-mvc-4

我试图通过godaddy工作区电子邮件帐户发送电子邮件,使用asp.net mvc在godaddy上传附件。邮件已发送但上传的文件未正确附加。

在视图级别

<form action="@Url.Action("index")" method="post" data-form-title="CONTACT FORM" enctype="multipart/form-data">
                    @Html.AntiForgeryToken()

                       ...other page inputs..


                    <div class=" row row-sm-offset">
                        <div class="col-xs-12 col-md-12">
                            <div class="form-group">
                                <label class="form-control-label" for="form1-h-name">Upload Design (Properties: Images Type - PNG, JPEG; Max Size - 500kb )</label>
                                @Html.TextBoxFor(x => x.Design,
                               new
                               {
                                   @class = "form-control",
                                   type = "file",
                                   required = "true"
                               })
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-control-label" for="form1-h-message">Enter Message (Optional)</label>
                        @Html.TextAreaFor(x => x.AdditionalMessage, new
                   {
                       @class = "form-control",
                       placeholder = "Enter additional message"
                   })
                    </div>                      
                    <div><button type="submit" class="btn btn-primary">Send Job</button></div>
                </form>

模型类

 public class SendPrintJob
{
    other properties of the model class..

    [Required]
    [DataType(DataType.Date)]
    public DateTime DeliveryDate { get; set; }

    [Required]
    public HttpPostedFileBase Design { get; set; }       

    public string AdditionalMessage { get; set; }
}

在控制器级别

[HttpPost, ActionName("index")][ValidateAntiForgeryToken]
public async Task<ActionResult> index_post(SendPrintJob obj)
{
        papertypeList();
        if (ModelState.IsValid)
        {
            var date = DateTime.Now;
            int dateresult = DateTime.Compare(date, obj.DeliveryDate);
            if (dateresult > 0)
            {
                FlashMessage.Danger("Delivery Date Error: Delivery date entered should be later than todays date!");
                return View(obj);
            }
            string uploadErrorMsg = string.Empty;
            string uploadedImageFormat;
            if(!isValidUpload(obj.Design, out uploadErrorMsg, out uploadedImageFormat))
            {
                FlashMessage.Danger("Upload Error", uploadErrorMsg);                    
            }
            var body = "<p>Print Job<br/><br/>" +
                    "<b>SENDER DETAILS</b><br/>" +
                    "Name: {0}<br/>" +
                    "Email: {1}<br/>" +
                    "Phone Number: {2}<br/><br/>" +
                    "<b>JOB DETAILS</b><br>" +
                    "Delivery Date: {3}<br/>" +
                    "Paper Type: {4}<br/>" +
                    "Paper/Image Size: {5}<br/>" +
                    "Print Copies Required: {6}<br/>" +
                    "Trimming Required: {7}<br/><br/>" +
                    "Additional Message:<br/>" +
                    "{8}<br/><br/>" +
                    "Download email attachment for Design<br/>";
            string add_Msg = null;

            if (string.IsNullOrWhiteSpace(obj.AdditionalMessage))
                add_Msg = "No Message!";
            else
                add_Msg = obj.AdditionalMessage;

            var message = new MailMessage();
            message.Body = string.Format(body, obj.Name, obj.email, obj.Phone,
                obj.DeliveryDate.ToLongDateString(), obj.papertype, obj.paper_imagesize,
                obj.numberofcopies, obj.triming, add_Msg);

            message.To.Add(new MailAddress("reciepient's email address here"));
            message.IsBodyHtml = true;                
            message.Subject = "Print job sent as at - "+date.ToLongDateString()+" / "+date.ToShortTimeString();
            if(obj.Design != null)                
                message.Attachments.Add(new Attachment(obj.Design.InputStream, Path.GetFileName(obj.Design.FileName)));

            var smtp = new SmtpClient();
            await smtp.SendMailAsync(message);
            FlashMessage.Confirmation("Print Job sent successfully.");
            return RedirectToAction("index");
        }
        return View(obj);
    }

电子邮件已成功发送,但附带的图片有0个字节

uploaded email attachment

1 个答案:

答案 0 :(得分:0)

这在输入流上使用搜索方法非常有效

 if (obj.Design != null)
 {
     var stream = obj.Design.InputStream;
     stream.Seek(0, SeekOrigin.Begin);
     message.Attachments.Add(new Attachment(stream, Path.GetFileName(obj.Design.FileName)));
 }

找到了答案here