2012-07-29 64 views
0

我想要的是發送一封電子郵件與嵌入式圖像。我正在努力,我不知道爲什麼我不能得到它。該系統將發送電子郵件,但不顯示圖像,這裏是結果的快照:如何使用C#中的郵件功能發送帶嵌入式圖像的電子郵件?

enter image description here

能否請你幫我修改如下處理髮送該圖像的代碼?讓我們假設我們有任何形象。

C#代碼:

protected void Page_Load(object sender, EventArgs e) 
    { 
     Send(); 
    } 


    protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml, AlternateView av) 
    { 
     SmtpClient sc = new SmtpClient("MailAddress"); 
     try 
     { 
      MailMessage msg = new MailMessage(); 
      msg.From = new MailAddress("[email protected]", "Test Sys"); 

      //QuizLink is appSetting inside your web config 
      string newLink = System.Configuration.ConfigurationManager.AppSettings["QuizLink"].ToString(); 


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

    } 

    protected void SendEmailTOAllUser() 
    { 
     string connString = "Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True"; 

     using (SqlConnection conn = new SqlConnection(connString)) 
     { 
      var sbEmailAddresses = new System.Text.StringBuilder(2000); 
      string quizid = ""; 

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

      string cmdText = "SELECT MIN (QuizID) As mQuizID FROM dbo.QUIZ WHERE IsSent <> 1"; 
      using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 
      { 
       SqlDataReader reader = cmd.ExecuteReader(); 
       if (reader != null) 
       { 
        while (reader.Read()) 
        { 
         // There is only 1 column, so just retrieve it using the ordinal position 
         quizid = reader["mQuizID"].ToString(); 

        } 
       } 
       reader.Close(); 
      } 

      string cmdText2 = "SELECT Username FROM dbo.employee"; 
      using (SqlCommand cmd = new SqlCommand(cmdText2, conn)) 
      { 
       SqlDataReader reader = cmd.ExecuteReader(); 
       if (reader != null) 
       { 
        while (reader.Read()) 
        { 
         var sName = reader.GetString(0); 
         if (!string.IsNullOrEmpty(sName)) 
         { 
          if (sbEmailAddresses.Length != 0) 
          { 
           sbEmailAddresses.Append(","); 
          } 
          // Just use the ordinal position for the user name since there is only 1 column 
          sbEmailAddresses.Append(sName).Append("@mailAddress.com"); 
         } 
        } 
       } 
       reader.Close(); 
      } 

      string cmdText3 = "UPDATE dbo.Quiz SET IsSent = 1 WHERE QuizId = @QuizID"; 
      using (SqlCommand cmd = new SqlCommand(cmdText3, conn)) 
      { 
       // Add the parameter to the command 
       var oParameter = cmd.Parameters.Add("@QuizID", SqlDbType.Int); 

       var sEMailAddresses = sbEmailAddresses.ToString(); 
       string link = "<a href='http://localhost/test.aspx?testid=" + quizid + "'> Click here to participate </a>"; 
       string body = @"................"; 
       AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html); 
       LinkedResource lr = new LinkedResource("contactUs.gif", MediaTypeNames.Image.Gif); 
       lr.ContentId="image1"; 
       av.LinkedResources.Add(lr); 


       int sendCount = 0; 
       List<string> addressList = new List<string>(sEMailAddresses.Split(',')); 
       StringBuilder addressesToSend = new StringBuilder(); 

       if (!string.IsNullOrEmpty(quizid)) 
       { 
        for (int userIndex = 0; userIndex < addressList.Count; userIndex++) 
        { 
         sendCount++; 
         if (addressesToSend.Length > 0) 
          addressesToSend.Append(","); 

         addressesToSend.Append(addressList[userIndex]); 
         if (sendCount == 10 || userIndex == addressList.Count - 1) 
         { 
          SendEmail(addressesToSend.ToString(), "", "........", body, true, av); 
          addressesToSend.Clear(); 
          sendCount = 0; 
         } 
        } 

        // Update the parameter for the current quiz 
        oParameter.Value = quizid; 
        // And execute the command 
        cmd.ExecuteNonQuery(); 
       } 

      } 
      conn.Close(); 
     } 
    } 
+1

你好,你有沒有嘗試用該使用Server.Mappath使用Server.Mappath是引用您的圖像(@「圖像/ contactUs.jpg」);而不是(「D:\\ inetpub \\ wwwroot \\ msa \\ PSSP \\ images \\ contactUs.gif」? – 2012-07-29 05:08:14

回答

0

縱觀所有exapmle它看起來像您發送這樣/somefolder/image.jpg圖像,你不能這樣,因爲用戶正在閱讀從另一個域名的圖像發送這很可能不是你的,所以如果你看看你的圖像的源代碼,你會發現它與你使用的不同。

你應該發送圖像鏈接這樣

<img src='http://www.mydomainname.com/fulladdress/someimage.png usemap' ='#clickMap'> 
相關問題