Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在html正文中嵌入图像也需要显示_C#_Html_Email_Email Attachments_Document Body - Fatal编程技术网

C# 在html正文中嵌入图像也需要显示

C# 在html正文中嵌入图像也需要显示,c#,html,email,email-attachments,document-body,C#,Html,Email,Email Attachments,Document Body,我需要发送图片和文字混合的邮件。我的问题是不能在身体内部发送图像,它总是出现在身体底部 所以我的问题是如何在邮件正文中发送写位置图片 下面是一些不太好用的代码 using System; using System.Collections.Generic; using System.IO; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Tex

我需要发送图片和文字混合的邮件。我的问题是不能在身体内部发送图像,它总是出现在身体底部

所以我的问题是如何在邮件正文中发送写位置图片

下面是一些不太好用的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Threading;
using System.Web;
using System.Net.Mime;
using WindowsFormsApplication5.Properties;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage();

        string To;

        To = Properties.Settings.Default.To.ToString();

        msg.To.Add("some.name@gmail.com");
        msg.From = new MailAddress("your.adress@gmail.com");
        msg.Subject = "Subject";

        string path = "C:\\root.jpg"; // my logo is placed in images folder

        LinkedResource logo = new LinkedResource(path, MediaTypeNames.Image.Jpeg);
        logo.ContentId = "companylogo";
        logo.TransferEncoding = TransferEncoding.Base64;

        //now do the HTML formatting
        AlternateView av1 = AlternateView.CreateAlternateViewFromString(
              "<html><body>" + "<p>This will have to be before picture</p>" + "<img src=cid:companylogo/>" + "<p>This will have to be after pictures.</p>" +
              "<br></body></html>",
              null, MediaTypeNames.Text.Html);

        //now add the AlternateView
        av1.LinkedResources.Add(logo);

        //now append it to the body of the mail
        msg.AlternateViews.Add(av1);
        msg.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";

        smtp.Port = 587;

        //for gmail
        smtp.EnableSsl = true;
        smtp.Credentials = new System.Net.NetworkCredential(
             "your.adress@gmail.com",
             "password");

        smtp.Send(msg);
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Web;
Net系统;
使用System.Net.Mail;
使用系统线程;
使用System.Web;
使用System.Net.Mime;
使用WindowsFormsApplication5.属性;
命名空间Windows窗体应用程序5
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
MailMessage msg=新的MailMessage();
串到;
To=Properties.Settings.Default.To.ToString();
msg.To.Add(“一些。name@gmail.com");
msg.From=新邮件地址(“您的。adress@gmail.com");
msg.Subject=“Subject”;
string path=“C:\\root.jpg”;//我的徽标放在“图像”文件夹中
LinkedResource徽标=新LinkedResource(路径,MediaTypeNames.Image.Jpeg);
logo.ContentId=“companylogo”;
logo.transferncode=transferncode.Base64;
//现在进行HTML格式化
AlternateView av1=AlternateView.CreateAlternateView-FromString(
“+”这必须在图片之前

“+”这必须在图片之后。

”+ “
”, null,MediaTypeNames.Text.Html); //现在添加AlternateView av1.LinkedResources.Add(徽标); //现在将其附加到邮件正文中 msg.AlternateViews.Add(av1); msg.IsBodyHtml=true; SmtpClient smtp=新SmtpClient(); smtp.Host=“smtp.gmail.com”; smtp.Port=587; //对于gmail smtp.EnableSsl=true; smtp.Credentials=new System.Net.NetworkCredential( “你的。adress@gmail.com", “密码”); smtp.Send(msg); } } }
我在gmail客户端上进行了测试,结果如下:


您使用哪些电子邮件客户端对此进行了测试?尝试发布完整的原始输出吗?我使用gmail客户端。。。查看问题更新,您是否在Gmail中显示图像?或者将它添加到安全发件人列表中?我用gmail显示它,它的外观如下图所示