Send a hyperlink within email - c#

I have a variable name that holds a hyperlink; I would like to send the hyperlink within an email.
I can send the email ok, but the hyperlink appears as text i.e.[http://www.google.com]Click here
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"], "FileTransfer");
mailMessage.Subject = "FileTransfer";
var body = new StringBuilder();
body.AppendFormat("<html><head></head><body> Hello World" + "<br />" + "<a href='{0}'>Click here</a></body></html>", link);
mailMessage.IsBodyHtml = true;
mailMessage.Body = body.ToString();
mailMessage.To.Add(new MailAddress(toEmail));
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mailMessage);
}

Try replacing this...
body.AppendFormat("<html><head></head><body> Hello World" + "<br />" + "<a href='{0}'>Click here</a></body></html>", link);
with this
body.Append("<html><head></head><body> Hello World" + "<br />" + "Click here</body></html>");

According to https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews(v=vs.110).aspx
It seems like you have to set the content type and create an alternate view. Or if you don't want to have a plaintext version, set the default view to text/html
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane#contoso.com",
recipients,
"This e-mail message has multiple views.",
"This is some plain text.");
// Construct the alternate body as HTML.
string body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">";
body += "</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text";
body += "</FONT></DIV></BODY></HTML>";
ContentType mimeType = new System.Net.Mime.ContentType("text/html");
// Add the alternate body to the message.
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
message.AlternateViews.Add(alternate);
Some email provider software won't be capable of displaying text/html. Whether this is a protective measure or just lack of support, I believe this would be the best solution since it compensates for both cases.

Related

email body coming in a single line

I am sending emails thorugh c#. The body of the mail is dynamic i.e. user fills it in before sending the mail. Issue is the body text is coming in a single line instead of multiple lines as they should.
using (MailMessage mm = new MailMessage(txtEmail.Text.Trim(), email.Trim()))
{
try
{
mm.CC.Add(txtcc.Text.Trim());
mm.Subject = txtSubject.Text.Trim();
mm.Body = txtBody.Text;
MemoryStream pdf = new MemoryStream(bytes);
mm.Attachments.Add(new Attachment(pdf, "Report.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = txtEmail.Text.Trim();
credentials.Password = txtPassword.Text.Trim();
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(mm);
}
catch (Exception ex)
{
Alert.show1("Could not send the e-mail - error: " + ex.Message, this);
return;
}
}
If you dont need the potential of HTML formatting, you could avoid the problem changing the email format to plain text. This could also avoid user entering text that could be parsed as html.
mm.IsBodyHtml = false;
Otherwise you should change the end line character to html breaking line:
mm.IsBodyHtml = true;
mm.Body = mm.Body.Replace(#"\n", "<br />");
In case of the html format option, It would be wise to follow Fildor's comment and add an alternateView in plain text for email clients that are not html capable.

Adding <img> HTML tag to C# String

I'm trying to email an image via Code behind but I can't get it to work. Basically trying to send the tag with a src so that it will display it in the email I sent:
Code behind
MailMessage mm = new MailMessage("mail-master#website.com", email);
mm.Subject = "Beta Signup";
string body = "<img src=\"\"http://popl.mpi-sws.org/2015/logos/google.png\"\" />";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("mail-master#website.com", "pass#123");
smtp.EnableSsl = true;
smtp.Send(mm);
However this doesn't send the image and I can't see the image showing up in the email I receive
string body = "<img src=\"\"http://popl.mpi-sws.org/2015/logos/google.png\"\" />";
You are including two quotes to enclose the src attribute value.
"<img src=\"\"http://popl.mpi-sws.org/2015/logos/google.png\"\" />";
This will result in invalid HTML:
<img src=""http://popl.mpi-sws.org/2015/logos/google.png"" />
Try replacing with:
string body = "<img src=\"http://popl.mpi-sws.org/2015/logos/google.png\" />";

Emails not being delivered to Gmail, Yahoo, Hotmail

I am attempting to send emails from a Web App written in C#/ASP.NET.
I am trying to send both HTML and text version of the email.
However, when I attempt to send to a Gmail, Yahoo, Hotmail, AOL, etc. email address it does not show up in their inbox.
When I send strictly text, without the HTML alternate view, it delivers just fine.
Has anyone here had any experience with this?
The email is delivered fine to our Hosted Exchange server "#mxxx.com"
Thanks!
EDIT: One more thing. When the emails don't get delivered, we don't receive any type of failed delivery notification, it essentially disappears.
Code:
// email the user
MailMessage message = new MailMessage("support#mxxx.com", user.EmailAddress);
message.Bcc.Add("xxx#gmail.com");
message.Bcc.Add("xxx#yahoo.com");
message.Bcc.Add("xxx#hotmail.com");
message.Subject = "Your \"mxxx.com\" password has been reset";
message.ReplyToList.Add(new MailAddress("support#mxxx.com"));
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay |
DeliveryNotificationOptions.OnSuccess;
message.Sender = new MailAddress("support#mxxx.com");
string htmlbody = "<html><body><p>Dear " + name + ":</p>" +
"<p>Your password at \"mxxx.com\" has been reset to: " + newPassword + "</p>" +
</body></html>";
var plainTextBody = "test plain text";
AlternateView plainTextView =
AlternateView.CreateAlternateViewFromString(
plainTextBody, message.BodyEncoding, MediaTypeNames.Text.Plain);
plainTextView.TransferEncoding = TransferEncoding.Base64;
message.AlternateViews.Add(plainTextView);
AlternateView htmlview =
AlternateView.CreateAlternateViewFromString(
htmlbody, message.BodyEncoding, MediaTypeNames.Text.Html);
htmlview.TransferEncoding = TransferEncoding.Base64;
message.AlternateViews.Add(htmlview);
SmtpClient mailMan = new SmtpClient();
mailMan.Send(message);
What if you avoid using AlternateView and specify that the body is HTML?
Consider testing the next snippet?:
MailMessage message = new MailMessage("support#mxxx.com", user.EmailAddress);
message.Bcc.Add("xxx#gmail.com");
message.Bcc.Add("xxx#yahoo.com");
message.Bcc.Add("xxx#hotmail.com");
message.Subject = "Your \"mxxx.com\" password has been reset";
message.ReplyToList.Add(new MailAddress("support#mxxx.com"));
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay |
DeliveryNotificationOptions.OnSuccess;
message.Sender = new MailAddress("support#mxxx.com");
string htmlbody = "<html><body><p>Dear " + name + ":</p>" +
"<p>Your password at \"mxxx.com\" has been reset to: " + newPassword + "</p>" +
</body></html>";
message.Body = htmlbody;
message.IsBodyHtml = true;
SmtpClient mailMan = new SmtpClient();
mailMan.Send(message);

Contact form doesn't work

I have a problem with a form, I can't send it. If I put wrong details (secure code, email, empty fields etc.), I get errors on screen, which is correct.
But when I enter all correct data, I can't send the form, I get this error:
There was an error submitting your form. Please check the following:
But the list is empty.
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("noreply#domain.com", "string1");
// decide who message goes to
string emailGoesTo = "person1#domain.com";
MailAddress toAddress = new MailAddress(emailGoesTo.ToString(), "string1");
message.From = fromAddress;
message.To.Add(toAddress);
message.To.Add("person2#domain.com");
message.Subject = "string2";
message.Body = "New Website Contact Request";
message.Body += "------------------------------------------\r\n";
message.Body += "Name: " + your_name.Text + "\r\n";
message.Body += "Email: " + your_email.Text + "\r\n";
message.Body += "Telephone: " + your_telephone.Text + "\r\n";
message.Body += "Company: " + your_company.Text + "\r\n";
message.Body += "Address: " + your_address.Text + "\r\n";
message.Body += "Postcode: " + your_zip.Text + "\r\n";
message.Body += "Enquiry: " + your_enquiry.Text + "\r\n";
// smtpClient.Host = "string3";
smtpClient.Host = "string4";
smtpClient.Credentials = new System.Net.NetworkCredential("string5", "string6");
smtpClient.Send(message);
Response.Redirect("thankyou.aspx");
}
catch (Exception ex)
{
statusLabel.Text = "Coudn't send the message!";
}
I'm noobie, so is there any1 string5 & string6 please?
Also what is wrong? How to make this form working?
never tried to use smtpclient.host but the below works. Just need to add in the ip/host name of the mail server and make sure it is set up to allow relay from the machine calling the code. and replace the strings with the text you want. I'd suggest starting with something simple rather than feeding from form fields to eliminate any outside issues. Once the mail is sending then start adding additional pieces in.
public static string mailServer = 'IP address or host name of mail server'
public static void Send()
{
string subject = "test subject";
string address = "test#somedomain.com";
string body = "some mail body";
MailMessage mm = new MailMessage();
mm.From = new MailAddress("no-reply#domain.net"); //on behalf of
mm.To.Add(new MailAddress(address));
mm.IsBodyHtml = true;
mm.Subject = subject;
mm.Body = body;
SmtpClient server = new SmtpClient(mailServer);
server.Send(mm);
}
}
string5 and string6 is email credentials on mail server (username and password). You should know it better us ;-)
You need to update your smtpClinet.Host with the name / domain information for your mail server. The Credentials need to be updated with the username and password of a user that can access your mail server. Sometimes this can be left out.

ASP.NET app to send an mail with a hyperlink

MailMessage message = new MailMessage();
message.From = new MailAddress("hkar#gmail.com");
message.Subject = "Subject";
message.Body = "Please login";
SmtpClient smtp = new SmtpClient();
message.To.Add("karaman#gmail.com");
smtp.Send(message);
I want to have a hyperlink in the body of sent mail where it says "login". How can I do that?
message.Body = "Please login";
Make sure you highlight when sending that the content is HTML though.
message.IsBodyHTML = true;
Set the message to message.IsBodyHTML = true
Login
message.Body = string.Format("Click <a href='{0}'>here</a> to login", loginUrl);
Format the message as HTML and make sure to set the IsBodyHtml property on the MailMessage to true:
message.IsBodyHtml = true;
System.Text.StringBuildersb = new System.Text.StringBuilder();
System.Web.Mail.MailMessage mail = new System.Mail.Web.MailMessage();
mail.To = "recipient#address";
mail.From = "sender";
mail.Subject = "Test";
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
sb.Append("<html><head><title>Test</title><body>"); //HTML content which you want to send
mail.Body = sb.ToString();
System.Web.Mail.SmtpMail.SmtpServer = "localhost"; //Your Smtp Server
System.Web.Mail.SmtpMail.Send(mail);
You just have to set the format of body to html then you can add html element within the bosy of mail message

Categories