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;
Related
I am a beginner programmer in C#, I want to create a contact form , where the user can enter any email address as the "From" address, and they can send a message. Every time it sends, it says the "From" address is the credentials that I am using. How do I change the From address to the address based on the user's input like any other contact form. Should I use a different Smtp Server?
Thanks
protected void sendbttn_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(fromtxt.Text);
message.To.Add("toEmail");
message.Subject = "Subject: " + subjecttxt.Text;
message.Body = mesgtxt.Text;
message.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("Credemail", "Credpassword");
smtpClient.EnableSsl = true;
smtpClient.Send(message);
MessageBox.Show("Your email has been sent!");
fromtxt.Text = "";
subjecttxt.Text = "";
mesgtxt.Text = "";
}
catch
{
MessageBox.Show("Something went wrong, please try again");
}
If your host is not registered as official mail provider, you should never even think about sending E-Mail in the name of somebody else.
Instead use a dedicated address as sender and to authenticate at the smart host and remember to store the credentials in a save location so nobody can "adopt" them - the program sourcecode as in the example is definitely not a save place, store them in IIS property fields.
The address entered in the form may be used as reply-to and possibly as the senders display name.
The body will not be html, too. The property therefore should be 'false'.
string credMail = "example#gmail.com";
string credPasswd = "example_p#asswd";
MailMessage message = new MailMessage();
message.From = new MailAddress(credMail,fromtxt.Text);
message.To.Add("toEmail");
message.Subject = "Subject: " + subjecttxt;
message.Body = mesgtxt.Text;
message.IsBodyHtml = false;
message.ReplyToList.Add(fromtxt.Text);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(credMail, credPasswd);
smtpClient.EnableSsl = true;
smtpClient.Send(message);
MessageBox.Show("Your email has been sent!");
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 am trying to send email with unsubsrible link in my aspx page. But when I check my email id It display only text. There was no link. Here is my code generate a email
string bodyContent = CKEditor1.Text;
string userLink = "http://www.abc.in/Message.aspx?action=rmsb&oldsubuser=";
string footerLink = "</br></br></br>You are receiving this mail because you have subscribed to our newsletter. If you do not wish to receive the mail, Click <a href='" + userLink + "" + ids[i].ToString() + "'>Here</a>";
bodyContent = bodyContent + footerLink;
EmailSend newsletter = new EmailSend();
newsletter.NewsLetterSend(ids[i].ToString(), bodyContent.Replace("'", "''"), txtSubject.Text.Replace("'", "''"));
//EmailSend.SendMailMessage("faredpt#gmail.com", ids[i].ToString(), "", "", txtSubject.Text, bodyContent);
bodyContent = bodyContent.Replace(footerLink, " ");
Here is the code for NewsLetterSend function
public void NewsLetterSend(string getemailAdd, string msgBody, string subject)
{
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("admin#abc.in", "admin#abc.in");
mail.To.Add(getemailAdd.Trim());
//set the content
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = msgBody;
mail.Priority = MailPriority.High;
//set the smtp settings
SmtpClient smtp = new SmtpClient("abc.in");
smtp.EnableSsl = false;
smtp.Credentials = new System.Net.NetworkCredential("admin#abc.in", "i#abc!23#");
//smtp.Port = 3535;
smtp.Port = 25;
//send email
smtp.Send(mail);
return;
}
Now this code is sending email successfully but unable to add link in my email. It show me simple text
Please tell me why this happening
You are replacing the quotation mark in href with two quotation marks!
Invalid statement: bodyContent.Replace("'", "''")
This will render your HTML invalid.
Could you try to replace </br></br></br> with <br/><br/><br/>? Probably because of invalid br tags your email is considered as not valid HTML and links become broken.
I have implemented a contact form to a web app but when I try to send the email I get JavaScript runtime errors, http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp at line 37 which is an empty line.
Does anyone have an idea as to what is wrong with my implementation or do I have to change some settings to test it on local host?
//calls the SendMail() and resets the textboxes
protected void sendBtn_Click(object sender, EventArgs e)
{
try
{
//here on button click what will done
SendMail();
confirmationLbl.Text = "Your email has been sent to customer support.";
confirmationLbl.Visible = true;
subjectTbx.Text = "";
emailTbx.Text = "";
nameTbx.Text = "";
questionTbx.Text = "";
}
catch (Exception ex) {
confirmationLbl.Text = "Your email has failed to send,please check your connection.";
Console.WriteLine("IOException source: {0}", ex.Message );
}
//line 37 is here, which is blank
}
//method to compose email from textboxes
protected void SendMail()
{
// Gmail Address from where you send the mail
var fromAddress = emailTbx.Text.ToString();
// any address where the email will be sending
var toAddress = "brianDoe#gmail.com";
//Password of your gmail address
const string fromPassword = "Password";
// Passing the values and make a email formate to display
string subject = subjectTbx.Text.ToString();
string body = "From: " + nameTbx.Text + "\n";
body += "Email: " + emailTbx.Text + "\n";
body += "Subject: " + subjectTbx.Text + "\n";
body += "Question: \n" + questionTbx.Text + "\n";
// 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(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
You should check your parsed HTML for JavaScript error rather than C# code. Also, make sure POP/IMAP access is enabled in Gmail under settings.
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.