Send email as HTML is not working. Why? - c#

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);

Related

c# MailMessage From email address display name is not working

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);

C# SMTP Client, setting Sent From parameter

I am using SmtpClient to send email like this
MailMessage mail = new MailMessage("email-from#gmail.com", "email-to#gmail.com", "Contact from " + emailFrom, text);
SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(emailAccount, accountPass);
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(mail);
This is working fine, I am receiving emails but I receive email from emailAccount address variable. I want to recieve emails from the 'From' field of message i.e email-from#gmail.com
Is it possible?
You can do something like these.
mail.From = fromEmailAddress; // Your variable that contains from address
for more details see.
https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage(v=vs.110).aspx
Sending Email to multiple Recipients with MailMessage
http://www.c-sharpcorner.com/article/smtpmail-and-mail-message-send-mails-in-net/

How to send a CC in C#. Create a duplicate

I developing an app which is an email generator, the thing is that I need to send a carbon copy of my email to the same email I am using to send the original one, basically I need to duplicate every email. But when I set the cc email the code simply ignores the CC.
C# Code:
const string fromPassword = "password";
string subject = "Subject " + sXmlArray[0].ToString();
SmtpClient smtp = new SmtpClient();
//Google SMTP Host
smtp.Host = "smtp.gmail.com";
//smtp.Host = "smtp.office365.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("myEmail#domain.com", fromPassword);
smtp.Timeout = 100000;
MailMessage message = new MailMessage(fromAddress, toAddress);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = StrContent.ToString();
//Carbon Copy
//HERE IS THE ISSUE
MailAddress CarbonCopy = new MailAddress("myEmail#domain.com");
message.CC.Add(CarbonCopy);
smtp.Send(message);
Why the code ignores this piece of code? If I declare a different email it works fine?
Can somebody help me out with this issue? Thanks in advance.

How to send email through SMTP to email address contain non-ASCII word

How to send email through SMTP to email address which contain non-ASCII word.
Consider an users with email id : téést#test.com
Code is not throwing any exception and while checking the delivery report it gave an error message:
"Technical details of permanent failure:
local-part of envelope RCPT address contains utf8 but remote server did not offer SMTPUTF8 ".
I am using .net 4.5 and my code sample is :
MailMessage message = new MailMessage();
message.From="test#test.com";
message.Subject = "subject";
message.IsBodyHtml = true;
message.Body = "body";
message.To.Add("téést#test.com");
SmtpClient smtp = new SmtpClient();
smtp.Host = "SMTPServer";
smtp.Port = "SMTPPort";
var userName = "SMTPUserName";
var password = "SMTPPassword";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(userName, password);
smtp.EnableSsl = true;
smtp.DeliveryFormat = SmtpDeliveryFormat.International;
smtp.Send(message);
Thanks In Advance.

.Net email integration won't send using godaddy SMTP Credential

Hi its been almost a day that I've been figuring things out with regards to sending email from godaddy email account to a gmail account. I have had my research online and almost tried everything but no luck .. here's what I made so far.
protected void generateEmail(){
MailMessage mail = new MailMessage ();
mail.From = new System.Net.Mail.MailAddress ("contact#company.com");
// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient ();
smtp.Port = 465; // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential ("contact#company.com", "password123!"); // [4] Added this. Note, first parameter is Email Address of the sender and the next parameter is the password.
smtp.Host = "relay-hosting.secureserver.net";
//recipient address
mail.To.Add (new MailAddress ("test#gmail.com"));
mail.To.Add (new MailAddress ("testagain#gmail.com"));
//Formatted mail body
mail.IsBodyHtml = true;
string st = "This is a Test Message";
mail.Body = st;
smtp.Send (mail);
}
Can anyone help me out ? would appreciate any hands..
protected void generateEmail()
{
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
msg.To.Add("whereEmailWillBeSent#gmail.com");
//Configure the address we are sending the mail from
MailAddress address = new MailAddress("mail#company.com");
msg.From = address;
msg.Subject ="Hi this is mail from company";
msg.Body = "Your Message";
SmtpClient client = new SmtpClient();
//for Godaddy
client.Host = "relay-hosting.secureserver.net";
client.Port = 25;
client.EnableSsl = false;
client.UseDefaultCredentials = false;
//Send the msg
client.Send(msg);
//Display some feedback to the user to let them know it was sent
}
}

Categories