I tried to send mail from my asp.net web application.
I want to send my application password to the user mail id, for that I took the password from datatabase using getdetailss function.
btn4getPwd is the button calling the mailing function.
txtusername.Text is the text box containing sending mail address. All values are receiving correctly, no error occur but it's not working ..!
protected void btn4getPwd_Click(object sender, EventArgs e)
{
if (txtusername.Text.Trim() != "")
{
em.username = txtusername.Text.Trim();
DataTable forget = em.getdetailss(10);
string passwd = (forget.Rows[0]["PassCode"].ToString());
try
{
string Subject = "Your NLS Password";
string Body = passwd;
string ToEmail = txtusername.Text.Trim();
string SMTPUser = "mymail#gmail.com", SMTPPassword = "pswd";
MailMessage mail = new MailMessage();
mail.From = new MailAddress(SMTPUser, "AspnetO");
mail.To.Add(ToEmail);
mail.Body = Body;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);
smtp.EnableSsl = true;
smtp.Send(mail);
Response.Write("<script language=javascript>alert('Mail send...!! ')</script>");
}
catch (SmtpException ex)
{
lbl4get.Text = "SmtpException ";
}
catch (Exception ex)
{
lbl4get.Text = "Exception";
}
}
else { Response.Write("<script language=javascript>alert('Invalid USERNAME...!! ')</script>"); }
}
smtp.Port = 25; is default port but for as you are sending over SSL use port 587 or 465 (nonstandard, but sometimes used for legacy reasons). Assume NetworkCredential are correct.
I have deployed my website to azure cloud and I am trying to send emails to my client. When I have tested this on local host it works properly but on moving to cloud it shows several errors like sometimes:
-> connection time out and i have declared timeout as 150000
->The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. (I have made less secure app allowed to mail account)
Here I am attaching my code. Please review it and suggest a way to resolve it.
Thank you all in advance.
protected void Button2_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
MailAddress from = new MailAddress("bvpcsccloud#gmail.com");
MailAddress to = new MailAddress(Label7.Text);
string subjectText = "E-appointment request";
string bodyText = #"Hi, This mail is for the request of e-appointment from our patient:"+TextBox4.Text.ToString()+"<br />"+ "The patient has age:";
bodyText += TextBox5.Text.ToString()+"<br /> Disease symbols are"+TextBox2.Text.ToString()+"<br /> Disease name:"+ TextBox3.Text.ToString();
bodyText += "<br />Patient address is" + TextBox1.Text.ToString();
bodyText += "<br /> preffered date choosen by patient is" + DropDownList1.SelectedItem.Text + "-" + DropDownList2.SelectedItem.ToString() + "-" + DropDownList3.SelectedItem.Text;
msg.To.Add(to);
msg.From = from;
msg.Subject = subjectText;
msg.Body = bodyText;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("bvpcsccloud#gmail.com", "password");
//smtp.UseDefaultCredentials = false;
try
{
smtp.EnableSsl = true;
smtp.Timeout = 150000;
smtp.Send(msg);
smtp.Dispose();
Response.Redirect("~/appointment_booked.aspx");
}
catch (Exception ex)
{
throw ex;
}
}
I have tried various useful code provided by various programmers. I found 1 easy to use, and tried to design a contact form. Its working fine while deploying in VS.NET 2008. But when I publish it onto the web server, it is showing "Timed Out Error".
I have also set the Trust Level to full in the Hosting Server(GoDaddy).
Also, tried this inside web.config file.
Still, not working.
Here is the Code :
protected void SendMail()
{
string fromAddress = "abc#gmail.com";// Gmail Address from where you send the mail
string toAddress = "xyz#gmail.com";
const string fromPassword = "password";//Password of your gmail address
string subject = "Enquiry from Website Contact Form.";
string body = "From: " + txtName.Text + "\n";
body += "Email: " + txtEmail.Text + "\n";
body += "Contact: " + txtPhone.Text + "\n";
body += "Message: \n" + txtMessage.Text + "\n";
lblMsgSend.Visible = true;
try
{
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);
Response.Write("Message sent successfully.");
lblMsgSend.Text = "Message sent successfully.";
}
catch (System.Net.Mail.SmtpException se)
{
lblMsgSend.Text = "Message sending failed. Details : " + se.Message.ToString() + se.StackTrace.ToString();
}
catch (Exception ex)
{
lblMsgSend.Text = "Message sending failed. Details : " + ex.Message.ToString() + ex.StackTrace.ToString();
}
}
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.
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;