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
Related
I tried to send an email and set display name of the from Email address. But when i received email in my client for instance gmail. Instead of display name i see email address without domain means From Email "FromEmail#abc.com" then when i received it shows me i received email from "FromEmail".
Here is my code
MailMessage mail = new MailMessage();
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.HeadersEncoding = System.Text.Encoding.UTF8;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Priority = System.Net.Mail.MailPriority.High;
mail.IsBodyHtml = true;
mail.From = new MailAddress("FromEmail#abc.com","Automate");
mail.To.Add(new MailAddress("ToEmail#abc.com"));
mail.Subject = "This is test email";
mail.Body = "This is test mail.";
SmtpClient client = new SmtpClient("smtp.office365.com");
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("senderEmail#abc.com", "*****");
client.Send(mail);
I want to create a button and when user click on that, a window form will open and the From is default text, "To" is also load from code behind and user can edit that text, "Content" is default text and user can edit too.
So now I can send email with:
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
SmtpClient SmtpServer = new SmtpClient("gw1.scei.a-star.edu.sg");
mail.From = new MailAddress("mydefaultemail");
mail.To.Add("the To emails will be input here");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail.";
SmtpServer.Credentials = new System.Net.NetworkCredential("mydefaultemail", "");
SmtpServer.Send(mail);
Now I don't know how could I make it to be wildows form and catch the text in that form to input into this code?
You add a textbox to your Windows form.
Then, in your code, you get the text property value of that textbox and set your email variable accordingly.
mail.Body = myTextBox.Text;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(
"yourid#gmail.com", "yourgmailpassword");
MailMessage msg = new MailMessage();
msg.To.Add("Send To email Id");
msg.From = new MailAddress("yourid#gmail.com");
msg.Subject ="Subject";
msg.Body = "Message";
client.Send(msg);
I have this code:
string email = "myemail#gmail.com";
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(email, "mypassword");
MailMessage mailMessage = new MailMessage(email, toEmail);
mailMessage.Subject = title;
mailMessage.Body = message;
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(email, toEmail, title, message);
My message is:
message = "hello <b>world</b>."
When I recieve the email, it display the <b>...</b> not making it bold!
What's wrong with it?!
You create a mailMessage variable holding your HTML-formatted message, but then you ignored it and sent the body as plain text.
You need to send the mailMessage itself.
Your message is not a valid html.
Enclose your message with html and body tags
message = "<html><body>hello <b>world</b>.</body></html>"
Also thanks to #SLaks to point that out
In your sample, you should send mailMessage and not message:
client.Send(mailMessage);
I'm trying to send a password reset email, but I'm having trouble figuring out how to specify the sender's address.
Here's what I'm trying to do:
MailMessage mail = new MailMessage();
mail.From.Address = "support#mycompany.com";
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "Click here to reset your password.";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);
I'm sure it's possible, so how can I accomplish this in ASP.Net?
It turns out I was getting ahead of myself.
Removing Address from mail.From.Address allowed me to set the value, but needed the type MailAddress.
Here's the solution:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("support#mycompany.com");
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "Click here to reset your password.";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);
How do I send an HTML email? I use the code in this answer to send emails with SmtpClient, but they're always plain text, so the link in the example message below is not formatted as such.
<p>Welcome to SiteName. To activate your account, visit this URL:
http://SiteName.com/a?key=1234.
</p>
How do I enable HTML in the e-mail messages I send?
This is what I do:
MailMessage mail = new MailMessage(from, to, subject, message);
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient("localhost");
client.Send(mail);
Note that I set the mail message html to true: mail.IsBodyHtml = true;
I believe it was something like:
mailObject.IsBodyHtml = true;
IsBodyHtml = true is undoubtedly the most important part.
But if you want to provide an email with both a text/plain part and a text/html part composed as alternates, it is also possible using the AlternateView class:
MailMessage msg = new MailMessage();
AlternateView plainView = AlternateView
.CreateAlternateViewFromString("Some plaintext", Encoding.UTF8, "text/plain");
// We have something to show in real old mail clients.
msg.AlternateViews.Add(plainView);
string htmlText = "The <b>fancy</b> part.";
AlternateView htmlView =
AlternateView.CreateAlternateViewFromString(htmlText, Encoding.UTF8, "text/html");
msg.AlternateViews.Add(htmlView); // And a html attachment to make sure.
msg.Body = htmlText; // But the basis is the html body
msg.IsBodyHtml = true; // But the basis is the html body
Apply the correct encoding of the Mailbody.
mail.IsBodyHtml = true;
i have an idea , you can add a check box to your project for sending email as html as an option for user , and add this code to enable it :
MailMessage mail = new MailMessage(from, to, subject, message);
if(checkBox1.CheckState == CheckState.Checked )
{
mail.IsBodyHtml = true;
}
SmtpClient client = new SmtpClient("localhost");
client.Send(mail);
If you are using Mailkit,We can use TextBody,HtmlBody and Both for message body. Just write this code. It will help you
MimeMessage mailMessage = new MimeMessage();
mailMessage.From.Add(new MailboxAddress(senderName, sender#address.com));
mailMessage.Sender = new MailboxAddress(senderName, sender#address.com);
mailMessage.To.Add(new MailboxAddress(emailid, emailid));
mailMessage.Subject = subject;
mailMessage.ReplyTo.Add(new MailboxAddress(replyToAddress));
mailMessage.Subject = subject;
var builder = new BodyBuilder();
builder.HtmlBody = "Hello There";
mailMessage.Body = builder.ToMessageBody();
try
{
using (var smtpClient = new SmtpClient())
{
smtpClient.Connect("HostName", "Port", MailKit.Security.SecureSocketOptions.None);
smtpClient.Authenticate("user#name.com", "password");
smtpClient.Send(mailMessage);
Console.WriteLine("Success");
}
}
catch (SmtpCommandException ex)
{
Console.WriteLine(ex.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}