Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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# 如何使用此定制的C Mail功能发送图像?_C#_Image - Fatal编程技术网

C# 如何使用此定制的C Mail功能发送图像?

C# 如何使用此定制的C Mail功能发送图像?,c#,image,C#,Image,我是一个新的C开发人员,试图在文本邮件中包含一个图像,我的系统正在使用邮件功能发送它。除了我在代码中添加了以下一组代码外,其他一切都正常工作: string imagePath = Server.MapPath("~") + "\\"; string fileName = imagePath + "EmailNotification.jpg"; AlternateView av = AlternateView.CreateAlter

我是一个新的C开发人员,试图在文本邮件中包含一个图像,我的系统正在使用邮件功能发送它。除了我在代码中添加了以下一组代码外,其他一切都正常工作:

string imagePath = Server.MapPath("~") + "\\";
                string fileName = imagePath + "EmailNotification.jpg";
                AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text)
                LinkedResource linkedRes = new LinkedResource(fileName);
                linkedRes.ContentId = "image1";
                linkedRes.ContentType.Name = fileName;
                av.LinkedResources.Add(linkedRes);
它在下面一行给了我一条红线:

    AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text)



C# Mail Function:

    /*For sending an email notification to the new user*/
        protected void SendNotificationByEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
        {
            SmtpClient sc = new SmtpClient("MailServer");
            try
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress("Test@MailServer.com", "Test System)");


                msg.Bcc.Add(toAddresses);
                msg.Subject = MailSubject;
                msg.Body = MessageBody;
                msg.IsBodyHtml = isBodyHtml;
                sc.Send(msg);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        protected void Send(string username)
        {
            string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=TestDB;Integrated Security=True";

            string networkID = username.ToString();
            using (SqlConnection conn = new SqlConnection(connString))
            {
                var sbEmailAddresses = new System.Text.StringBuilder(2000);

                //initiate the varibles 
                string name = null;

                // Open DB connection.
                conn.Open();

                string cmdText2 = @"SELECT     Name
                                    FROM       dbo.employee
                                    WHERE     (Username = @networkID)";
                using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
                {
                    cmd.Parameters.AddWithValue("@networkID", networkID);
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader != null)
                    {
                        if (reader.Read())
                        {
                            name = reader["Name"].ToString();
                            sbEmailAddresses.Append(username).Append("@mailServer.com");
                        }
                    }

                    //var sEMailAddresses = sbEmailAddresses.ToString();
                    string body = @"Good day "
                                    + name +
                                    @", <br /><br />
                                    You have been added to the <a href='http://localhost/TestSys'>Test</a>. 
                                    <br /><br />
resources. 
                                    </b> <br /> <br />
                                    <img src='images/Admin/EmailNotification.jpg' alt=' Message'/>

                    string imagePath = Server.MapPath("~") + "\\";
                    string fileName = imagePath + "EmailNotification.jpg";
                    AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text)
                    LinkedResource linkedRes = new LinkedResource(fileName);
                    linkedRes.ContentId = "image1";
                    linkedRes.ContentType.Name = fileName;
                    av.LinkedResources.Add(linkedRes);

                    SendNotificationByEmail(sbEmailAddresses.ToString(), "", "Welcome", body, true);
                    sbEmailAddresses.Clear();
                    reader.Close();


                }

                conn.Close();
            }
        }
那么,如何解决这个问题,以便能够通过我的文本邮件发送图像呢

您只需在MediaTypeName之后编写Html。像这样

AlternateView av = AlternateView.CreateAlternateViewFromString(
  body, null, MediaTypeNames.Text.Html);
您只需在MediaTypeName之后编写Html。像这样

AlternateView av = AlternateView.CreateAlternateViewFromString(
  body, null, MediaTypeNames.Text.Html);