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.
Related
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.
Created simple tool to send test mails in c# as below
but getting error as
Mailbox name not allowed. The Server response was: 5.7.1
:Sender address rejected:not logged in
Currently i am using internal networks(sending mail using intranet.i.e. using private email server)
it is working fine with Microsoft outlook but not with my code or using telnet
config:
port no:25
try
{
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = txtHostName.Text;
//0001_013896_642014_642014.pdf
if (chkEnable.Checked)
{
smtp.EnableSsl = true;
}
else
{
smtp.EnableSsl = false;
}
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = false;
smtp.Credentials = NetworkCred;
smtp.Port = Convert.ToInt32(txtPortNO.Text);
smtp.Send(mm);
MessageBox.Show("Mail send at:" + DateTime.Now.ToString());
}
}
catch (Exception e)
{
MessageBox.Show("error Occured:" + e.Message);
}
Solved by Setting/Configuration of Display name (sender name/email setting)
due to this sender name is not visible to smtp server
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;
}
}
This code works fine on my local machine ut when I deploy it. it gives Failure sending mail error.. Please Help...
MailAddress addrsTo = new MailAddress(toEmail);
MailAddress addrsFrom = new MailAddress("XXX#XXX.com", "XXX Title");
MailMessage mailmsg = new MailMessage(addrsFrom, addrsTo);
mailmsg.Subject = mailSbjct;
mailmsg.Body = "XXX Body";
SmtpClient smtp = new SmtpClient("mail.XXX.com");
smtp.EnableSsl = false;
smtp.Port = 26;
smtp.Credentials = new NetworkCredential("XXX#XXX.com", "XXXXXXX");
try {
smtp.Send(mailmsg);
} catch (Exception exc) {
throw new XXXException(1234, "---" + exc.Message);
}
you can try this, if you are using gmail :
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
As you have mentioned in your question works fine on my local machine.
It is suggesting that the problem is of credential which you are providing to send the mail.
Edit 1
If you are using you own domain credential then it is not going to work on the server.
The user IIS should have enough authority to send mail.
IIS User More detail
http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Here is a SO link
How to send email through IIS7?
for sending email i set server name smtp.mail.yahoo.com and port is 465 i trying to send email but failed to send email
what is the correct servername and smtp port to send email using yahoo
what other configuration i needed to set ?
my code is here :
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(address);
message.Subject = subject;
message.From = new System.Net.Mail.MailAddress(from);
message.Body = body;
message.Bcc.Add(bcc);
message.CC.Add(cc);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.mail.yahoo.com");
smtp.Credentials = new System.Net.NetworkCredential(emailid,password);
smtp.Port = 465;
smtp.EnableSsl = true;
smtp.Send(message);
I just tried the code and I think the yahoo mail server does not use SSL, because if you comment out
//smtp.Port = 465;
//smtp.EnableSsl = true;
it works.
I am not confirm about your smtp server settings these are working fine for me..
replace your smtp server settings and have idea from this code snippet.
some of the smtp server standart settings are here:
http://www.emailaddressmanager.com/tips/mail-settings.html
//Send mail using Yahoo id
protected void Button2_Click(object sender, EventArgs e)
{
String frmyahoo = "fromid#yahoo.com"; //Replace your yahoo mail id
String frmpwd = "fromidpwd"; //Replace your yahoo mail pwd
String toId = txtTo.Text;
String ccId = txtCc.Text;
String bccId = txtBcc.Text;
String msgsubject = txtSubject.Text;
String mailContent = txtContent.Text;
try
{
MailMessage msg = new MailMessage();
msg.To.Add(toId);
MailAddress frmAdd = new MailAddress(frmyahoo);
msg.From = frmAdd;
//Check user enter CC address or not
if (ccId != "")
{
msg.CC.Add(ccId);
}
//Check user enter BCC address or not
if (bccId != "")
{
msg.Bcc.Add(bccId);
}
msg.Subject = msgsubject;
//Check for attachment is there
if (FileUpload1.HasFile)
{
msg.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
msg.IsBodyHtml = true;
msg.Body = mailContent;
SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
clear();
Label1.Text = "Mail Sent Successfully";
}
catch (Exception ex)
{
Label1.Text = "Unable to send Mail Please try again later";
}
}