Access denied microsoft exchange of our organization using c # - c#

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 !!

Related

How to send an email from any Email Address in ASP.Net/C# Webforms

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!");

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

Server Connection using SMTP

I am trying to send a default passwords using smtp for users who want to retrieve their new passwords!
My Code :
public void SendUserPass()
{
string sql = "Select Username, Password from Registration
where Email='" + Email + "'";
DataTable table = new DataTable();
table = db.RunQuery(sql);
MailMessage message = new MailMessage("Esco#gmail.com", Email);
message.Subject = "ESCO -Forgot Password";
message.Body = "Username " + table.Rows[0][0].ToString() + "<br/> Password" +
table.Rows[0][1].ToString();
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("Esco#gmail.com", "Mypassword");
smtp.EnableSsl = true;
smtp.Send(message);
}
I get this error :
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at....
Line 310: smtp.Send(message);
Any help please ?
This worked for me
Login in to gmail ( in your case it is Esco#gmail.com)
visit
https://www.google.com/settings/security/lesssecureapps
Enable access to less secure apps

failed to send email from yahoo server in gmail is working

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

Categories

Resources