C# mail is not displaying link in my email - c#

I am trying to send email with unsubsrible link in my aspx page. But when I check my email id It display only text. There was no link. Here is my code generate a email
string bodyContent = CKEditor1.Text;
string userLink = "http://www.abc.in/Message.aspx?action=rmsb&oldsubuser=";
string footerLink = "</br></br></br>You are receiving this mail because you have subscribed to our newsletter. If you do not wish to receive the mail, Click <a href='" + userLink + "" + ids[i].ToString() + "'>Here</a>";
bodyContent = bodyContent + footerLink;
EmailSend newsletter = new EmailSend();
newsletter.NewsLetterSend(ids[i].ToString(), bodyContent.Replace("'", "''"), txtSubject.Text.Replace("'", "''"));
//EmailSend.SendMailMessage("faredpt#gmail.com", ids[i].ToString(), "", "", txtSubject.Text, bodyContent);
bodyContent = bodyContent.Replace(footerLink, " ");
Here is the code for NewsLetterSend function
public void NewsLetterSend(string getemailAdd, string msgBody, string subject)
{
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("admin#abc.in", "admin#abc.in");
mail.To.Add(getemailAdd.Trim());
//set the content
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = msgBody;
mail.Priority = MailPriority.High;
//set the smtp settings
SmtpClient smtp = new SmtpClient("abc.in");
smtp.EnableSsl = false;
smtp.Credentials = new System.Net.NetworkCredential("admin#abc.in", "i#abc!23#");
//smtp.Port = 3535;
smtp.Port = 25;
//send email
smtp.Send(mail);
return;
}
Now this code is sending email successfully but unable to add link in my email. It show me simple text
Please tell me why this happening

You are replacing the quotation mark in href with two quotation marks!
Invalid statement: bodyContent.Replace("'", "''")
This will render your HTML invalid.

Could you try to replace </br></br></br> with <br/><br/><br/>? Probably because of invalid br tags your email is considered as not valid HTML and links become broken.

Related

Send multiple emails from listbox c#

hi im geting my email list from db in a listbox(email_c) and i need to send to all
this is the error that i get with my code
System.FormatException: 'The specified string is not in the form required for an e-mail address.'
login = new NetworkCredential(txtuser.Text, txtPassword.Text);
client1 = new SmtpClient(txtSmtp.Text);
client1.Port = Convert.ToInt32(txtPort.Text);
client1.Credentials = login;
string emails = email_c.GetItemText(email_c.Items);
msg = new MailMessage { From = new MailAddress(txtuser.Text + txtSmtp.Text.Replace("smtp.", "#"), "Sesizare", Encoding.UTF8) };
msg.To.Add(new MailAddress(emails));
if (!string.IsNullOrEmpty(txtCC.Text))
msg.To.Add(new MailAddress(txtCC.Text));
msg.Subject = txtSubject.Text;
msg.Body = "Abonamentul dumneavoastra a fost activat";
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client1.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userstate = "Ttimite...";
client1.SendAsync(msg, userstate);
The MailAddress does represent the address of an electronic mail sender or recipient. That means it is one single email.
Because of that, instead of writing all the email string into a single string variable you should iterate over the items in the listbox and add each of them individually to the MailMessage. Something like this:
foreach (var email in email_c.Items)
{
msg.To.Add(new MailAddress(email.ToString()));
}

Email sent still contains HTML tags.

I'm sending an email to people using a template. The problem is that all the tags are being shown.
<p>Hello,</p>
<p> Please, click the following link to change your password </p>
//...
<p> PLEASE DO NOT REPLY TO THIS MESSAGE</p>
The mail received is displaying exactly the original message with all the tags. Is there a way to make it look like
Here's my code:
string path = System.Web.HttpContext.Current.Server.MapPath("~/path/myTemplate.txt");
String body;
using (StreamReader sr = new StreamReader(path))
{
body = sr.ReadToEnd();
}
body = body.Replace("<%PasswordLink%>", pwdLink);
var mail = new MailMessage("from", "to");
mail.Subject = "Password Reset";
mail.Priority = MailPriority.High;
mail.Body = body;
var client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "123.45.67.89";
client.Send(mail);
You need to state that the mail message is HTML. If your using System.Web.Mail.MailMessage then use:
mail.BodyFormat = MailFormat.Html;
If you're using System.Net.Mail.MailMessage then use:
mail.IsBodyHtml = true;
Add mail.IsBodyHtml = true;
This will enable HTML formatting for the email.
I guess you have to define the mail body as HTML:
mail.IsBodyHtml = true;

Hyperlinks in mails not showing in gmail, but it does in Outlook

I've got a c# process that send emails to customers with a hyperlink in the mail. The mail is send from a SQL Server stored proc. My c# program just invokes the sp.
The hyperlink works fine in Outlook, but on online gmail it only shows as text. It is not clickable.
My mail text looks something like:
Hi.
This is the hyperlink:<br>
<a href=\"serveraddress\Documents\\123_128635312685687531322.gif\">
Click Here</a><br><br>
What should I do to fix it?
EDIT:
My code:
string email = "xx#gmail.com;
string password = "MyPassword";
var credentials = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email, senderName);
msg.To.Add(new MailAddress(toAddress));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.Send(msg);
EDIT 2:
Compile message being send:
string message = #"Hi. <br>
This is the intro line in the mail message.<br>";
using (DataTable dtLinks = data.ExecuteDataSet(#"SELECT *
FROM LessonFiles
WHERE Course = " + dr["Course"].ToString().DBValue() + #" AND
Lesson = " + dr["NextLesson"].ToString().DBValue()).Tables[0])
{
int i = 0;
foreach (DataRow drLink in dtLinks.Rows)
{
i += 1;
message += "<a href=\"" + drLink["Link"].ToString() + "\">" + drLink["Lesson"].ToString();
message += i == 1 ? "" : " file " + i;
message += "</a>" + "<br>";
}
}
message += "<br>Regards<br><br>";
try to add target="_blank", like this...
message += "<a href=\"" + drLink["Link"].ToString() + "\"target=\"_blank\">" + drLink["Lesson"].ToString();
Seems like something was funny with the hyperlink itself.
Using http://serveraddress/Documents/logoColourBG635315550177822533.jpg seems to work.
The original contained backslashes in the path. The fact that it showed the hyperlink in Outlook led me to believe the address was correct.
Thanks for all your help.
creating mail message object...
var smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(UserName, Password);
smtp.Timeout = 20000;
MailMessage Msg = new MailMessage();
Msg.IsBodyHtml = true;
MailAddress fromMail = new MailAddress(SenderID);
Msg.From = fromMail;
Msg.To.Add(new MailAddress(TosendID));
Msg.Subject = subject;
Msg.Body = body;
In body add ur code.....
Hope This Helps.........

Update panel visibility hidden after "mail sent successfully"

I have a page with two update panels - updatepanelform (visible), updatepanelthanks (hidden). I have used this method to send mail on click event of button on asp page.
protected void BtnRfqClick(object sender, EventArgs e)
{
// Gmail Address from where you send the mail
var fromAddress = TbEmailRfq.Text.ToString();
// any address where the email will be sending
var toAddress = "user#gmail.com";
//Password of your gmail address
var name = TbNameRfq.Text.ToString();
const string fromPassword = "password";
// Passing the values and make a email formate to display
string subject = "Welcome To world";
string body = "Name :" + TbNameRfq.Text.ToString() + Environment.NewLine +
"Email Id :" + TbEmailRfq.Text.ToString()
+ Environment.NewLine + TaComment.InnerText.ToString();
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("user email", "password");
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
Everything is working fine but I am not able to set visibility of updatepanelform to hidden and updatepanelthanks to visible after it sent email successfully.
Place the following after you send the email:
updatepanelform.Visible = false;
updatepanelthanks.Visible = true;

insert a link in to a email send using c#

I develop a program to send emails automatically using c#, and I want to insert a link to a web site to that email. How can I do it?
public bool genarateEmail(String from, String to, String cc, String displayName,
String password, String subjet, String body)
{
bool EmailIsSent = false;
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
try
{
m.From = new MailAddress(from, displayName);
m.To.Add(new MailAddress(to, displayName));
m.CC.Add(new MailAddress("xxx#gmail.com", "Display name CC"));
m.Subject = subjet;
m.IsBodyHtml = true;
m.Body = body;
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new
System.Net.NetworkCredential(from, password);
sc.EnableSsl = true;
sc.Send(m);
EmailIsSent = true;
}
catch (Exception ex)
{
EmailIsSent = false;
}
return EmailIsSent;
}
I want to send a link through this email. How should I add it to email?
You should be able to just add the mark-up for the link in your body variable:
body = "blah blah <a href='http://www.example.com'>blah</a>";
You shouldn't have to do anything special since you're specifying your body contains HTML (m.IsBodyHtml = true).
String body = "Your message : <a href='http://www.example.com'></a>"
m.Body = body;
Within the body. This will require that the body be constructed as HTML so the that a or can be used to render your link. You can use something like StringTemplate to generate the html including your link.
For some dynamic links, the email service providers will not show your link into email body if the link not prepend http (security issues)
like localhost:xxxx/myPage
m.body = "<a href='http://" + Request.Url.Authority + "/myPage'>click here</a>"

Categories

Resources