I have an ASP.Net MVC application. I need to send html content from HTML-Editor. but I have problem with it.
This is my Html-Editor:
This is my code:
public void MailMessageHtml(string body, string subject, string from, IEnumerable<string> to)
{
var message = new MailMessage();
message.To.Add(new MailAddress("myemail#example.com"));
message.From = new MailAddress(_Settings.MailServer.UserName);
message.Subject = subject;
message.IsBodyHtml = true;
var htmlBody = AlternateView.CreateAlternateViewFromString(
body, Encoding.UTF8, "text/html");
message.AlternateViews.Add(
AlternateView.CreateAlternateViewFromString(string.Empty, new ContentType("text/plain")));
message.AlternateViews.Add(htmlBody);
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = _Settings.MailServer.UserName,
Password = _Settings.MailServer.Password
};
smtp.Credentials = credential;
smtp.Host = _Settings.MailServer.IPAddress;
smtp.Port = _Settings.MailServer.Port;
smtp.EnableSsl = _Settings.MailServer.EnableSSL;
smtp.Send(message);
}
}
but the html content in Email's body has shown like this:
What should I do?
You need to Decode the content before passing the content to the body.
body = HttpUtility.HtmlDecode(body);
Just this.
Related
I am working with asp .net. I want to send an authentication code to user's email when he sign up. I'm using this code to send email. I'm adding a html file to send. How can I add the authentication code to this HTML file?
This is the code which I used to send the html file via Email. So how can I add a string(authenticationCode) to HTML file to send it to the user for authentication.
var fromAddress = new MailAddress("hamza230#gmail.com", "From Hamza");
var toAddress = new MailAddress("usama90#gmail.com", "To Usama");
const string fromPassword = "****";
const string subject = "email";
const string body = "hello world";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
MailMessage message = new MailMessage(fromAddress, toAddress);
message.Subject = subject;
message.Body = body;
message.BodyEncoding = Encoding.UTF8;
String plainBody = "Title";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, "text/plain");
message.AlternateViews.Add(plainView);
MailDefinition msg = new MailDefinition();
msg.BodyFileName = "~/newsletter.html";
msg.IsBodyHtml = true;
msg.From = "usamaazam10#gmail.com";
msg.Subject = "Subject";
ListDictionary replacements = new ListDictionary();
MailMessage msgHtml = msg.CreateMailMessage("usamaazam10#gmail.com", replacements, new LiteralControl());
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, Encoding.UTF8, "text/html");
LinkedResource logo = new LinkedResource(System.Web.HttpContext.Current.Server.MapPath("~/images/logo.jpg"), MediaTypeNames.Image.Jpeg);
logo.ContentId = "logo";
htmlView.LinkedResources.Add(logo);
LinkedResource cover = new LinkedResource(System.Web.HttpContext.Current.Server.MapPath("~/images/cover.jpg"), MediaTypeNames.Image.Jpeg);
cover.ContentId = "cover";
htmlView.LinkedResources.Add(cover);
message.AlternateViews.Add(htmlView);
smtp.Send(message);
You can do this for small html pages
string smalHhtml = "<body>"
+ "your some designing"
+ "your Code : " + code
+ "</body>";
Note: This is for small body html and sending verification code via email is usually small html.
You can add the authentication code to the message body, for example:
...
message.Body = body + authenicationCode;
...
Or, if you don't create body as a const then you can create it at the top of your code, for example;
...
const string subject = "email";
string body = "Your authentication code is: " + authenicationCode;
...
am am doing a website in mvc4 using c#. currently i test this in localhost. I want to send passwords to registering persons email. i use the following code.
//model
public void SendPasswordToMail(string newpwd,string email)
{
string mailcontent ="Your password is "+ newpwd;
string toemail = email.Trim();
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("paru.mr#gmail.com");
mail.To.Add(toemail);
mail.Subject = "Your New Password";
mail.Body = mailcontent;
//Attachment attachment = new Attachment(filename);
//mail.Attachments.Add(attachment);
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password"); //is that NetworkCredential
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception e)
{
throw e;
}
}
//controller
string newpwd;
[HttpPost]
public ActionResult SendPassword(int Id,string email)
{
bool IsMember=CheckMember(Id);
if (IsMember == true)
{
newpwd = new Member().RandomPaswordGen();
}
.......//here i want to call that model
return View();
}
Is the model is right or not. and what should be the return type of this model(now it is void and i dont know how this call in controller). and how to get default NetworkCredential. Please help me
Network Credential is the user name and password of the email sender (as in case of company a defualt mail address like noreply#abccompany.com).
Hence use you gmail user name and password in that case.
And you could use following code to send email.
var client = new SmtpClient()
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
};
var from = new MailAddress(Email);
var to = new MailAddress(Email);
var message = new MailMessage(from, to) { Body = Message, IsBodyHtml = IsBodyHtml };
message.Body += Environment.NewLine + Footer;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = Subject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
var nc = new NetworkCredential("me#gmail.com", "password");
client.Credentials = nc;
//send email
client.Send(message);
I'm using the below code to send plan text but it's not working in html template..
static void Main(string[] args)
{
String username = "test"; // Replace with your SMTP username.
String password = "test"; // Replace with your SMTP password.
String host = "email-smtp.us-east-1.amazonaws.com";
int port = 25;
using (var client = new System.Net.Mail.SmtpClient(host, port))
{
client.Credentials = new System.Net.NetworkCredential(username, password);
client.EnableSsl = true;
client.Send
(
"sales#imagedb.com", // Replace with the sender address.
"rohit#imagedb.com", // Replace with the recipient address.
"Testing Amazon SES through SMTP",
"This email was delivered through Amazon SES via the SMTP end point."
);
}
You need to use AlternateViews with the .NET SmtpClient to send HTML messages. The mail client will render the appropriate views that it can support.
Here is a code snippet that demonstrates how to create a message with a text view and an HTML view, and then send the message via Amazon SES.
string host = "email-smtp.us-east-1.amazonaws.com";
int port = 25;
var credentials = new NetworkCredential("ses-smtp-username", "ses-smtp-password");
var sender = new MailAddress("sender#example.com", "Message Sender");
var recipientTo = new MailAddress("recipient+one#example.com", "Recipient One");
var subject = "HTML and TXT views";
var htmlView = AlternateView.CreateAlternateViewFromString("<p>This message is <code>formatted</code> with <strong>HTML</strong>.</p>", Encoding.UTF8, MediaTypeNames.Text.Html);
var txtView = AlternateView.CreateAlternateViewFromString("This is a plain text message.", Encoding.UTF8, MediaTypeNames.Text.Plain);
var message = new MailMessage();
message.Subject = subject;
message.From = sender;
message.To.Add(recipientTo);
message.AlternateViews.Add(txtView);
message.AlternateViews.Add(htmlView);
using (var client = new SmtpClient(host, port))
{
client.Credentials = credentials;
client.EnableSsl = true;
client.Send(message);
}
Using the simple SMTP C# code below to send an email, how can i send an email template?
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(toEmailAddress);
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress(from);
message.Body = "http://www.yoursite.com/email.htm";
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("server");
smtp.Send(message);
Currently, as expected the received email just contains the URL for the template. how can i get it to send the template?
System.Net.WebClient client = new System.Net.WebClient();
string html = client.DownloadString("http://www.yoursite.com/email.htm");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(toEmailAddress);
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress(from);
message.Body = html;
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("server");
smtp.Send(message);
Your question is actually about reading a string from url and one of the possible answers is:
var url = "http://www.yoursite.com/email.htm";
var body = "";
using(var client = new WebClient()) {
body = client.DownloadString(url);
}
If the file is local, instead of using a download, you can simply read it in using System.IO, such as
string html;
System.IO.StreamReader fstream;
fstream = File.OpenText("yourpathgoeshere.html");
html = fstream.ReadToEnd();
fstream.Close();
after this, just assign the rest of the properties as suggested in the other posts. If the html file you're after is stored locally, this is probably better, or if it will be accessed frequently, it may be a better idea to store it locally and use this method.
note, you will need to import System.IO for this to work correctly.
I below code is to send email by reading the template html, and it works fine. But now my question is how to pass Salutation customerName from my .net code to the template at run time.
StringBuilder strBlr = new StringBuilder();
string strHTML = string.Empty;
string strTempalteHtmlpath = string.Empty;
//create the mail message
MailMessage mail;
string strFrom = ConfigurationSettings.AppSettings["fromAddressForBT"];
string strSubject = "Thanks for choosing Email contact preference";
mail = new MailMessage(strFrom, customerDetails.EmailId);
mail.Subject = strSubject;
//Read Html Template File Path
strTempalteHtmlpath = Convert.ToString(ConfigurationSettings.AppSettings["TemplatePath"]);
strHTML = File.ReadAllText(strTempalteHtmlpath);
strBlr = strBlr.Append(strHTML);
mail.Body = strBlr.ToString();
mail.IsBodyHtml = true;
//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString(strBlr.ToString(), null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(strBlr.ToString(), null, "text/html");
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
//send the message
SmtpClient smtpMail = new SmtpClient(ConfigurationSettings.AppSettings["smtpClient"]);
smtpMail.Send(mail);
mail.Dispose();
Thanks.
This is code for sendemail button
StreamReader sr = new StreamReader(Server.MapPath("Sendpage.htm"));
string body = sr.ReadToEnd();
sr.Close();
body = body.Replace("#NameFamily#", txtNameFamily.Text);
body = body.Replace("#Email#", txtEmail.Text);
body = body.Replace("#Tellphone#", txtTellphone.Text);
body = body.Replace("#Text#", txtText.Text);
body = body.Replace("#Date#", DateTime.Now);
string Time = Convert.ToString(DateTime.Now.ToShortTimeString());
body = body.Replace("#Time#", Time);
SendMail("email that you want to send to it", body);
this is sendmail function code:
private void SendMail(string To, string Body)
{
SmtpClient Mailing = new SmtpClient("mail.domain.com");
MailMessage Message = new MailMessage();
Message.From = new MailAddress("mail#domain.com", "Your name or company name");
Message.Subject = "Subject";
Message.SubjectEncoding = Encoding.UTF8;
Message.IsBodyHtml = true;
Message.BodyEncoding = Encoding.UTF8;
Message.Body = Body;
Message.To.Add(new MailAddress(To));
Mailing.UseDefaultCredentials = false;
NetworkCredential MyCredential = new NetworkCredential("mail#domain.com", "password");
Mailing.Credentials = MyCredential; Mailing.Send(Message);
}