Object reference error

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
11/20/2009 11:30:03 AM
Gravatar
Total Posts 111
Matt Millican InternetMill

Object reference error

Hi all,

I'm not sure if this is exactly the right place to be posting this, but I guess it's worth a shot.

I am working on creating a custom module which needs to send email.  I am having issues with the following code, getting an "Object reference not set to an instance of an object" error, which I think means that something isn't instantiated.  However, I can't find in here what's not.  All the variables in the MailMessage() parameters are set right above the try statement.

If someone could look at this and see what I might be missing, that would be great.

Thanks,

--------------------------------



try

{

//MailMessage msg = new MailMessage(yourEmail, friendEmail, subject, html);

 

MailMessage msg = new MailMessage("millicanms18@gmail.com", "millicanms18@uww.edu", "test", "test");

msg.IsBodyHtml = true;

 

SmtpClient smtp = new SmtpClient();

smtp.Host = mailServer;

smtp.Port = 587;

 

smtp.Send(msg);

msg.Dispose();

}

catch (Exception ex)

{

lblMessage.Text = ex.InnerException.Message;

lblMessage.Visible = true;

}

11/20/2009 11:41:20 AM
Gravatar
Total Posts 550

Re: Object reference error

Hi,

Debug your code and trace which object isn't instantiated.

Hope Helps

Asad

11/20/2009 11:46:14 AM
Gravatar
Total Posts 18439

Re: Object reference error

It's not clear in your code if the mailServer variable has been initialized.

Also you have not set user or password credentials on the smtpClient. Also you have specified Html but did not pass in html message body.

Actually we have built in helper methods to make it easier to send email. Here is an example snippet from our contact form.

using mojoPortal.Net; (you will need to add a project reference)

try

{

Email.SendEmail(

SiteUtils.GetSmtpSettings(),

siteSettings.DefaultEmailFromAddress,

txtEmail.Text,

Settings["ContactFormEmailSetting"].ToString(),

string.Empty,

string.Empty,

txtSubject.Text,

message.ToString(),

true,

"Normal");

 Hope it helps,

Joe

 

}

catch (Exception ex)

{

log.Error(ex);

}

 

Hope it helps,

Joe

11/20/2009 12:12:47 PM
Gravatar
Total Posts 111
Matt Millican InternetMill

Re: Object reference error

Thanks Joe!

One question though.  What namespace is log.Error() in?

It doesn't seem to be sending and I want to see if it's throwing an error message.

Thanks,

11/20/2009 12:17:16 PM
Gravatar
Total Posts 18439

Re: Object reference error

In the example from the contact form

using log4net; //(must set a dll reference from _libs folder)

private static readonly ILog log = LogManager.GetLogger(typeof(ContactForm));

Hope it helps,

Joe

9/16/2010 5:59:07 AM
Gravatar
Total Posts 15
Growing old is inevitable, growing up is optional :)

Re: Object reference error

Hi Joe,

 

I have similar problem with sending mail using mojoPortal.Net
like in your example i have in button click on my module - usercontrol

try{
   Email.SendEmail(.....);
}
catch (Exception ex)
{

   //do something;
}

BUT although there is an error in sending mail (I found it by debugging - smtp server problems) I never get an exception because in Email Sending methods are lot of
try/catch with log.Error(...) parts.

I would like to write on usercontrol for example "There was a problem and mail was not sent", but there is no error in user control event.
All errors were handled by log.Error()

Can I somehow check "log" after Email.SendEmail() to see if there were errors so I can write on my module "Success/Fail" ? How, and is there an example in some mojo modules?

Thanx
Goran

9/17/2010 9:05:35 AM
Gravatar
Total Posts 18439

Re: Object reference error

Even if there is no error there can be no guarantee that an email is delivered and it is not a good idea to bother end users with concern about whether the smtp is configured correctly. That may be a concern for a site owner or developer but the site owner should check the logs and once smtp is configured correctly you will rarely ever have a problem.

If you think those methods should throw exceptions that must be handled by UI code then you could write your own email helper class to do that for your custom features.

Best,

Joe

9/20/2010 1:14:51 AM
Gravatar
Total Posts 15
Growing old is inevitable, growing up is optional :)

Re: Object reference error

thanx, writing my own email helper class is also an option :)
For now I'll stick to the optimistic version, when smtp settings start working it's ok :)

G.

You must sign in to post in the forums. This thread is closed to new posts.