0

我是一名新的 C# 开发人员,并试图在我的系统使用 Mail 功能发送的文本邮件中包含一个图像。一切正常,除非我将以下一堆代码添加到我的代码中:

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();
            }
        }

那么如何解决这个问题以便能够通过我的短信发送图像呢?

4

1 回答 1

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

你必须HtmlMediaTypeNames. 像这样。

AlternateView av = AlternateView.CreateAlternateViewFromString(
  body, null, MediaTypeNames.Text.Html);
于 2012-09-11T11:25:09.283 回答