ASPX email HTML format - c#

I have this aspx function which send email. Unfortunetely, it does not render html tag on gmail or outlook, but on my android phone looks fine. How to fix it? thanks.
protected void SendMail()
{
var fromAddress = "device#email.com";
var toAddress = "toEMAIL#gmail.com";
const string fromPassword = "****";
var subject = "subject";
string body = " <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\">";
body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">";
body += "</HEAD><BODY>";
body += "<b>some text some text</b><br></br>";
body += "</BODY></HTML>";
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(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(fromAddress, toAddress, subject, body);
}
EDITED:
So here is what i have now. But the compiler gives this error:
Line 392: mail.From = fromAddress;
Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'
var fromAddress = "device#***";
var toAddress = "*****#gmail.com";
const string fromPassword = "******";
var subject = "New Project Started!";
string body = "<b>TA:</b>";
MailMessage mail= new MailMessage();
mail.From = fromAddress;
mail.To = toAddress;
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
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(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(mail);
EDITED:
My working solution:
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
var fromAddress = "****"; // mail Address from where you send the mail
var toAddress = "****";
const string fromPassword = "***"; //Password of your gmail address
var subject = "New Job Created!";
// set smtp-client with basicAuthentication
SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
mySmtpClient.Port = 587;
mySmtpClient.EnableSsl = true;
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new
System.Net.NetworkCredential(fromAddress, fromPassword);
mySmtpClient.Credentials = basicAuthenticationInfo;
// add from,to mailaddresses
MailAddress from = new MailAddress(fromAddress, "");
MailAddress to = new MailAddress(toAddress, "Devision");
MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
// set subject and encoding
myMail.Subject = subject;
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
// set body-message and encoding
myMail.Body = "<b>TEST</b>";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text or html
myMail.IsBodyHtml = true;
mySmtpClient.Send(myMail);

Create MailMessage Class. The key thing is to specify that the Body of the is HTML this is done with property IsBodyHtml
MailMessage mail= new MailMessage();
mail.From = fromAddress;
mail.To = toAddress;
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
//other stuff
smtp.Send(mail);

Related

SmtpException - Mailbox unavailable [duplicate]

I can't understand why this code is not working. I get an error saying property can not be assigned
MailMessage mail = new MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.To = "user#hotmail.com"; // <-- this one
mail.From = "you#yourcompany.example";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
mail.To and mail.From are readonly. Move them to the constructor.
using System.Net.Mail;
...
MailMessage mail = new MailMessage("you#yourcompany.example", "user#hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
This :
mail.To = "user#hotmail.com";
Should be:
mail.To.Add(new MailAddress("user#hotmail.com"));
Finally got working :)
using System.Net.Mail;
using System.Text;
...
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("user#gmail.com","password");
MailMessage mm = new MailMessage("donotreply#domain.example", "sendtomyemail#domain.example", "test", "test");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mm);
sorry about poor spelling before
public static void SendMail(MailMessage Message)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.googlemail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("myemail#gmail.com", "password");
client.Send(Message);
}
This is how it works for me. Hope you find it useful
MailMessage objeto_mail = new MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "smtp.internal.mycompany.com";
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("user", "Password");
objeto_mail.From = new MailAddress("from#server.com");
objeto_mail.To.Add(new MailAddress("to#server.com"));
objeto_mail.Subject = "Password Recover";
objeto_mail.Body = "Message";
client.Send(objeto_mail);
First go to https://myaccount.google.com/lesssecureapps and make Allow less secure apps true.
Then use the below code. This below code will work only if your from email address is from gmail.
static void SendEmail()
{
string mailBodyhtml =
"<p>some text here</p>";
var msg = new MailMessage("from#gmail.com", "to1#gmail.com", "Hello", mailBodyhtml);
msg.To.Add("to2#gmail.com");
msg.IsBodyHtml = true;
var smtpClient = new SmtpClient("smtp.gmail.com", 587); //**if your from email address is "from#hotmail.com" then host should be "smtp.hotmail.com"**
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential("from#gmail.com", "password");
smtpClient.EnableSsl = true;
smtpClient.Send(msg);
Console.WriteLine("Email Sent Successfully");
}
If you want to have your email and password not appear in your code and want your company email client server to use your windows credentials use below.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Source
This just worked for me as of March 2017.
Started with solution from above "Finally got working :)" which didn't work at first.
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("<me>#gmail.com", "<my pw>");
MailMessage mm = new MailMessage(from_addr_text, to_addr_text, msg_subject, msg_body);
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mm);
This answer features:
Using 'using' whenever possible (IDisposable interfaces)
Using Object initializers
Async Programming
Extract SmtpConfig to external class (adjust it to your needs)
Here's the extracted code:
public async Task SendAsync(string subject, string body, string to)
{
using (var message = new MailMessage(smtpConfig.FromAddress, to)
{
Subject = subject,
Body = body,
IsBodyHtml = true
})
{
using (var client = new SmtpClient()
{
Port = smtpConfig.Port,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Host = smtpConfig.Host,
Credentials = new NetworkCredential(smtpConfig.User, smtpConfig.Password),
})
{
await client.SendMailAsync(message);
}
}
}
Class SmtpConfig:
public class SmtpConfig
{
public string Host { get; set; }
public string User { get; set; }
public string Password { get; set; }
public int Port { get; set; }
public string FromAddress { get; set; }
}
MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text);
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
if (fuAttachment.HasFile)//file upload select or not
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
Response.write("Send Mail");
View Video:
https://www.youtube.com/watch?v=bUUNv-19QAI
smtp.Host = "smtp.gmail.com"; // the host name
smtp.Port = 587; //port number
smtp.EnableSsl = true; //whether your smtp server requires SSL
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
Go through this article for more details
Just need to try this:
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "companyemail";
string password = "password";
string emailTo = "Your email";
string subject = "Hello!";
string body = "Hello, Mr.";
MailMessage mail = new MailMessage();
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
MailKit is an Open Source cross-platform .NET mail-client library that is based on MimeKit and optimized for mobile devices.
It has more and advance features better than System.Net.Mail
Microsoft TNEF support via MimeKit.
Download nuget package from here.
See this example you can send mail
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.TextBody = "Hello There";
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());
}
Initialize the MailMessage with sender and reciever's email addresses. It should be something like
string from = "codeforwin#gmail.com"; //Senders email
string to = "reciever#gmail.com"; //Receiver's email
MailMessage msg = new MailMessage(from, to);
Read the full code snippet of how to send emails in c#
this would work too..
string your_id = "your_id#gmail.com";
string your_password = "password";
try
{
SmtpClient client = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(your_id, your_password),
Timeout = 10000,
};
MailMessage mm = new MailMessage(your_iD, "recepient#gmail.com", "subject", "body");
client.Send(mm);
Console.WriteLine("Email Sent");
}
catch (Exception e)
{
Console.WriteLine("Could not end email\n\n"+e.ToString());
}
//Hope you find it useful, it contain too many things
string smtpAddress = "smtp.xyz.com";
int portNumber = 587;
bool enableSSL = true;
string m_userName = "support#xyz.com";
string m_UserpassWord = "56436578";
public void SendEmail(Customer _customers)
{
string emailID = gghdgfh#gmail.com;
string userName = DemoUser;
string emailFrom = "qwerty#gmail.com";
string password = "qwerty";
string emailTo = emailID;
// Here you can put subject of the mail
string subject = "Registration";
// Body of the mail
string body = "<div style='border: medium solid grey; width: 500px; height: 266px;font-family: arial,sans-serif; font-size: 17px;'>";
body += "<h3 style='background-color: blueviolet; margin-top:0px;'>Aspen Reporting Tool</h3>";
body += "<br />";
body += "Dear " + userName + ",";
body += "<br />";
body += "<p>";
body += "Thank you for registering </p>";
body += "<p><a href='"+ sURL +"'>Click Here</a>To finalize the registration process</p>";
body += " <br />";
body += "Thanks,";
body += "<br />";
body += "<b>The Team</b>";
body += "</div>";
// this is done using using System.Net.Mail; & using System.Net;
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
}
send email by smtp
public void EmailSend(string subject, string host, string from, string to, string body, int port, string username, string password, bool enableSsl)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtpServer = new SmtpClient(host);
mail.Subject = subject;
mail.From = new MailAddress(from);
mail.To.Add(to);
mail.Body = body;
smtpServer.Port = port;
smtpServer.Credentials = new NetworkCredential(username, password);
smtpServer.EnableSsl = enableSsl;
smtpServer.Send(mail);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

How to send Mail Using SMTP?

So far I have this CODE for email validation and a Mail to send Using SMTPClient However it won't work ,it won't send to the gmail stated. But i think there is no problem with my code. I need some help to make a way for sending Mail .
string validEmailPattern = #"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
+ #"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
+ #"#[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
Regex ex = new Regex(validEmailPattern, RegexOptions.IgnoreCase);
if (ex.IsMatch(TextBox1.Text))
{
MailMessage m = new MailMessage();
m.From = new MailAddress("kennethmontealto91#gmail.com");
m.To.Add(new MailAddress("kennethmontealto91#gmail.com"));
m.Subject = "Try";
m.Body = "TEST";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential()
{
UserName = "kennethmontealto91#gmail.com",
Password = "********"
};
smtp.EnableSsl = true;
smtp.Send(m);
Change Your smtp.Host = "yourdomainname.com";
Try this code after **smtp.EnableSsl = true;**
string validEmailPattern = #"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
+ #"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
+ #"#[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
Regex ex = new Regex(validEmailPattern, RegexOptions.IgnoreCase);
if (ex.IsMatch(TextBox1.Text))
{
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential()
{
UserName = "kennethmontealto91#gmail.com",
Password = "********"
};
smtp.EnableSsl = true;
MailMessage m = new MailMessage();
m.From = new MailAddress("kennethmontealto91#gmail.com");
m.To.Add(new MailAddress("kennethmontealto91#gmail.com"));
m.Subject = "Try";
m.Body = "TEST";
smtp.Send(m);
}

C# mails not being sent sometimes, but no error is thrown

This is my code :
public static void SendMail(string to, string subject, string body, string mailTitle)
{
MailMessage mail = new MailMessage();
mail.Subject = subject;
mail.From = new MailAddress("***", mailTitle);
mail.To.Add(to);
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient c = new SmtpClient("leavingstone.net", 25);
c.EnableSsl = false;
c.Credentials = new NetworkCredential("***", "***");
c.Send(mail);
}
Sometimes mails are sent, other times not - but no error is thrown.
Is it possible to fix the problem from code, or is it host fail?
Try my SMTP code, works perfect for me !
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.domain.com";
smtp.EnableSsl = Convert.ToBoolean("false");
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "no-reply#domain.com";
NetworkCred.Password = "**************";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = Convert.ToInt16(25);
smtp.Send(mm);

Send Email With C# (Cpanel Hosting)

I have bought a cPanel host and the SMTP server information is:
This is my code:
string smtpAddress = "mandane.hostcream.com";
int portNumber = 465;
bool enableSSL = true;
string emailFrom = "mahabadi#exirsec.ir";
string password = Authenitication.PassWord;
string emailTo = To.Text;
string subject = Subject.Text;
string body = Body.Text;
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
When I run my code and click on the send button after 1 or 2 minutes this appears:
Additional information: Failure sending mail.
What am I doing wrong?
I think you missed something, try this:
SmtpClient smtpClient = new SmtpClient();
NetworkCredential smtpCredentials = new NetworkCredential("email from","password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("email from");
MailAddress toAddress = new MailAddress("email to");
smtpClient.Host = "smpt host address";
smtpClient.Port = your_port;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = smtpCredentials;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Timeout = 20000;
message.From = fromAddress;
message.To.Add(toAddress);
message.IsBodyHtml = false;
message.Subject = "example";
message.Body = "example";
smtpClient.Send(message);
It seems that you cannot reach Yahoo address on port 465, please check if this address reachable first because it appears to be a network issue.

how to send email without go to spam mail

MailAddress mailFrom = new MailAddress("test#smtp.com");
MailAddress mailTo = new MailAddress("tester#gmail.com");
MailMessage mail2 = new MailMessage(mailFrom, mailTo);
mail2.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "xxx.xx.xx.xxx"; // smtp host ip
mail2.Subject = "Testing.";
mail2.Body = "Hello";
mail2.SubjectEncoding = System.Text.Encoding.UTF8;
mail2.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(mail2);
the above is my function that use to send an email via smtp, but I realized all the mail was located in my spam mail folder (Gmail). Is there anyway that can solve it ?
IsBodyHTML is marked true, but you're only providing text/html. You minimally need to include an alternate view with text
mail2.Body = "Hello";
make sure you not using Mail from and mailto is same address or
MailMessage mail2 = new MailMessage(mailFrom, mailTo);
UPDATE
mail2.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "xxx.xx.xx.xxx"; // smtp host ip
mail2.Subject = "Testing.";
mail2.Body = "Hello";
string html = "html";
// here is example to user AlternateViews
mail2.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, new System.Net.Mime.ContentType("text/html"));
string Plaintext ="plain text";
mail2.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(Plaintext, new System.Net.Mime.ContentType("text/plain"));
mail2.SubjectEncoding = System.Text.Encoding.UTF8;
mail2.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(mail2);
Try this and revert.
string mailServer;
int port;
string mailId, mailPass;
string subject;
string mailTo;
subject="something";
StringBuilder mailBody = new StringBuilder();
mailTo = "someone#gmail.com";
mailServer = "smtp.gmail.com";
mailId = "something#gmail.com";
myString.Length = 0;
myString.Append("<html><body><div>BODY CONTENT</div></body></html>");
mailPass = "xxxxxx";
port = 587;
MailMessage mail = new MailMessage(mailId, mailTo, subject, myString.ToString());
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(mailServer, port);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mailId, mailPass);
smtp.UseDefaultCredentials = false;
smtp.Credentials = nc;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);

Categories