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;
}
}
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 mails from Microsoft Exchange from our organization, but I get the error message: The customer is not allowed to submit mail to this server. The response from the server was: 4.7.1 : Relay access denied
My Code is :
private void button2_Click(object sender, EventArgs e)
{
string Username = "MyUsername";
string Password = "MyPassword ";
string SmtpServer = "OurSmtpServer";
string From = Username + "SmtpServer ";
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
NetworkCredential basicCredential = new NetworkCredential(Username, Password, SmtpServer);
MailMessage message = new MailMessage();
System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress(From);
// setup up the host, increase the timeout to 5 minutes
smtpClient.Host = SmtpServer;
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
//smtpClient.Credentials = basicCredential;
smtpClient.UseDefaultCredentials = true;
smtpClient.Timeout = (60 * 5 * 1000);
message.From = fromAddress;
message.Subject = " - " + DateTime.Now.Date.ToString().Split(' ')[0];
message.IsBodyHtml = true;
message.Body = " -88888888888888888888888888888 ";
message.To.Add("recipient#gmail.com");
smtpClient.Send(message);
}
We use windows authentication normally in our organization. Is this the cause of the problem? if so how to use windows authentication in my code?
And thank you in advance for your help !!
I have created a Windows application which is used to send emails. i have given credentials. i turned on google/settings/lesssecure apps. Eventhough its not sending. Its showing the error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required here is my code.
MailMessage message = new MailMessage();
message.From = new MailAddress("Selvacsebe23#gmail.com");
string[] mailaddress = new string[count];
int i;
if (textSubject.Text != string.Empty)
{
message.Subject = textSubject.Text;
if (textBody.Text != string.Empty)
{
message.To="Selvakesavan#gmail.com"
message.IsBodyHtml = true;
string tmpBody = "Hello " + "," + "<br/> <br/>" + textBody.Text + "<br/> <br/>" + "Thanks and Regardds";
message.Body = tmpBody;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("selvacsebe23#gmail.com", "mypassword");
message.Priority = MailPriority.High;
client.EnableSsl = true;
client.Send(message);
MessageBox.Show("Mail has sent successfully !!!", "Success !");
}
else
{
MessageBox.Show("Please Enter Body of the Message !");
}
}
else
{
MessageBox.Show("Please Enter Subject !");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Failure !");
log.Fatal(ex.Message);
}
}
If you turn on 2-step-verification, then you need to login using app-specific-password. You can create it here:
https://support.google.com/accounts/answer/185833?hl=en. If you use your normal password, then you will get exception : 5.5.1 Authentication Required. You don't need lots of code, this code is enough to send email without attachment:
const string from = "user1#gmail.com";
const string to = "user2#yahoo.com";
const string subject = "This is subject";
const string body = "This is body";
const string appSpecificPassword = "akdfkajsdhklakdfh";
var mailMessage = new MailMessage(from, to, subject, body);
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(from, appSpecificPassword);
smtpClient.Send(mailMessage);
}
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;
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.