2

Scenario: We are using a createwizard control to create users for our website. When the user has an email we simply send the user the email with their username and password saying their account has been created. Using MailMessage and SMTPClient.

Problem: When the user doesn't have an email (ex. temp employee, student) I need to be able to generate the email but have it open via the creator's mail client so that they can enter in an email themselves/see the username and password. Similar to @mailto functionality.

Question: Is there a way to do this from the code behind? I have tried searching but everywhere is telling me to use MailMessage and SMTPClient which I don't need.

Thanks in advance.

Gage
  • 7,365
  • 9
  • 47
  • 77
  • 4
    **NEVER** send a password in an email message, except for generated passwords that must be changed on next login. – SLaks Aug 25 '11 at 14:00
  • @SLaks: I agree, unless it's encrypted, the password I mean :) – Răzvan Flavius Panda Aug 25 '11 at 14:02
  • @SLaks, knew that was going to come up lol. It's an autogenerated password so the user needs someway of knowing what it is. Hence the email. – Gage Aug 25 '11 at 14:02
  • 2
    Why don't you just display it in the page? – SLaks Aug 25 '11 at 14:02
  • @Răzvan: (Almost-) Never encrypt a password either. – SLaks Aug 25 '11 at 14:03
  • Not sure what you ask. If someone does not have email, how you want to send him email? He doesn't have any.. (Endless loop here) – Shadow The GPT Wizard Aug 25 '11 at 14:04
  • 1
    @Shadow Wizard, If the user doesn't have an email then the creator at least sees the email with the username and password and can phone the user and tell them or email it to their dept manager. – Gage Aug 25 '11 at 14:05
  • 1
    So just send the email to the creator.. still can't see how it's related to "@mailto" stuff. – Shadow The GPT Wizard Aug 25 '11 at 14:07
  • Răzvan Panda: And how should a user enter an encrypted password ? Decrypt it in the head, with a little mental arithmetic and not key ? Oh right, you could send the key and the encryption method name along... Unless it would be asymmetric encryption, there you would need to send both keys :) – Stefan Steiger Aug 25 '11 at 14:08
  • @Shadow Wizard, If it was up to me I would but it's not. I need to be able to open an email with the information so the creator can do what they want with it. – Gage Aug 25 '11 at 14:15
  • Sorry Gage, my answer is not the real solution see the edit. – Shadow The GPT Wizard Aug 25 '11 at 15:30

3 Answers3

5

You can create a mailto-link and click it with JavaScript.

See here: How do I programmatically click a link with javascript?

Or you can create a form with textboxes "To, Subject and Body", and a button send, where you wait 'till the user filled it out (the to field), then you send the mail with smtpclient.

On a sidenote: why not just prompt for an email address and then send it there ?

Community
  • 1
  • 1
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
  • That's what I was thinking but I was hoping there was an easier way. – Gage Aug 25 '11 at 14:15
  • @Gage A: I don't see how this is particularly difficult. Shadow Wizard has already given you the code for the mailto link, and the code for clicking the link is there as well. all you need to do is add a script block with content clickLink(document.getElementById('yourlinkid')); at the end of the page. – Stefan Steiger Sep 03 '11 at 19:04
2

Well.. you can do a response.write or use a substitution control somewhere that links to a function that generates the propertly a href=mailto:.......etc. html

if you want it done automatically, you can do a javascript target redirect

dan
  • 393
  • 1
  • 14
1

I think I finally understand what you're after. This code should work:

string sMailTo = "putmailhere@domain.com";
string subject = "New member data";
string body = "New member has been added.\nName: John Doe\nPassword: pass";
Process.Start(string.Format("mailto:{0}?subject={1}&body={2}", sMailTo, Uri.EscapeUriString(subject), Uri.EscapeUriString(body)));

Note that you can use only plain text, from what I've seen you can't force HTML format.

Edit: Since it's ASP.NET application this approach is actually useless, as the mail client will open on the server machine not the visitor machine.

Leaving this for those who use WinForms or any non web environment.

For ASP.NET only way is client side script invoking <a href="mailto:..."> anchor.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208