Using SMTP in Visual Studio 2012 - c#

I am having problems with sending an email using SMTP, it says "waiting for local host" then eventually times out.
My assumption is that SMTP is disabled on local host, so I had a look at the directions here: http://msdn.microsoft.com/en-us/library/8b83ac7t%28v=vs.100%29.aspx But none of the steps were applicable to my computer (Windows 8, asp.net 4, VS 2012)
Is this enabled by default and the problem is my code?
Here is my code
protected void SendMail()
{
MailMessage email = new MailMessage();
email.To.Add(new MailAddress("removed"));
email.Subject = "Contact Form";
email.From = new MailAddress(emailTextbox.Text);
email.Body = "From: " + nameTextbox.Text + "<br />";
email.Body = "Message: " + commentTextbox.Text + "<br />";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 465;
smtp.Credentials = new NetworkCredential("removed", "removed");
smtp.EnableSsl = true;
smtp.Send(email);
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SendMail();
messageLabel.Text = "Thanks for your email, we will get back to you shortly.";
}
catch (Exception error) {
messageLabel.Text = "Error " + error;
}
}
Any help is appreciated, thanks!

Please see, if adding following configuration helps:
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

Related

How to send value from textBox to mail?

I got this idea of making a mail sender. I tried to look for solutions but none really worked and they were badly explained or if it is even possible to do this?
So i basically have this if else code that check's if it's empty or not and if it's not it would send the value to mail.
using System.Net.Mail; //i think this is what i need?
private void button1_Click(object sender, EventArgs e)
{
if(string.IsNullOrWhiteSpace(textBox1.Text) && string.IsNullOrWhiteSpace(textBox2.Text))
{
MessageBox.Show("You're empty!");
}
else if(Int32.Parse(textBox1.Text) != 0)
{
// send text box to mail
}
else
{
MessageBox.Show("Something went wrong.");
System.Threading.Thread.Sleep(2000);
MessageBox.Show("Closing");
System.Threading.Thread.Sleep(1000);
this.Close();
}
}
Is someone willing to direct me in the correct direction or perhaps help me explain how to do it?
You can put textBox1.Text as an email body, something like this :
mail.From = new MailAddress(emailaddress);
mail.To.Add(recipient);
mail.Subject = "Test Mail";
mail.Body = textBox1.Text; // sets the body to the text box's content
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(emailaddress, password);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
System.Windows.Forms.MessageBox.Show("Mail sent");
Try this :
private void button1_Click(object sender, EventArgs e)
{
sendMailToAdmin(textbox1.Text,textbox2.text);
}
protected void sendMailToAdmin(string uname, string email)
{
MailMessage myMsg = new MailMessage();
myMsg.From = new MailAddress("****#mail.com");
myMsg.To.Add(email);
myMsg.Subject = "New User Email ";
myMsg.Body = "New User Information\n\nUser Name: " + uname + "\nEmail : " + email;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("****#mail.com", "pass***");
smtp.EnableSsl = true;
smtp.Send(myMsg);
}

filesystemwatcher event is not triggering when excel file data are changed

I have made a web service which uses object FileSystemWatcher. Now When I try to open a file, service stopped automatically otherwise filecreated method is working properly. Here is my code,
protected override void OnStart(string[] args)
{
string dirname = #"C:\Users\Admin\Documents\Visual Studio 2017\WebSites\Project1\client";
watch = new System.IO.FileSystemWatcher(dirname, "*.*");
watch.Created += new FileSystemEventHandler(this.FileCreated);
watch.Changed += new FileSystemEventHandler(this.FileChanged);
watch.Deleted += new FileSystemEventHandler(this.FileDeleted);
watch.Renamed += new RenamedEventHandler(this.FileRenamed);
watch.IncludeSubdirectories = true;
watch.EnableRaisingEvents = true;}
and filechanged method is this -
public void FileChanged(object sender, FileSystemEventArgs e1)
{
MailMessage message = new MailMessage("***#gmail.com", "***#gmail.com");
Attachment attachfile = new Attachment(e1.FullPath);
message.Subject = "Data Changed " + e1.Name.ToString();
message.IsBodyHtml = true;
message.Body = "The Following File is Changed " + Path.GetFileName(e1.FullPath.ToString()) + " in " + " at " + DateTime.Now.ToLongTimeString() + " by " + System.Environment.UserName.ToString();
message.Attachments.Add(attachfile);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "email#gmail.com";
credentials.Password = "password";
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(message);
foreach (Attachment attachment in message.Attachments)
{
attachment.Dispose();
}
}
So, Please help me out, how to keep running web service even when I open a file and change its data.

How to create an SMTP mail?

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.

error in email sending via cloud

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

sending email is not working with my smtp server

The following is my coding for sending email with my smtp server. but it throws an smtpexception as failure to sent .
I am using windows webserver for my website.
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add("info#dhuvara.com");
Msg.Subject = txtSubject.Text;
Msg.Body = txtMessage.Text;
Msg.IsBodyHtml = true;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.dhuvara.com";
smtp.Port = 25;
Msg.Priority = MailPriority.Normal;
smtp.Credentials = new System.Net.NetworkCredential("info#dhuvara.com", "SolaiMail");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Thanks for Contact us";
// Clear the textbox valuess
txtName.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
You need to add smtp.UseDefaultCredentials = false;
It needs to be before you set the credentials.
Try to set EnableSSL = false;
It might be the problem - i've run into this problem before.
I doubt that mail.dhuvara.com is the right host to connect to.
DNS lookup for this host tells me, that it is a CNAME for go.domains.live.com and go.domains.live.com is a CNAME for domains.live.com, so I assume that you are using windows live to host your domain. Maybe you should try smtp.live.com as the mail server instead.

Categories

Resources