question

Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 asked azure-cxp-api edited

mailItem issue

Hi,
What to adjust to code like

due to this error?

Error CS0165 Use of unassigned local variable 'mailItem'

                      if (Pos0 > 0 && Pos1 > 0)
                      {
                          if (Pos1 - Pos0 - 17 > 1)
                          {
                              Str0 = Line0.Substring(Pos0 + 18, Pos1 - Pos0 - 19);Pos2 = Str0.Trim().Length;
                              for (int j = 0; j <l7.Count(); j++)
                              {
                                  if (l7[j].Trim().Length>=Str0.Trim().Length)
                                  {
                                      if (l7[j].Substring(0,Pos2)==Str0)
                                      {
                                          Pos3 = l7[j].IndexOf(",,");Pos4 = l7[j].Trim().Length;
                                          if (Pos3>0)
                                          {
                                              mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                                              Email = l7[j].Substring(Pos3 + 2, Pos4 - 3 - Pos3);
                                              if (mailItem.EntryID != null)
                                              {
                                                  mailItem.Subject = "Coarri Codeco Issue ";
                                                  //mailItem.Send();
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      }
                      else
                      {
                          if (mailItem.EntryID != null)
                          {
                              mailItem.HTMLBody = mailItem.HTMLBody + Line0;
                              //mailItem.Send();
                          }
                      }

Error is owing this line

                          if (mailItem.EntryID != null)


dotnet-csharpwindows-forms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is this C#, not Visual Basic.

0 Votes 0 ·

Hi,
It is the same Email message, right? Do I need to repeat

mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

before the line having error?

0 Votes 0 ·

Hi @Jackson1990-7147,
The mailItem is not initialized in the else statement, so you cannot set the HTMLBody attribute. So you need to first assign a value to the mailItem through mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem).
Best Regards,
Daniel Zhang


0 Votes 0 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT edited

Hi Jackson1990-7147,
Based on your error message, the "mailItem" is not initialized before use.
In other word, the code does not enter the if (Pos3>0) conditional statement, so mailItem is not initialized
So you need to determine whether the mailItem has a value in the else statement.

 else 
 {
 if (mailItem !=null &&mailItem.EntryID != null)
  {  mailItem.HTMLBody = mailItem.HTMLBody + Line0;
     //mailItem.Send();
   }
  }

Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

Maybe you should extract the code from else block and put it after ‘mailItem.Subject = "Coarri Codeco Issue".


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 answered DanielZhang-MSFT commented

Hi Daniel,
As code below is under another loop, I am afraid of that the code below would have other issue as it is supposed to initialize mailItem once only.

                      if (Pos0 > 0 && Pos1 > 0)
                      {
                          if (Pos1 - Pos0 - 17 > 1)
                          {
                              Str0 = Line0.Substring(Pos0 + 18, Pos1 - Pos0 - 19);Pos2 = Str0.Trim().Length;
                              for (int j = 0; j <l7.Count(); j++)
                              {
                                  if (l7[j].Trim().Length>=Str0.Trim().Length)
                                  {
                                      if (l7[j].Substring(0,Pos2)==Str0)
                                      {
                                          Pos3 = l7[j].IndexOf(",,");Pos4 = l7[j].Trim().Length;
                                          if (Pos3>0)
                                          {
                                              mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                                              Email = l7[j].Substring(Pos3 + 2, Pos4 - 3 - Pos3);
                                              if (mailItem.EntryID != null)
                                              {
                                                  mailItem.Subject = "Coarri Codeco Issue ";
                                                  //mailItem.Send();
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      }
                      else
                      {
                          mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                          if (mailItem.EntryID != null)
                          {
                              mailItem.HTMLBody = mailItem.HTMLBody + Line0;
                              //mailItem.Send();
                          }
                      }


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Jackson1990-714,
You can check whether the "mailItem" is null before it is initialized.
Best Regards,
Daniel Zhang

0 Votes 0 ·
Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 answered DanielZhang-MSFT edited

Hi,

You can check whether the "mailItem" is null before it is initialized.

How?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Jackson1990-7147,
Please try the following code:

  else 
      {
         if(mailItem ==null)
           {
                mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem); 
            } 
           else  if (mailItem.EntryID != null)
                      {  mailItem.HTMLBody = mailItem.HTMLBody + Line0;
                          //mailItem.Send();
                      }
      }

Best Regards,
Daniel Zhang






0 Votes 0 ·
Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 answered DanielZhang-MSFT commented

Hi Daniel,
Sorry to error

Use of unassigned local variable 'mailItem'

due to 1st line below.

                          if (mailItem == null)
                              mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                          if (mailItem.EntryID != null)
                          {
                              mailItem.HTMLBody = mailItem.HTMLBody + Line0;
                              //mailItem.Send();
                          }


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Jackson1990-7147,
Outside your entire loop, please add the following code:

 object mailItem=null;

Best Regards,
Daniel Zhang

1 Vote 1 ·