C#。发送邮件时如何避免附加图像

时间:2015-05-29 05:11:28

标签: email email-attachments

我正在尝试从my.net应用程序发送电子邮件。 我在其中包含了一张图片。我在电子邮件中收到了图片。问题是图像也作为附件出现。 我只需要内嵌图像。没有附件。删除附件的任何选项? 我已经包含以下代码

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <h2>Gratisofferte</h2>
  <form role="form" class="form-horizontal">
    <div id="form1">
      <div class="form-group ">
        <div class="col-sm-4 col-xs-5">
          <label>Aktuelle Adresse</label>
          <div class=" has-feedback has-success">
            <input class="rightstraight form-control" placeholder="z.B. Paradiesstrasse 10" type="text" id="n_adr">
            <span id="fname1" class="glyphicon form-control-feedback glyphicon-ok"></span>
          </div>
        </div>
        <div class="col-sm-3 col-xs-4" style="margin-left:-30px;">
          <label>PLZ</label>
          <input class="leftstraight form-control" placeholder="PLZ" type="text" id="n_plz">
        </div>
        <div class="col-sm-5 col-xs-3">
          <label>Zimmer</label>
          <select class="slim form-control">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
          </select>


          <label>Etage</label>
          <select class="slim form-control">
            <option>E</option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
          </select>

          <label>Lift</label>
          <select class="slim form-control">
            <option>ja</option>
            <option>nein</option>
          </select>
        </div>
      </div>

      <div class="form-group">
        <div class="col-sm-12">
          <button id="next" type="submit" class="btn btn-info pull-right">weiter</button>
        </div>
      </div>

    </div>
  </form>
  <hr>
</div>

1 个答案:

答案 0 :(得分:0)

尝试以下两个选项中的任何一个:(Reference

选项1: -

System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepath\filename.png");
inline.ContentDisposition.Inline = true;

选项2: -

using (var client = new SmtpClient())
{
    MailMessage newMail = new MailMessage();
    newMail.To.Add(new MailAddress("you@your.address"));
    newMail.Subject = "Test Subject";
    newMail.IsBodyHtml = true;

    var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png"));
    inlineLogo.ContentId = Guid.NewGuid().ToString();

    string body = string.Format(@"
            <p>Lorum Ipsum Blah Blah</p>
            <img src=""cid:{0}"" />
            <p>Lorum Ipsum Blah Blah</p>
        ", inlineLogo.ContentId);

    var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
    view.LinkedResources.Add(inlineLogo);
    newMail.AlternateViews.Add(view);

    client.Send(newMail);
}