Share via


Sending email with URL to a page in the project

Question

Monday, March 10, 2014 10:48 AM

I have a 4.5.1 MVC 5 project.  I am trying to figure how to put a link in the email that will point the user to another view to change pw.  I need to be able to test this locally and when deployed to the cloud.  We are using the Google SMTP service for testing.  Sending email works, I just need to know what the format of sending the link to a view in the body is.

Thanks in advance,

Mark

All replies (11)

Monday, March 10, 2014 2:23 PM âś…Answered

MailMessage mm = new MailMessage(emmailFrom,emailTo);
mm.Subject = "Your Subject";
mm.IsBodyHtml = true;
mm.Body = "To change your password, click <a href=http://" + Request.Url.Host + "/Account/Login/>here</a>.";

SmtpClient smtp = new SmtpClient();
smtp.Send(mm);

Monday, March 10, 2014 11:03 AM

If you want to have a local url on localhost (http://localhost/someaction) and url with domain on remote server then you can use Request.Url.Host

"http://" + Request.Url.Host + "/someaction"


Monday, March 10, 2014 11:10 AM

Hi,

Or if you meant the mail format, you could just use HTML: see http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml(v=vs.110).aspx and http://www.w3schools.com/html/html_links.asp

As already pointed you can then use the current request to generate a link to the current site whatever it is...


Monday, March 10, 2014 11:57 AM

You need to use a token along with the url so that you know the user who is changing password. Check this link

http://www.dominikgorecki.com/2013/10/mvc4-password-rese/


Monday, March 10, 2014 11:57 AM

I want it to go to the PasswordReset view.  I am not sure what to put for /someaction?


Monday, March 10, 2014 12:39 PM

if your current url is http://localhost/PasswordReset

then you can do 

"http://" + Request.Url.Host + "/PasswordReset"


Monday, March 10, 2014 1:34 PM

I tried this and didnt resolve:

"To change your password, click '<a href=http://" + Request.Url.Host + "/Account/Login/'> here</a>.";

email had:

To change your password, click '<a href=http://localhost/Account/Login/'> here</a>.


Monday, March 10, 2014 2:04 PM

Do you mean it shows the HTML markup. As previously told do you use MailMessage.IsBodyHTML ?

It will happen also if using a non HTML client. You can define also an alternate view for this kind of client...


Monday, March 10, 2014 2:13 PM

I didn't see anything about MailMessage.IsBody...

I am pretty sure the quotes could be my problem because it is NOT resolving as my previous message states.  It displays the markup vs the link.


Monday, March 10, 2014 2:22 PM

See http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml(v=vs.110).aspx that I posted earlier. It is a property of the MailMessage object that allows to tell that the body is HTML rather than plain text :

yourMessage.IsBodyHTML=true;


Monday, March 10, 2014 2:49 PM

You hit it on the nose.  IsBodyHtml was false!  Thanks for your help