Activation code is not displaying in url after click on the link - asp.net

I am sending a link using email to create password but while sending link in an email i have attach my Activation Code also to display with link but it is not displaying when i click on link though in debugging i'm getting the link with Activation Code. Below is my code to add link in Body section
body+=#"<br /><a href='http://localhost:49234/Index.aspx?ActivationCode='"+objUserDetailsBE.ActivationCode+"'>Create a login to account</a>";
I am getting only till http://localhost:49234/Index.aspx?ActivationCode= in browser after click on the link Please let me know where i am doing wrong.
Adding code as per in comments:
string emailAddress = txtEmailAddress.Text;
string subject = "Login Credentials For Nth Star";
string body = string.Format("Hello,");
body+=#"<br /><a href='http://localhost:49234/Index.aspx?ActivationCode='"+objUserDetailsBE.ActivationCode+"'>Create a login to account</a>";
Email.SendMail(objemail, emailAddress, subject, body, "");
and below is my 'SendMail' method
public static bool SendMail(EmailConfigurationBE objEmailConfig, string toEmailAddresses, string subject, string body, string mailAttachments)
{
char[] splitter = { ';' };
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(objEmailConfig.Email);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.Priority = MailPriority.High;
string[] multi = toEmailAddresses.Split(';');
string[] multipath = mailAttachments.Split(';');
foreach (string MultiemailId in multi)
{
mailMessage.To.Add(new MailAddress(MultiemailId));
}
if (mailMessage.To.Count > 0)
{
//Adding Multiple Attachments
if (mailAttachments != "")
{
foreach (string Multipath1 in multipath)
{
Attachment attachFile = new Attachment(Multipath1);
mailMessage.Attachments.Add(attachFile);
}
}
SmtpClient smtpClient = new SmtpClient();
try
{
smtpClient.Host = objEmailConfig.SMTPServer;
smtpClient.EnableSsl = EnableSsl;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = objEmailConfig.Email;
NetworkCred.Password =objEmailConfig.Password;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = NetworkCred;
smtpClient.Port =Convert.ToInt32(objEmailConfig.PortNumber);
smtpClient.Send(mailMessage);
return true;
}
catch
{
mailMessage = null;
smtpClient = null;
return false;
}
}
else
{
return false;
}
}

It looks like a simple quoting problem. Look
<a href='http://localhost:49234/Index.aspx?ActivationCode='"+objUserDetailsBE.ActivationCode+"'
In here you have a single quote before http, another one after ActivationCode= and third one at the end. Looks like one is redundant, and that breaks your markup.
Correct version:
body+=#"<br /><a href='http://localhost:49234/Index.aspx?ActivationCode="+objUserDetailsBE.ActivationCode+"'>Create a login to account</a>";
The only change I did was to remove the single quote after ActivationCode=.
Also make sure the active code does not contain symbols like quotes or <>, that can also break the markup.

Related

Sending Mail using dynamic values with asp.net core 2.0

I'm sending email via asp.net Core 2.0 like this tutorial, so I have something like this into my controller
SmtpClient client = new SmtpClient("mysmtpserver");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password");
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("whoever#me.com");
mailMessage.To.Add("receiver#me.com");
mailMessage.Body = "body";
mailMessage.Subject = "subject";
client.Send(mailMessage);
and it works, but I want to do it less generic. Like sending code to class and call it from controller. For example in class I want to use variables instead static content like
mailMessage.Body = "body";
Instead this I want to use something like:
var body;
mailMessage.Body = body;
So into controller have ability to change that content. How can I achieve that? Regards
In your controller, add a private function called SendEmail something like
private bool SendEmail(string mail_to, string mail_subject, mail_body)
{
bool result = false;
try
{
SmtpClient client = new SmtpClient("mysmtpserver");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("username", "password");
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("whoever#me.com");
mailMessage.To.Add(mail_to);
mailMessage.Body = mail_body;
mailMessage.Subject = mail_subject;
client.Send(mailMessage);
result = true;
}
catch(Exception ex){ result = false; }
return result;
}
Use it in your controller
string mailBody = "Anything can be in the body\n. Mail contents.";
string subject = "Mail Subject";
string mailTo = "someone#someone.com"
SendEmail(mailTo, subject, mailBody)

How to send emails from my website domain in asp.net

I want to send mail from my website domain(www.sample.com)
I have written below code. It is not returning any error. But it is not sending mail once I uploaded the pages in the server. Once click on email send, it is not getting any error, but not receiving the mail.
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("mail.sample.co.uk", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("smtpuser#sample.co.uk", "pass#123");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//Setting From , To and CC
mail.From = new System.Net.Mail.MailAddress("smtpuser#sample.co.uk", "MyWeb Site");
mail.To.Add(new System.Net.Mail.MailAddress("myemail#gmail.com"));
mail.CC.Add(new System.Net.Mail.MailAddress("myemail1#gmail.com"));
smtpClient.Send(mail);
I am getting the "mailsend" response text in the page once calling the sending method.
Please help
Anjana
The following code works perfect for me. Make sure you have added a mail account on the smtp server and activated it.
private static string EMAIL_SERVER = "mailservername.mydomain.com";
private static string EMAIL_NAME = "myMailAccount#mydomain.com";
private static string EMAIL_PASSWORD = "complex_password123";
private static string EMAIL_SEND_TO = "sample#mail.com";
protected string SendMail()
{
try
{
System.Net.Mail.MailMessage objMM = new System.Net.Mail.MailMessage();
objMM.From = new MailAddress("fromMe#mail.com", "john doe");
objMM.To.Add(new MailAddress(EMAIL_SEND_TO)); //Note: this To a collection
objMM.Subject = "Subject1";
objMM.Body = "Hello world this is my text";
objMM.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(EMAIL_SERVER);
smtp.Credentials = new NetworkCredential(EMAIL_NAME, EMAIL_PASSWORD);
smtp.Send(objMM);
}
catch (Exception e)
{
return "Message can not be send couse of error: " + e.ToString();
}
return "Message is send.";
}

Sending Emails with BCC list not working

I am trying to send emails to a single 'To' recipient, and a list of 'Bcc' recipients.
The list of Bcc recipients is a list of string, and they are successfully being added to the mailMessage's Bcc collection, but not actually being sent. If I add the same list to the message's 'Cc' collection it works fine. Just not the Bcc collection.
The code I'm using is this:
public void SendEmailMessage(String FromAddress, String ToAddress, String Subject, String Body, List<String> CCAddress, List<String> BccAddress, String Filepath)
{
using (SmtpClient mailClient = new SmtpClient())
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(FromAddress);
mailMessage.To.Add(new MailAddress(ToAddress));
foreach (String _email in CCAddress)
{
mailMessage.CC.Add(new MailAddress(_email));
}
foreach (String _email in BccAddress)
{
mailMessage.Bcc.Add(new MailAddress(_email));
}
mailMessage.Priority = MailPriority.Normal;
mailMessage.Subject = Subject;
if (Filepath != string.Empty)
{
Attachment _attachment = new Attachment(Filepath, MediaTypeNames.Application.Octet);
mailMessage.Attachments.Add(_attachment);
}
AlternateView plainTextView = AlternateView.CreateAlternateViewFromString(GetTextonly(Body), null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");
mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);
SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Send(mailMessage);
}
}
any ideas?
one thing i didn't mention is that the mail is put in a pickup directory rather than sent direct.
I found a blog which explains that bcc addresses aren't sent if using a pickup directory, and you can put them in the retry directory instead. This solved my problem with an easy fix:
Unable to send Bcc using System.Net.Mail when specifying a Pickup Directory(Exchange 2007/Exchange 2010) in code
As a workaround, you could send your email explicitly to the BCC address.
After you've successfully sent your email:
mailClient.Send(mailMessage);
Clear the To address collection, then add your BCC address as a To address, and resend.
mailMessage.To.Clear(); // clear the existing To & Cc fields
mailMessage.Cc.Clear();
mailMessage.To.Add(new MailAddress("bcc#address.com","CopyAddress"));
mailClient.Send(mailMessage);
I created a test application and ran SmptForDev to capture any emails going out from my local IIS. I used the code below and it works fine. All I've really done to your code is tidy it up and it works fine. I also decompiled System.Net.Mail.SmtpClient to see what it does under the hood, the To address and Bcc addresses are all put into one collection, if one address is sending it's good to assume they all are.
public void SendEmailMessage(string fromAddress, string toAddress, string subject, string body, IEnumerable<string> ccAddress, IEnumerable<string> bccAddress, string filepath)
{
using (var mailClient = new SmtpClient())
{
var mailMessage = new MailMessage(fromAddress, toAddress);
foreach (var email in ccAddress)
{
mailMessage.CC.Add(new MailAddress(email));
}
foreach (var email in bccAddress)
{
mailMessage.Bcc.Add(new MailAddress(email,"Matty Boy"));
}
mailMessage.Priority = MailPriority.Normal;
mailMessage.Subject = subject;
if (!string.IsNullOrEmpty(filepath))
{
var attachment = new Attachment(filepath, MediaTypeNames.Application.Octet);
mailMessage.Attachments.Add(attachment);
}
var plainTextView = AlternateView.CreateAlternateViewFromString(GetTextonly(body), null, "text/plain");
var htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);
mailClient.Send(mailMessage);
}
}

How to make a "Reset Password"?

How can I make this code send a new generated password or the user its old password?
I figured out how to let the code send a email! The sender is not the problem but the receiver is. Where EmailAdresNaar must be replaced with an email that someone puts into a text box or something.
public void SendEmail()
{
string emailAdresVan = "invoerenSVP";
string password = "invoerenSVP";
MailMessage msg = new MailMessage();
msg.From = new MailAddress(emailAdresVan);
msg.To.Add(new MailAddress(EmailAdresNaar));
msg.Subject = Onderwerp;
msg.Body = Bericht;
msg.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential loginInfo = new NetworkCredential(emailAdresVan, password);
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
}
}
Put data you need like this:
msg.From = new MailAddress("support#yoursite.com");
msg.To.Add(new MailAddress(UserEmailTextBox.Text));
msg.Subject = "New Password";
msg.Body = GenerateNewPassword();
and example of method GenerateNewPassword(), but you should make it much more complex to return randomly generated new password (you need to google for different variations of implementation):
public string GenerateNewPassword()
{
string new_pass = "";
// generate new password
return new_pass;
}
Ideally, you should make another page like PasswordsReset.aspx, and send to user link to this page with some kind of GUID, where user can create new password by himself.

Attachment Excel File from Stream in ASP.NET?

I have an excel document.I want to mail it as attachment from stream.
It sended mail with attachment but i cant open excel file correctly
this is my code:
public static string EPostaGonder(...,Stream AttachmentStream,string AttachmentFileName)
{
.
.
.
SmtpClient mailClient = new SmtpClient(Host, Port);
mailClient.EnableSsl = true;
NetworkCredential cred = new NetworkCredential(KullaniciAdi, Sifre);
mailClient.Credentials = cred;
MailMessage ePosta = new MailMessage();
ePosta.IsBodyHtml = true;
ePosta.From = new MailAddress(Kimden, Isim);
foreach (string Kime_ in Kime.Split(';'))
{
if (Kime_.Trim() != "")
ePosta.To.Add(Kime_.Trim());
ePosta.Subject = Konu;
ePosta.Body = Mesaj.Replace("\n","<br/>");
if (Cc != "")
ePosta.CC.Add(Cc);
if (AttachmentStream != null)
{
AttachmentStream .Seek(0, SeekOrigin.Begin);
ePosta.Attachments.Add(
new Attachment(AttachmentStream, AttachmentFileName + ".xlsx"));
}
try
{
//mailClient.SendAsync(ePosta, (object)ePosta);
mailClient.Send(ePosta);
return "Done";
}
catch (SmtpException SmtpException_)
{
return SmtpException_.Message;
}
}
Use following code to add Attachment in mail. Simply pass the file path to Attachment constructor.
Attachment attachment = new Attachment(file);
ePosta.Attachments.Add(attachment);
Add an attachment from stream:
ePosta.Attachments.Add( new Attachment( AttachmentStream, filename, "application/msexcel" ));
Try something like
var attach = new MailAttachment(Server.MapPath(strFileName));
ePosta.Attachments.Add(attach);
Have a look at this article about ASP.NET email with multiple attachments

Resources