【问题标题】:Select particular image from Database and attach it to an email in ASP.NET MVC 5从数据库中选择特定图像并将其附加到 ASP.NET MVC 5 中的电子邮件
【发布时间】:2015-06-16 03:30:31
【问题描述】:

我正在开发一个类似这样的图像共享网络应用程序:

1) 用户可以从不同的类别(A、B、C、D)中进行选择

2) 选择类别后,用户将被重定向到一个不同的页面,该页面将列出该类别中的所有图像(图像到我的 localdb)

3) 在这里我想添加图片、描述和按钮。该按钮需要选择要附加到电子邮件的图像。剩下的就是填写电子邮件的收件人、主题和正文部分。这是我的问题。我不知道如何自动将该图像附加到电子邮件中。

4) 发送电子邮件

到目前为止,我有这个:

型号:

public class MailModel
{
    public string From { get; set; }
    public string To { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

控制器:

public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ViewResult Index(NASAWebAppDemo.Models.MailModel _objModelMail, HttpPostedFileBase fileUploader)
    {
        if (ModelState.IsValid)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(_objModelMail.To);
            mail.From = new MailAddress(_objModelMail.From);
            mail.Subject = _objModelMail.Subject;
            string Body = _objModelMail.Body;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("something@gmail.com", "MyPass");
            smtp.EnableSsl = true;
            smtp.Send(mail);
            return View("Index", _objModelMail);
}
else
{
    return View();
}

还有观点:

@model NASAWebAppDemo.Models.MailModel
@{
    ViewBag.Title = "Email";
}
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script>
    $(document).ready(function () {
        if ('@ViewBag.Message' == 'Sent') {
            alert('Mail has been sent successfully');
        }
    });
</script>

<h2>Index</h2>
<legend>Send Email </legend>



@using (@Html.BeginForm("Index", "SendMailer", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" })) 
{ 
    @Html.ValidationSummary() <input type="submit" value="Send" />
}



<table>
    <tbody>
        <tr>
            <td>To:</td>
            <td>@Html.TextBoxFor(m => m.To)</td>
        </tr>
        <tr>
            <td>Subject:</td>
            <td>@Html.TextBoxFor(m => m.Subject)</td>
        </tr>
        <tr>
            <td>Attachment</td>
            <td><input name="fileUploader" type="file" /></td>
        </tr>
        <tr>
            <td>Body:</td>
            <td>@Html.TextAreaFor(m => m.Body)</td>
        </tr>
    </tbody>
</table>

【问题讨论】:

    标签: asp.net asp.net-mvc email attachment


    【解决方案1】:

    MailMessage 类具有 Attachments 属性。你应该把你的形象放在那里。

    这个article 描述了如何从发布请求中附加图像。如果你的数据库中有它,你需要发送它的 ID(或者可以帮助你找到它的东西)并附加它从数据库加载。 Here 是从流创建附件的示例。

    【讨论】:

    • 这很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2016-12-29
    相关资源
    最近更新 更多