tags:

views:

87

answers:

3

I have a webpage which I would like users to be able to send to a friend at the click of a button. I am currently using Chilkat's MailMan but I keep getting intermittent problems with it. It seems occassionaly on the first attempt to mail it throws a null pointer exception. Then if try the exact same page again it sends no problem.

  • Are there any other components out there that will do what I am trying to do?
  • Would it be easier to right my own light weight component to do it?
  • Has anyone had the above problem that can be solved easily and then I don't have to worry about the above?

EDIT: Maybe I should clear something up. I know how to send emails. That is not the problem. The Chilkat component I was using could take in a webpage and put it into an email and send it. The person that receives it then has an email with all the CSS included and the pictures and everything in the email.

A: 

1) .NET comes with a reasonably adequate class for sending mail, in System.Net.Mail.

2) If it happens only rarely and does not repeat, just put it in a try block and retry two more times before considering it a failure. While it may sound crude, it's a very effective solution.

Steven Sudit
I thought of that but I am a bit anal and I want to fix the problem. But I have done that in the interim. Once again though, looking for a way to do the "send to a friend" thing.
uriDium
What's the key issue here that's stopping you from using System.Net.Mail? Is it that you want to send an HTML page, complete with attachments containing the CSS, images and so on?
Steven Sudit
+1  A: 

Could you use the WebClient class to get the webpage that the user is requesting? You'd want to change any relative links to absolute links (e.g. from "/images/logo.gif" to "http://myapp.com/images/logo.gif"), then take the output and use that as the body of the MailMessage object

i.e.

public void MailToAFriend(string friendMailAddress, Uri uriToEmail) {
  MailMessage message = new MailMessage();
  message.From = "[email protected]";
  message.To = friendEmailAddress;
  message.Subject = "Check out this awesome page!";
  message.Body = GetPageContents(uriToEmail);

  SmtpClient mailClient = new SmtpClient();
  mailClient.Send(message);
}

private string GetPageContents(Uri uri) {
  var webClient = new WebClient();
  string dirtyHtml = webClient.DownloadString(uri);
  string cleanedHtml = MakeReadyForEmailing(dirtyHtml); 
  return cleanedHtml;
}

private string MakeReadyForEmailing(string html) {
  // some implementation to replace any significant relative link 
  // with absolute links, strip javascript, etc
}

There's lots of resources on Google to get you started on the regex to do the replacement.

Jeremy Frey
+1  A: 

This is actually not a trivial exercise.

What you want to do, is download the HTML (which is the easy part). You then have to parse it, and extract all of the css references, and image references, and either:

1)Embed them into the email

or

2)Convert all links to absolute links.

When you look at all the bad HTML out there, you find out this isn't trival. The reason why I know this, is I wrote this functionality into aspNetEmail (www.aspNetEmail.com), and had to account for all sorts of bad HTML.

Cheers! Dave

dave wanta
I realize that this is going to be quite a bugger. I am just at wit's end to try and figure out what is causing my null pointer exception with Chilkat. I will take a look at your product.
uriDium
If you have any questions, or feature suggestions, let me know. We can take this offlist. Just contact me through my website.Cheers!Dave
dave wanta