I'm using the following method to send an email. I want to be able to format the email with bold text.
Ex.
From: name
Email: email address
Message: message
How would I do this?
protected void SendMail()
{
var fromAddress = "myemail#gmail.com";
var toAddress = "myotheremail#gmail.com";
const string fromPassword = "mypassword";
string subject = "New Email from Website";
string body = "From: " + name.Text + "\n";
body += "Email: " + email.Text + "\n";
body += "Message: \n" + message.Text + "\n";
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;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
Set isBodyHtml to true, the following code describes it,
To send a bold text you can use "<b> My bold text </b> ".
To send an italicized text you can use "<i> Italic text </i> ".
To send an underlined text you can use "<u> underlined text </u>".
You can copy and use the following method. By using this method, it will be very easy to send emails.
public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
{
try
{
MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 25,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("your email address", "your password")
};
using (MailMessage message = new MailMessage(FromAddr, ToAddr)
{
Subject = Subject,
Body = Body,
IsBodyHtml = IsBodyHTML,
BodyEncoding = System.Text.Encoding.UTF8,
})
{
smtp.Send(message);
}
return true;
}
catch
{
return false;
}
}
When you call this method call it like this
SendEmail("Here address to" , "Here to name" , "Here from", "here from name", "here subject" , here Body, " Here whether HTML or Plain" )
You need only few minor changes.
say IsBodyHtml is true
replace all \n with <br/>
and here's the final the code
protected void SendMail()
{
var fromAddress = "myemail#gmail.com";
var toAddress = "myotheremail#gmail.com";
const string fromPassword = "mypassword";
string subject = "New Email from Website";
string body = "From: " + name.Text + "<br/>";
body += "Email: " + email.Text + "<br/>";
body += "Message: <br/>" + message.Text + "<br/>";
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;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body,IsBodyHtml:true);
}
Hope that helps.
Related
I'm trying encode a param email in an url, to do this I'm trying use HttpUtility.HtmlEncode but it doesn't work because the parameter isn't encoded. How could I do this ?
Trying
private void sendMailToValidation(String email){
//String server = HttpContext.Current.Request.Url.Host;
//encripta a url HttpUtility.HtmlEncode
string absoluteURL = "http://" + HttpContext.Request.Url.Authority + "/Usuario/validaEmail?email=";
try{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.From = new MailAddress("myemail#gmail.com");
mail.Subject = "MeuPedido validação de email";
string url = absoluteURL + HttpUtility.HtmlEncode(email);
mail.Body = "<html><body>Test</body></html>";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "xxxx");// Enter seders User name and password
smtp.EnableSsl = true;
smtp.Timeout = 20000;
smtp.Send(mail);
}catch (SmtpException e){
Console.WriteLine(e.StackTrace);
}
}
How can I send an activation email when new users register in my website using asp.net c#? I have tried the steps described
this link but I have faced this error:
System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
Is there any solution to this or any other way to send an email?
this is my code
using (MailMessage mm = new MailMessage("sender#gmail.com", txtEmail.Text))
{
mm.Subject = "Account Activation";
string body = "Hello " + txtUsername.Text.Trim() + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("sender#gmail.com", "<password>");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
The proposed duplicate did not solve the problem. I am already using port 587.
See the code below:
public static class email_utility
{
public static async Task<bool> send_email(this string body, string subject, string email, int try_count)
{
return await Task.Run(() =>
{
var cli = new SmtpClient();
using (var message = new MailMessage(config.sender_email, email))
{
message.Subject = subject;
message.SubjectEncoding = UTF8Encoding.UTF8;
message.Body = body;
message.BodyEncoding = UTF8Encoding.UTF8;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
for (var count = 0; count < try_count; ++count)
{
try
{
lock (config.sender_email)
{
cli.Send(message);
return true;
}
}
catch (SmtpFailedRecipientsException)
{
return false;
}
catch (SmtpException)
{
Thread.Sleep(config.send_timeout);
}
}
return false;
}
});
}
}
Try this, it works perfectly and you only have to change the fields that are before the var smtp = newSMTPCient.
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
The following code unable to send an emails to customers and it not throwing any exception. The code it not send any email or exception but executed.I am completely new about the asp.net. Some one can help me how to resolve the problem.
Code:
try
{
String userName = "ramesh";
String passWord = "123456";
String sendr = "ramesh#gmail.com";
String recer = "customer#yahoo.com";
String subject = "Comformation ";
String body = "Dear Customer";
MailMessage msgMail = new MailMessage(sendr, recer, subject, body);
int PortNumber = 25;
SmtpClient smtp = new SmtpClient("smtp.test.com", PortNumber);
msgMail.IsBodyHtml = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential(userName, passWord);
smtp.Send(msgMail);
MsgLP.Text = "Emailed to Customer..";
LogInLink.Visible = true;
}
catch (Exception ex){
AuditLog.LogError("ErrorE-mail " + ex.Message);
}
You have to set smtp.EnableSsl=true and use port number 587. Your final code will be this:
try
{
String userName = "ramesh";
String passWord = "123456";
String sendr = "ramesh#gmail.com";
String recer = "customer#yahoo.com";
String subject = "Comformation ";
String body = "Dear Customer";
MailMessage msgMail = new MailMessage(sendr, recer, subject, body);
int PortNumber = 587; //change port number to 587
SmtpClient smtp = new SmtpClient("smtp.gmail.com", PortNumber); //change from test to gmail
smtp.EnableSsl = true; //set EnableSsl to true
msgMail.IsBodyHtml = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential(userName, passWord);
smtp.Send(msgMail);
MsgLP.Text = "Emailed to Customer..";
LogInLink.Visible = true;
}
catch (Exception ex){
AuditLog.LogError("ErrorE-mail " + ex.Message);
}
I tested this code with my credentials and it works fine.
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
mm.From = new MailAddress("email#gmail.com");
mm.To.Add("email#gmail.com");
System.Net.Mail.Attachment attachment;
string strFileName;
strFileName = "Uploadfile/" + "200814062455PM_Admin_Screenshot (10).JPEG";
attachment = new System.Net.Mail.Attachment(Server.MapPath(strFileName));
mm.Attachments.Add(attachment);
mm.Body = ("<html><head><body><table><tr><td>Hi</td></tr></table></body></html><br/>"); ;
mm.IsBodyHtml = true;
mm.Subject = "Candidate " + Name + " for your Requirement " + Jobtt + " ";
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("email#gmail.com", "password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
object userstate = mm;
client.Send(mm);
I've got a c# process that send emails to customers with a hyperlink in the mail. The mail is send from a SQL Server stored proc. My c# program just invokes the sp.
The hyperlink works fine in Outlook, but on online gmail it only shows as text. It is not clickable.
My mail text looks something like:
Hi.
This is the hyperlink:<br>
<a href=\"serveraddress\Documents\\123_128635312685687531322.gif\">
Click Here</a><br><br>
What should I do to fix it?
EDIT:
My code:
string email = "xx#gmail.com;
string password = "MyPassword";
var credentials = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email, senderName);
msg.To.Add(new MailAddress(toAddress));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.Send(msg);
EDIT 2:
Compile message being send:
string message = #"Hi. <br>
This is the intro line in the mail message.<br>";
using (DataTable dtLinks = data.ExecuteDataSet(#"SELECT *
FROM LessonFiles
WHERE Course = " + dr["Course"].ToString().DBValue() + #" AND
Lesson = " + dr["NextLesson"].ToString().DBValue()).Tables[0])
{
int i = 0;
foreach (DataRow drLink in dtLinks.Rows)
{
i += 1;
message += "<a href=\"" + drLink["Link"].ToString() + "\">" + drLink["Lesson"].ToString();
message += i == 1 ? "" : " file " + i;
message += "</a>" + "<br>";
}
}
message += "<br>Regards<br><br>";
try to add target="_blank", like this...
message += "<a href=\"" + drLink["Link"].ToString() + "\"target=\"_blank\">" + drLink["Lesson"].ToString();
Seems like something was funny with the hyperlink itself.
Using http://serveraddress/Documents/logoColourBG635315550177822533.jpg seems to work.
The original contained backslashes in the path. The fact that it showed the hyperlink in Outlook led me to believe the address was correct.
Thanks for all your help.
creating mail message object...
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(UserName, Password);
smtp.Timeout = 20000;
MailMessage Msg = new MailMessage();
Msg.IsBodyHtml = true;
MailAddress fromMail = new MailAddress(SenderID);
Msg.From = fromMail;
Msg.To.Add(new MailAddress(TosendID));
Msg.Subject = subject;
Msg.Body = body;
In body add ur code.....
Hope This Helps.........
I have a page with two update panels - updatepanelform (visible), updatepanelthanks (hidden). I have used this method to send mail on click event of button on asp page.
protected void BtnRfqClick(object sender, EventArgs e)
{
// Gmail Address from where you send the mail
var fromAddress = TbEmailRfq.Text.ToString();
// any address where the email will be sending
var toAddress = "user#gmail.com";
//Password of your gmail address
var name = TbNameRfq.Text.ToString();
const string fromPassword = "password";
// Passing the values and make a email formate to display
string subject = "Welcome To world";
string body = "Name :" + TbNameRfq.Text.ToString() + Environment.NewLine +
"Email Id :" + TbEmailRfq.Text.ToString()
+ Environment.NewLine + TaComment.InnerText.ToString();
// smtp settings
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("user email", "password");
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
Everything is working fine but I am not able to set visibility of updatepanelform to hidden and updatepanelthanks to visible after it sent email successfully.
Place the following after you send the email:
updatepanelform.Visible = false;
updatepanelthanks.Visible = true;